node.js - 使用 NodeJs 在 Pdftron 中导入多个注释
问题描述
在我的应用程序中,注释作为单个注释存储在数据库中。对于文档表中的一个文档,我在注释表中存储了许多注释(多个 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 字符串的注释列表
- 下载文件 : 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>
解决方案
您正在加载一个名为 Command 的 XFDF 变体,您可以在此处查看文档。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();
推荐阅读
- graph - 具有后边缘的深度优先搜索的时间复杂度
- python - Python:按值范围拆分熊猫数据框
- reactjs - 从 React 组件加载 Django 静态文件
- generics - Apache Nifi - 使用通用流程处理 100 种不同 Jolt 转换的策略
- pandas - 如何用条件将两列替换为一列?[熊猫]
- sql-server - 表值函数查询是否缓存?
- postgresql - 如何列出 Postgres 中的所有会话变量?
- graph - GNUPlot 中具有不同行为的错误栏
- javascript - 使用分页时如何使用 querySelectorAll?
- css - 我的样式表没有加载到我的 jsp 文件中。如何解决这个问题?