微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 NodeJs 在 Pdftron 中导入多个注释

如何解决使用 NodeJs 在 Pdftron 中导入多个注释

在我的应用程序中,注释作为单个注释存储在数据库中。对于文档表中的一个文档,我在注释表中存储了许多注释(多个 xfdf 字符串)。 我写了一个代码生成pdf并导入这些注释。我参考了此代码的以下链接

参数,

  • 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(); 正在运行。如果未运行,则注释不会合并到文档中。 而且,如果它运行一次,则注释会显示而不展平。但是,如果我将同一行添加三遍,它就可以正常工作。 我认为我这样做的方式是不正确的。我需要您的帮助才能正确编写此代码

以下代码可以正常工作,但应该有正确的方法来做到这一点。

async importAnnotationsTodocument(
    annotations: 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();
,

默认情况下,API pdfDocument.flattenAnnotations() 仅展平 FormFields,而不是注解。 https://www.pdftron.com/api/pdfnet-node/PDFNet.PDFDoc.html#flattenAnnotations__anchor

所以我建议将调用更改为

pdfDocument.flattenAnnotations(false);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。