|
THotPDF Loaded Form Field Methods
Syntax
function GetLoadedAcroForm: THPDFDictionaryObject;
function HasFormFields: Boolean;
function FormFieldExists(const FieldName: AnsiString): Boolean;
function GetFormFieldCount: Integer;
function GetFormFieldName(FieldIndex: Integer): AnsiString;
function GetFormFieldValue(FieldIndex: Integer): AnsiString; overload;
function GetFormFieldValue(const FieldName: AnsiString): AnsiString; overload;
function GetFormFieldType(FieldIndex: Integer): THPDFLoadedFormFieldType; overload;
function GetFormFieldType(const FieldName: AnsiString): THPDFLoadedFormFieldType; overload;
function GetFormFieldDescription(FieldIndex: Integer): AnsiString; overload;
function GetFormFieldDescription(const FieldName: AnsiString): AnsiString; overload;
function GetFormFieldOptionCount(FieldIndex: Integer): Integer; overload;
function GetFormFieldOptionCount(const FieldName: AnsiString): Integer; overload;
function GetFormFieldOptionValue(FieldIndex, OptionIndex: Integer): AnsiString; overload;
function GetFormFieldOptionValue(const FieldName: AnsiString; OptionIndex: Integer): AnsiString; overload;
function IsFormFieldReadOnly(FieldIndex: Integer): Boolean; overload;
function IsFormFieldReadOnly(const FieldName: AnsiString): Boolean; overload;
procedure SetFormFieldValue(FieldIndex: Integer; const Value: AnsiString); overload;
procedure SetFormFieldValue(const FieldName, Value: AnsiString); overload;
procedure SetFormFieldReadOnly(FieldIndex: Integer; ReadOnly: Boolean); overload;
procedure SetFormFieldReadOnly(const FieldName: AnsiString; ReadOnly: Boolean); overload;
procedure RenameFormField(FieldIndex: Integer; const NewName: AnsiString); overload;
procedure RenameFormField(const FieldName, NewName: AnsiString); overload;
procedure RemoveFormField(FieldIndex: Integer); overload;
procedure RemoveFormField(const FieldName: AnsiString); overload;
function RecalculateLoadedFormFields: Integer;
function FlattenLoadedFormFields(const FieldNames: array of AnsiString;
EnforceSignaturePermissions: Boolean = True): Integer;
function FlattenFormFields: Integer;
procedure SaveLoadedDocument(const TargetFile: TFileName);
Description
These methods inspect and update AcroForm fields that already exist in
a PDF loaded with LoadFromFile or
BeginIncrementalUpdate. They support text fields
(/FT /Tx), button fields (/FT /Btn), choice
fields (/FT /Ch), signature fields
(/FT /Sig), and unknown field types
GetLoadedAcroForm returns the resolved loaded
/AcroForm dictionary for callers that need direct
dictionary inspection before using the higher-level field methods
HasFormFields reports whether the loaded PDF or current
in-progress document has AcroForm fields. FormFieldExists
checks a loaded field tree or the authoring field list by field name
without raising when the field is absent
GetFormFieldDescription reads the loaded field's
/TU alternate field name, commonly used by PDF viewers as
the field tooltip. It returns an empty string when the field has no
description
GetFormFieldOptionCount and
GetFormFieldOptionValue read the /Opt array
on loaded choice fields. Simple string entries are returned directly;
two-element option arrays return the first export value
IsFormFieldReadOnly reads bit 1 of the loaded field's
/Ff flag. RenameFormField updates the loaded
field's local /T name and rejects duplicate fully qualified
names in the same loaded form tree
RecalculateLoadedFormFields visits the loaded
/AcroForm /CO array in document order and evaluates the
bounded AFSimple_Calculate operations SUM,
AVG, PRD, MIN, and
MAX. Updated values receive regenerated appearances
RemoveFormField deletes a loaded form field from the
AcroForm field tree and removes the matching widget annotations from
its pages. FlattenLoadedFormFields paints only the named
fields into static content on their owning pages, removes their widget
annotations, and preserves untouched fields and pages. An empty name
array selects every field. The default permission gate refuses any
structural change prohibited by DocMDP or FieldMDP signatures
FlattenFormFields retains the all-fields convenience
contract and delegates loaded documents to the same signature-aware,
page-local path. It returns the number of fields flattened, or zero
when no matching form fields were present
Use SaveLoadedDocument to write a fully saved copy of the
loaded object graph after direct field edits. For append-only workflows
such as signed PDFs, load with BeginIncrementalUpdate, edit
fields, and then call SaveIncrementalUpdate
Example
PDF.LoadFromFile('InputForm.pdf');
for I := 0 to PDF.GetFormFieldCount - 1 do
Writeln(PDF.GetFormFieldName(I), ' = ', PDF.GetFormFieldValue(I));
for I := 0 to PDF.GetFormFieldOptionCount('Plan') - 1 do
Writeln('Plan option ', I, ': ', PDF.GetFormFieldOptionValue('Plan', I));
Writeln('Customer description: ', PDF.GetFormFieldDescription('CustomerName'));
PDF.SetFormFieldValue('CustomerName', 'Bob');
PDF.SetFormFieldReadOnly('CustomerName', True);
PDF.RenameFormField('CustomerName', 'ClientName');
PDF.RemoveFormField('Approved');
PDF.RecalculateLoadedFormFields;
PDF.SaveLoadedDocument('UpdatedForm.pdf');
PDF.LoadFromFile('UpdatedForm.pdf');
if PDF.HasFormFields then
Writeln('Plan exists: ', PDF.FormFieldExists('Plan'));
FlattenedCount := PDF.FlattenFormFields;
PDF.SaveLoadedDocument('FlattenedForm.pdf');
PDF.BeginIncrementalUpdate('SignedForm.pdf');
FlattenedCount := PDF.FlattenLoadedFormFields(['CalculatedTotal']);
PDF.SaveIncrementalUpdate('SignedForm-flattened.pdf');
See Also
|