Flatten PDF form fields on Android

Nutrient allows form flattening using the PdfProcessor class.

❗Important: 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.

Form elements are of a special annotation type, AnnotationType::WIDGET, so first see our guide on flattening annotations, which also covers flattening for printing. You can use the method discussed there to flatten all types of form elements:

// Flatten only forms, and copy everything else.
val task = PdfProcessorTask.fromDocument(document)
                .changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP)
                .changeAnnotationsOfType(AnnotationType.WIDGET, PdfProcessorTask.AnnotationProcessingMode.FLATTEN)
PdfProcessor.processDocumentAsync(task, ...)
// Flatten only forms, and copy everything else.
PdfProcessorTask task = PdfProcessorTask.fromDocument(document)
				.changeAllAnnotations(PdfProcessorTask.AnnotationProcessingMode.KEEP)
				.changeAnnotationsOfType(AnnotationType.WIDGET, PdfProcessorTask.AnnotationProcessingMode.FLATTEN);
PdfProcessor.processDocumentAsync(task, ...);

If you want to flatten only form elements of a specific FormType, you can use PdfProcessorTask#changeFormsOfType instead.

For example, you might not want to flatten a signature annotation, as only the visual representation of the digital signature — and not the actual digital signature — would be included in the resulting document:

// Flatten all forms except for signature types.
val task = PdfProcessorTask.fromDocument(document)
                .changeAnnotationsOfType(AnnotationType.WIDGET, PdfProcessorTask.AnnotationProcessingMode.FLATTEN)
                .changeFormsOfType(FormType.SIGNATURE, PdfProcessorTask.AnnotationProcessingMode.KEEP)
PdfProcessor.processDocument(task, ...)
// Flatten all forms except for signature types.
PdfProcessorTask task = PdfProcessorTask.fromDocument(document)
				.changeAnnotationsOfType(AnnotationType.WIDGET, PdfProcessorTask.AnnotationProcessingMode.FLATTEN)
				.changeFormsOfType(FormType.SIGNATURE, PdfProcessorTask.AnnotationProcessingMode.KEEP);
PdfProcessor.processDocumentAsync(task, ...);