将多个 PDF 合并在一起并使用 Apache PdfBox 创建新的 PDF/A

Merge several PDF together and create new PDF/A with Apache PdfBox

我正在使用 Apache PdfBox 预设几个非 PDF/A 表单并使用 PDFMergerUtility 将这些 PDF 合并在一起并创建新 PDF 的字节数组。


PDFMergerUtility mergerUtility = new PDFMergerUtility();

// presetting forms of these PDFs is omitted for readability
mergerUtility.addSource(new File("a.pdf"));
mergerUtility.addSource(new File("b.pdf"));
mergerUtility.addSource(new File("c.pdf"));

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
mergerUtility.setDestinationStream(outputStream);

try {    
  mergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
  return outputStream.toByteArray();
} catch (IOException ex) {
  log.error("Unable to merge documents", ex);
  throw new RuntimeException("Unable to merge", ex);
}

有没有办法告诉 PDFMergerUtility 创建一个不能再修改的有效 PDF/A 文档?

将现有 PDF "from the wild" 转换为合规的 PDF/A is a very complex topic, unless you created it yourself. I rather suggest you use a product from Callas or PDF-Tools 或其他公司。

(PDFBox 有一个命令行工具(预检)来检查您的 PDF 是否 PDF/A-1b 兼容。这可以让您领略 "ordinary" PDF 的所有缺点)