Flatten annotations on Android
Use the PdfProcessor class to flatten annotations.
Initialize Nutrient before using the processor API, including before loading a document. Otherwise, processing fails. For details, refer to the initializing Nutrient guide.
Flattening removes the annotation object from the document but preserves its appearance as page content. Neither users nor your app can edit the flattened content as an annotation. By default, the processor keeps annotations unchanged.
Use one of these methods to control how the processor handles annotations:
PdfProcessorTask#changeAllAnnotationsPdfProcessorTask#changeAnnotationsOfTypePdfProcessorTask#changeAnnotations
The following examples flatten all annotations or only free text annotations:
// `processDocument()` blocks until processing finishes, so run it in the background.lifecycleScope.launch(Dispatchers.IO) { // Process all pages of the document, flattening all of its annotations. val flattenAllTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN) // Write to a new file. The output can't be the file the document was opened from. PdfProcessor.processDocument(flattenAllTask, File(context.filesDir, "flattened.pdf"))
// Flatten only free text annotations, and copy everything else. val flattenFreeTextTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP) .changeAnnotationsOfType(AnnotationType.FREETEXT, PdfProcessorTask.AnnotationProcessingMode.FLATTEN) PdfProcessor.processDocument(flattenFreeTextTask, File(context.filesDir, "free-text-flattened.pdf"))}// `processDocument()` blocks until processing finishes, so run it on a background thread.new Thread(() -> { // Process all pages of the document, flattening all of its annotations. PdfProcessorTask flattenAllTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.FLATTEN); // Write to a new file. The output can't be the file the document was opened from. PdfProcessor.processDocument(flattenAllTask, new File(context.getFilesDir(), "flattened.pdf"));
// Flatten only free text annotations, and copy everything else. PdfProcessorTask flattenFreeTextTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP) .changeAnnotationsOfType(AnnotationType.FREETEXT, PdfProcessorTask.AnnotationProcessingMode.FLATTEN); PdfProcessor.processDocument(flattenFreeTextTask, new File(context.getFilesDir(), "free-text-flattened.pdf"));}).start();Flattening for print
To prepare a document for printing, use the PRINT annotation processing mode. This mode flattens printable annotations and removes the others:
// Process all pages of the document, flattening only the annotations that can be printed.val printTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.PRINT)val outputFile = File(context.filesDir, "flattened-for-print.pdf")
lifecycleScope.launch(Dispatchers.IO) { PdfProcessor.processDocument(printTask, outputFile)}// Process all pages of the document, flattening only the annotations that can be printed.PdfProcessorTask printTask = PdfProcessorTask.fromDocument(document) .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.PRINT);File outputFile = new File(context.getFilesDir(), "flattened-for-print.pdf");
new Thread(() -> PdfProcessor.processDocument(printTask, outputFile)).start();