Flatten annotations on Android
Nutrient allows annotation flattening using the PdfProcessor
class.
When using the processor API before loading a document, you must ensure Nutrient is fully initialized, or processing will fail. Check out our integrating Nutrient guide for more information.
When flattening an annotation, the annotation is removed from the document while its visual representation is kept intact. A flattened annotation is still visible but is no longer editable by your users or by your app. This can be used to, for example, fix annotations onto your document. If not otherwise specified, the processor will keep all annotations as they are.
To change how annotations are processed, use the PdfProcessorTask#changeAllAnnotations
, PdfProcessorTask#changeAnnotationsOfType
, or PdfProcessorTask#changeAnnotations
method calls:
// Process all pages of the document, flattening all of its annotations. val task = PdfProcessorTask.fromDocument(document).changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN) PdfProcessor.processDocumentAsync(task, ...) // Flatten only free text annotations, and copy everything else. val task = PdfProcessorTask.fromDocument(document).changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP) .changeAnnotationsOfType(AnnotationType.FREETEXT, PdfProcessorTask.AnnotationProcessingMode.FLATTEN) PdfProcessor.processDocumentAsync(task, ...)
// Process all pages of the document, flattening all of its annotations. PdfProcessorTask task = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN); PdfProcessor.processDocumentAsync(task, ...); // Flatten only free text annotations, and copy everything else. PdfProcessorTask task = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP) .changeAnnotationsOfType(AnnotationType.FREETEXT, PdfProcessorTask.AnnotationProcessingMode.FLATTEN); PdfProcessor.processDocumentAsync(task, ...);
Flattening for print
If a document is being flattened for the purposes of printing to paper, flatten using the PRINT
annotation processing mode. This ensures that only the annotations that can be printed will be flattened, and the others will be removed:
// Process all pages of the document, flattening only the annotations that can be printed. val task = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.PRINT) PdfProcessor.processDocumentAsync(task, ...)
// Process all pages of the document, flattening only the annotations that can be printed.
PdfProcessorTask task = PdfProcessorTask.fromDocument(document)
.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.PRINT);
PdfProcessor.processDocumentAsync(task, ...);