|
THotPDF Loaded Form Field Methods
语法
function GetFormField(FieldIndex: Integer): THPDFLoadedFormField; overload;
function GetFormField(const FieldName: AnsiString): THPDFLoadedFormField; overload;
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;
procedure FlattenFormFields;
procedure SaveLoadedDocument(const TargetFile: TFileName);
说明
这些方法检查并更新已用 LoadFromFile 或 BeginIncrementalUpdate 加载的 PDF 中现存的 AcroForm 字段。它们支持文本字段(/FT /Tx)、按钮字段(/FT /Btn)、choice 字段(/FT /Ch)、签名字段(/FT /Sig)以及未知字段类型 GetLoadedAcroForm 返回解析后的已加载 /AcroForm 字典,便于在使用高级字段方法前直接检查
HasFormFields 报告已加载的 PDF 或当前正在生成的文档是否含有 AcroForm 字段。FormFieldExists 按字段名检查已加载的字段树或正在编写的字段列表,字段不存在时不会抛出异常
GetFormFieldDescription 读取已加载字段的 /TU 备用字段名,PDF 查看器通常把它用作字段工具提示。字段没有描述时返回空字符串
GetFormFieldOptionCount 与 GetFormFieldOptionValue 读取已加载 choice 字段的 /Opt 数组。简单字符串条目直接返回;双元素选项数组返回第一个导出值
IsFormFieldReadOnly 读取已加载字段 /Ff 标志的第 1 位。RenameFormField 更新已加载字段的本地 /T 名称,并拒绝同一已加载表单树中重复的完全限定名称
已加载表单关系缓存在一次惰性线性构建后,双向映射字段、完全限定名、widget、注释和页面。高层修改方法会自动维护或使其失效。通过原始对象 API 做出影响关系的编辑后,请调用 InvalidateLoadedFormRelationshipCache
after relationship-changing edits made through raw object APIs RecalculateLoadedFormFields 按文档顺序遍历已加载的 /AcroForm /CO 数组,求值有界的 AFSimple_Calculate 操作 SUM、AVG、PRD、MIN 与 MAX。更新后的值会重新生成外观
RemoveFormField 从 AcroForm 字段树删除已加载表单字段,并从其页面移除匹配的控件注释。FlattenLoadedFormFields 只把命名字段绘制为其所属页面上的静态内容,移除其控件注释,并保留未触碰的字段与页面。空名称数组选择全部字段。默认权限门拒绝任何 DocMDP 或 FieldMDP 签名禁止的结构性更改
FlattenFormFields 保留全字段便捷契约,把已加载文档委托给同一条签名感知、页面局部路径。返回压平的字段数,没有匹配字段时返回 0
直接编辑字段后,用 SaveLoadedDocument 写出已加载对象图的完整保存副本。对签名 PDF 等 append-only 工作流,用 BeginIncrementalUpdate 加载、编辑字段,然后调用 SaveIncrementalUpdate
示例
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.SaveLoadedDocument('UpdatedForm.pdf');
Field := PDF.GetFormField('ClientName');
try
Field.Value := 'Bob';
Field.ReadOnly := True;
finally
Field.Free;
end;
PDF.LoadFromFile('UpdatedForm.pdf');
PDF.FlattenFormFields;
PDF.SaveLoadedDocument('FlattenedForm.pdf');
另请参阅
|