Skip to content

文档级附件 (Attachment)

在福昕 PDF SDK 中,attachments 指的是文档附件而不是文件附件注释。它允许将整个文件封装在文档中,就像电子邮件附件一样。福昕 PDF SDK 提供应用程序 APIs 来访问附件,例如加载附件,获取附件,插入/删除附件,以及访问附件的属性。

如何将指定文件嵌入到 PDF 文档

js
import { FoxitRDKNative } from 'foxit_rdk';

class AttachmentUnit {
  private addFileAttachment(): void {
    try {
      const pdfpath = "XXX/Sample.pdf";
      const doc = new FoxitRDKNative.pdf.PDFDoc(pdfpath);
      doc.Load('');

      // 将指定文件嵌入到PDF文档中
      const filePath = "/xxx/fileToBeEmbedded.xxx";
      const nameTree = new FoxitRDKNative.pdf.objects.PDFNameTree(doc, FoxitRDKNative.pdf.objects.PDFNameTree.e_EmbeddedFiles);
      const attachments = new FoxitRDKNative.pdf.Attachments(doc, nameTree);
      const fileSpec = new FoxitRDKNative.pdf.FileSpec(doc);
      fileSpec.SetFileName(filePath);
      if (!fileSpec.Embed(filePath)) {
        return;
      }
      attachments.AddEmbeddedFile(filePath, fileSpec);
    } catch (e) {
      console.error(e);
    }
  }
}

如何从 PDF 文档中导出嵌入的附件文件,并将其另存为单个文件

js
import { FoxitRDKNative } from 'foxit_rdk';

class AttachmentUnit2 {
  private exportAttachment(): void {
    try {
      const pdfpath = "XXX/Sample.pdf";
      const doc = new FoxitRDKNative.pdf.PDFDoc(pdfpath);
      doc.Load('');

      const nameTree =
        new FoxitRDKNative.pdf.objects.PDFNameTree(doc, FoxitRDKNative.pdf.objects.PDFNameTree.e_EmbeddedFiles);
      const attachments = new FoxitRDKNative.pdf.Attachments(doc, nameTree);
      // 提取嵌入的附件文件
      const count = attachments.GetCount();
      for (let i = 0; i < count; i++) {
        const key: string = attachments.GetKey(i);
        if (key != null && key.length > 0) {
          const fileSpec1 = attachments.GetEmbeddedFile(key);
          const exportedFile = "/somewhere/" + fileSpec1.GetFileName();
          if (fileSpec1 != null && !fileSpec1.IsEmpty()) {
            fileSpec1.ExportToFile(exportedFile);
          }
        }
      }
    } catch (e) {
      console.error(e);
    }
  }
}