function FlattenPage(nFlag: Integer = FLAT_NORMALDISPLAY): Integer;
| nFlag | Integer. Selects the flattening profile. Pass FLAT_NORMALDISPLAY (0, default) to optimise the output for on-screen viewing, or FLAT_PRINT (1) to optimise for printed output. |
FLATTEN_SUCCESS (1) — annotations were merged into the page content successfully; FLATTEN_NOTHINGTODO (2) — the page had no annotations or widgets to flatten; FLATTEN_FAIL (0) — the operation failed.FlattenPage merges all annotations and AcroForm widgets on the current page into the permanent, non-editable page content stream. Once flattened, the annotations are no longer interactive — they become ordinary graphics that cannot be edited, deleted, or submitted. This is the standard technique for producing a "locked" PDF that preserves the visual appearance of a filled form or annotated document.
The nFlag parameter selects the rendering profile used when drawing widgets into the page content. Use FLAT_NORMALDISPLAY (0) for documents that will be viewed on screen, and FLAT_PRINT (1) for documents destined for printing.
When the page contains filled-in form fields, you must call GenerateFormAppearances before FlattenPage. Without appearance streams, the flattener has no visual representation to merge and will silently omit the field values from the output. The recommended sequence for a single page is: assign FormField[] values → GenerateFormAppearances → FlattenPage → SaveAs.
To flatten every page in a document in one call use FlattenAllPages. Check the return value against FLATTEN_FAIL to detect errors; FLATTEN_NOTHINGTODO is not an error condition.
var
Res: Integer;
begin
Pdf1.PageIndex := 0;
// Fill a form field, regenerate appearances, then flatten
Pdf1.FormField[0] := 'John Smith';
Pdf1.GenerateFormAppearances;
Res := Pdf1.FlattenPage(FLAT_NORMALDISPLAY);
if Res = FLATTEN_FAIL then
ShowMessage('Flatten failed')
else
Pdf1.SaveAs('output_flat.pdf');
end;