添加附件

添加文档附件

  1. 将源 PDF 绑定到 PdfContentEditor 对象。
  2. 将附件文件作为输入流打开。
  3. 调用 addDocumentAttachment(...) 使用流、文件名和描述。
  4. 保存更新后的 PDF 文档。
public static void addAttachment(Path inputFile, Path attachmentFile, Path outputFile) throws Exception {
    PdfContentEditor editor = new PdfContentEditor();
    try (InputStream attachmentStream = Files.newInputStream(attachmentFile)) {
        editor.bindPdf(inputFile.toString());
        editor.addDocumentAttachment(attachmentStream, attachmentFile.getFileName().toString(), "Sample attachment.");
        editor.save(outputFile.toString());
    } finally {
        editor.close();
    }
}