在我的应用程序中,注释作为单个注释存储在数据库中。对于文档表中的一个文档,我在注释表中存储了许多注释(多个xfdf字符串(。我写了一个代码来生成pdf并导入这些注释。我参考了这个代码的以下链接,
- https://www.pdftron.com/documentation/web/guides/features/forms/import-data/
- https://groups.google.com/g/pdfnet-sdk/c/gXaG5X-zpR8
参数,
- annotations:带有xfdfString的注释列表
- downloadedFile:pdf文件作为缓冲区
- isFlatternAnnotifications-是一个布尔选项,用于展开批注
async importAnnotationsToDocument(
annotations: any,
downloadedFile: any,
isFlatternAnnotations: any,
) {
await PDFNet.initialize();
const pdfDocument = await PDFNet.PDFDoc.createFromBuffer(downloadedFile);
pdfDocument.lock();
let fdfDocument = null;
annotations.forEach(async annotation => {
fdfDocument = await PDFNet.FDFDoc.createFromXFDF(annotation.xfdfString);
await pdfDocument.fdfMerge(fdfDocument);
});
if (isFlatternAnnotations === 'true') {
await pdfDocument.flattenAnnotations();
}
const documentBuffer = await pdfDocument.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_remove_unused,
);
const documentBufferResponse = Buffer.from(documentBuffer);
PDFNet.shutdown();
return documentBufferResponse;
}
然而,我注意到只有await pdfDocument.flattenAnnotations();
在运行时代码才起作用。如果没有运行,则不会在文档中合并注释。而且,如果它运行一次,注释将在不展开的情况下显示。但如果我把同一行加三次,它就正确了。我认为我这样做是不对的。我需要你的帮助才能正确地写下这段代码。
下面的代码工作正常,但应该有一个正确的方法来做到这一点。
async importAnnotationsToDocument(
annotations: any,
downloadedFile: any,
isFlatternAnnotations: any,
) {
await PDFNet.initialize();
const pdfDocument = await PDFNet.PDFDoc.createFromBuffer(downloadedFile);
pdfDocument.lock();
let fdfDocument = null;
annotations.forEach(async annotation => {
fdfDocument = await PDFNet.FDFDoc.createFromXFDF(annotation.xfdfString);
await pdfDocument.fdfMerge(fdfDocument);
});
if (isFlatternAnnotations === 'true') {
await pdfDocument.flattenAnnotations();
await pdfDocument.flattenAnnotations();
await pdfDocument.flattenAnnotations();
} else {
// This shows the annotations without flattening
await pdfDocument.flattenAnnotations();
}
const documentBuffer = await pdfDocument.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_remove_unused,
);
const documentBufferResponse = Buffer.from(documentBuffer);
PDFNet.shutdown();
return documentBufferResponse;
}
以下是单个注释的xfdf字符串
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields />
<add>
<square page="0"
rect="387.88,525.73,525,609.07"
color="#FFCD45" flags="print"
name="d1aa1a2a-822f-507b-6ff6-d61bcc6bd862"
title="test.title" subject="Rectangle"
date="D:20210405104448+08'00'"
interior-color="#FFCD45"
opacity="0.5"
creationdate="D:20210405104445+08'00'" />
</add>
<modify /><delete />
</xfdf>
您正在加载XFDF的一个变体,称为Command,您可以在这里看到文档。https://www.pdftron.com/documentation/web/guides/xfdf/
以下代码是如何加载XFDF命令XML数据并将其应用于PDFDoc
实例的。
let fdfDoc = await pdfDoc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
await fdfDoc.saveAsXFDF('data.xfdf');
fdfDoc = await PDFNet.FDFDoc.createFromXFDF('data.xfdf');
await fdfDoc.mergeAnnots(str);
await pdfDoc.fdfUpdate(fdfDoc);
await pdfDoc.refreshAnnotAppearances();
默认情况下,APIpdfDocument.flattenAnnotations()
仅展平FormFields,而不展平注释。https://www.pdftron.com/api/pdfnet-node/PDFNet.PDFDoc.html#flattenAnnotations__anchor
所以我建议将呼叫改为
pdfDocument.flattenAnnotations(false);