我正在处理一个应用程序,我需要能够编辑现有的PDF表单,并在文件上以交互方式删除PDF表单字段。
表单字段,如下拉,文本,多行文本,单选按钮。目前,我正在使用ActiveX控件在Delphi应用程序中打开PDF文件。
然而,我只能阅读PDF表单,但是我希望能够编辑它,就像删除表单字段一样。
其想法是在现有的PDF表单上添加表单并保存它。我已经尝试过Gnostice,PowerPDF,quickPDF,但是他们不允许像Adobe那样交互地修改PDF文件。这个在线应用程序PDFEscape允许我们创建新的和编辑现有的PDF表单交互。

在“诺斯替”中,我们可以编程地编辑,例如:

问题:
发布于 2016-03-21 08:29:40
Acrobat的完整版本包括从Delphi代码访问和操作表单字段的功能,并支持将表单字段添加到文档中、通过OLE自动化在使用代码中填充表单字段以及在不需要时删除它们的操作。
你当然可以在你自己的应用程序中托管一个Acrobat窗口,这样它就可以显示f.i。在TPanel中。您可以使用OpenInWindowEx中的CAcroAVDoc对象的Acrobat_TLB.Pas方法来实现这一点。然而,这只是绘制PDF文档(加上注释/注释,iirc),它不提供显示或提供对Acrobat的gui的访问。在Delphi应用程序中,我知道的唯一方法是在TOleContainer对象中打开Pdf文件,但如果这样做,Acrobat的gui将在自己的窗口中打开,而不是托管在您的窗口中。而现在的OleContainer界面在屏幕上看上去也是过时的。
所以,如果您想要与Acrobat的表单字段编辑器gui进行交互,我认为您被困住了。另一方面,您可以使用自己的DIY代码操作表单字段并填充它们--参见下面的示例。
要在代码中操作表单字段,需要将Acrobat类型库导入到Delphi中。
您需要导入的类型库相当于
D:\Program Files\Adobe\Acrobat 8.0\Acrobat\plug_ins\AcroForm.api 您可能需要使用Delphi的TRegSvr实用程序(或Windows的RegSvr32)向Windows注册此文件;
这使您可以访问IAFormApp对象(它是用于处理窗体的顶级对象),以及用于创建单个字段的IField对象。
如果您有Acrobat文档,那么通过Ole自动化使用这些对象的方法将在“应用程序间通信API引用”(iac_api_reference.pdf)中描述。我在下面包含了IFIeld接口的Delphi,这样您就可以看到您可以用它做的事情了。
“有没有一种方法可以交互式地编辑PDF表单以删除表单字段?”
在Acrobat中打开表单后,可以删除一个字段(如果" drop“指的是"delete")。您可以通过调用Remove接口的IFields方法在代码中这样做。如果otoh的意思是“我们可以删除表单上的字段”,那么也可以通过创建一个可用字段列表并从列表中选择一个字段来做到这一点。
“我们可以在Delphi中嵌入Adobe或任何其他软件来编辑现有的PDF吗?”
是的,下面的示例演示如何从Delphi代码中填充现有字段并添加新字段:
implementation
{$R *.DFM}
uses
AFORMAUTLib_TLB, Acrobat_TLB;
const
scDocument = 'D:\delphi\code\acrobat\blank.pdf';
procedure TForm1.FillInForm;
var
Acrobat : CAcroApp;
AVDoc : CAcroAVDoc;
PDDoc : CAcroPDDoc;
FormApp : IAFormApp;
Fields : IFields;
Field : IField;
begin
Acrobat := CoAcroApp.Create;
AVDoc := CoAcroAVDoc.Create;
AVDoc.Open(scDocument, scDocument);
PDDoc := AVDoc.GetPDDoc as CAcroPDDoc;
FormApp := CoAFormApp.Create;
Fields := FormApp.Fields as IFields;
Field := Fields.Item['Text1'] as IField;
Field.Value := 'My test';
Field := Fields.Add('AnotherField', 'text',
0,
360,
790,
360 + 200,
790 - 30) as IField;
Field.Value := 'Added';
end;顺便说一句,我不知道你是否打算问这个问题,但也可以在Delphi应用程序的窗口中托管Acrobat的gui。但是,如果你想知道这一点,你需要在一个单独的q问它,因为这是一个不同的技术问题。
从AFORMAUTLib_TLB.Pas中提取:
*********************************************************************//
// Interface: IField
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {673E8454-7646-11D1-B90B-00A0C9259304}
// *********************************************************************//
IField = interface(IDispatch)
['{673E8454-7646-11D1-B90B-00A0C9259304}']
// getters amd setters omitted
procedure SetBorderColor(const bstrColorSpace: WideString; GorRorC: Single; GorM: Single;
BorY: Single; K: Single); safecall;
procedure SetBackgroundColor(const bstrColorSpace: WideString; GorRorC: Single; GorM: Single;
BorY: Single; K: Single); safecall;
procedure SetJavaScriptAction(const bstrTrigger: WideString; const bstrTheScript: WideString); safecall;
procedure SetSubmitFormAction(const bstrTrigger: WideString; const bstrTheURL: WideString;
theFlags: Integer; arrFields: OleVariant); safecall;
procedure SetResetFormAction(const bstrTrigger: WideString; theFlags: Integer;
arrFields: OleVariant); safecall;
procedure SetButtonIcon(const bstrFace: WideString; const bstrFullPath: WideString;
pageNum: Smallint); safecall;
procedure SetForegroundColor(const bstrColorSpace: WideString; GorRorC: Single; GorM: Single;
BorY: Single; K: Single); safecall;
procedure PopulateListOrComboBox(arrItems: OleVariant; arrExportVal: OleVariant); safecall;
procedure SetButtonCaption(const bstrFace: WideString; const bstrCaption: WideString); safecall;
property Name: WideString read Get_Name;
property Value: WideString read Get_Value write Set_Value;
property IsHidden: WordBool read Get_IsHidden write Set_IsHidden;
property IsTerminal: WordBool read Get_IsTerminal;
property Type_: WideString read Get_Type_;
property IsReadOnly: WordBool read Get_IsReadOnly write Set_IsReadOnly;
property IsRequired: WordBool read Get_IsRequired write Set_IsRequired;
property PrintFlag: WordBool read Get_PrintFlag write Set_PrintFlag;
property BorderWidth: Smallint read Get_BorderWidth write Set_BorderWidth;
property Alignment: WideString read Get_Alignment write Set_Alignment;
property CharLimit: Smallint read Get_CharLimit write Set_CharLimit;
property DefaultValue: WideString read Get_DefaultValue write Set_DefaultValue;
property IsMultiline: WordBool read Get_IsMultiline write Set_IsMultiline;
property IsPassword: WordBool read Get_IsPassword write Set_IsPassword;
property CalcOrderIndex: Smallint read Get_CalcOrderIndex write Set_CalcOrderIndex;
property BorderStyle: WideString read Get_BorderStyle write Set_BorderStyle;
property Editable: WordBool read Get_Editable write Set_Editable;
property Highlight: WideString read Get_Highlight write Set_Highlight;
property Style: WideString read Get_Style write Set_Style;
property TextFont: WideString read Get_TextFont write Set_TextFont;
property TextSize: Smallint read Get_TextSize write Set_TextSize;
property ButtonLayout: Smallint read Get_ButtonLayout write Set_ButtonLayout;
property NoViewFlag: WordBool read Get_NoViewFlag write Set_NoViewFlag;
end;更新11月11日2021年
我正在更新这个答案,以响应一个删除的非答复,要求详细说明如何在一个最新版本的Delphi中生成导入单元Acrobat_Tlb.Pas和AFORMAUTLib_TLB.Pas。
以下方法适用于从Adobe商店和Delphi 10.4.2购买并安装的“AdobeAcrobatStandard2020”。
对于Acrobat_Tlb.Pas
对于AFORMAUTLib_Tlb.Pas
通过以下更改,重复Acrobat_Tlb.Pas的步骤:
https://stackoverflow.com/questions/36123427
复制相似问题