Import and Export Annotations using Java

Copy annotations from one PDF to another

  1. Open the source PDF Document.
  2. Add a Page to the destination Document.
  3. Add each Annotation to the target Page.
  4. Read or iterate through the Annotation items on the target page.
  5. Save the updated PDF Document.
  6. Enumerate the Annotation items on the first source page and add each one to the destination page.
public static void importExport(Path inputFile, Path outputFile) {
    try (Document sourceDocument = new Document(inputFile.toString());
         Document destinationDocument = new Document()) {
        Page page = destinationDocument.getPages().add();

        for (Annotation annotation : sourceDocument.getPages().get_Item(1).getAnnotations()) {
            page.getAnnotations().add(annotation, true);
        }

        destinationDocument.save(outputFile.toString());
    }
}