【发布时间】:2021-03-26 03:46:41
【问题描述】:
在我的应用程序中,注释作为单个注释存储在数据库中。对于文档表中的一个文档,我在注释表中存储了许多注释(多个 xfdf 字符串)。 我编写了一个代码来生成 pdf 并导入这些注释。我为此代码引用了以下链接,
- https://www.pdftron.com/documentation/web/guides/features/forms/import-data/
- https://groups.google.com/g/pdfnet-sdk/c/gXaG5X-zpR8
参数,
- annotations : 带有 xfdf 字符串的注释列表
- downloadedFile : pdf 文件作为缓冲区
- isFlatternAnnotations - 是一个布尔选项 展平注释
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(); 正在运行。如果它未运行,则注释不会合并到文档中。
而且,如果它运行一次,注释会显示而不会展平。但是,如果我将同一行添加 3 次,它就可以正常工作。
我认为我这样做的方式是不正确的。我需要您的帮助才能正确编写此代码。
以下代码可以正常工作,但应该有适当的方法来做到这一点。
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>
【问题讨论】:
-
PDFNet 有两种类型的 XFDF 文件,Regular 和 Command。您可以通过查看 XFDF 并查看是否有
顶级元素来告诉命令。如果那是您的 XFDF 字符串的格式,那么您使用不同的 API。如果存在这些值,请告诉我,我可以提供进一步的建议(理想情况下,您可以在问题中提供这些 XFDF 字符串之一的示例)。 -
@Ryan 很抱歉回复得太晚了。我已经添加了 xfdf 字符串。
-
感谢您的更新,这确认您使用的是备用 XFDF 格式。请参阅我关于如何应用这些服务器端的新答案。