Save only annotated pages

Result document will contains only pages, which contains any annotation.

The following are the steps how to export to resultant document only annotated pages: 

  • Instantiate Annotator object with input document path or stream;
  • Instantiate SaveOptions object and set AnnotationType desired annotation type(s), that will presented in resultant document;
  • Call save method with resultant document path or stream and SaveOptions object;

Following code snippet shows how to save only annotated pages of document

String outputPath = "SavingOnlyPagesWithAnnotations.pdf";

Annotator annotator = new Annotator("input.pdf");

AreaAnnotation area = new AreaAnnotation();
area.setBox(new Rectangle(100, 100, 100, 100));
area.setBackgroundColor(65535);
area.setPageNumber(1);

EllipseAnnotation ellipse = new EllipseAnnotation();
ellipse.setBox(new Rectangle(100, 100, 100, 100));
ellipse.setBackgroundColor(123456);
ellipse.setPageNumber(2);

List<AnnotationBase> annotations = new ArrayList<AnnotationBase>();
annotations.add(area);
annotations.add(ellipse);

annotator.add(annotations);

SaveOptions saveOptions = new SaveOptions();
saveOptions.setOnlyAnnotatedPages(true);
annotator.save(outputPath, saveOptions);

annotator.dispose();

More resources

Advanced Usage Topics

To learn more about document annotating features, please refer to the advanced usage section.

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple, but powerful free Apps.
You are welcome to annotate your PDF, DOC or DOCX, XLS or XLSX, PPT or PPTX, PNG and other documents with free to use online GroupDocs Annotation App.