Add Attachment

Add a document attachment

  1. Bind the source PDF to the PdfContentEditor facade.
  2. Open the attachment file as an input stream.
  3. Call addDocumentAttachment(...) with the stream, file name, and description.
  4. Save the updated PDF document.
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();
    }
}