首页 > 解决方案 > 使用 NodeJs 在 Pdftron 中导入多个注释

问题描述

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

参数,

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>

标签: node.jspdfpdftron

解决方案


您正在加载一个名为 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();

推荐阅读