Import and Export Annotations using Java

Copy annotations from one PDF to another

  1. Open the source PDF and create a new destination document with a target page.
  2. Enumerate the annotations on the first source page and add each one to the destination page.
  3. Save the destination document to persist the copied annotations.
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());
    }
}