彩票走势图

使用Aspose.Pdf和Acrobat Automation填充表单域的不同之处

原创|其它|编辑:郝浩|2011-09-08 10:56:10.000|阅读 829 次

概述:当操作PDF文件时,通过编程来填充表单域是一种常见的任务。Adobe Acrobat Automation 和Aspose.Pdf for .NET都提供了该功能。本文将通过两个示例来向你展示Adobe Acrobat Automation 和Aspose.Pdf for .NET填充表单域功能。通过示例,你会发现Aspose.Pdf for .NET在实现填充表单域功能时非常简单易用,而且你还可以截取下面的代码片段来帮你实现自己的项目任务。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  当操作PDF文件时,通过编程来填充表单域是一种常见的任务。Adobe Acrobat Automation 和Aspose.Pdf for .NET都提供了该功能。本文将通过两个示例来向你展示Adobe Acrobat Automation 和Aspose.Pdf for .NET填充表单域功能。通过示例,你会发现Aspose.Pdf for .NET在实现填充表单域功能时非常简单易用,而且你还可以截取下面的代码片段来帮你实现自己的项目任务。

  在这个例子中,我们有一个文本字段名为“Name”的PDF文件,打算以“John Doe”值填充到PDF文件中。使用Acrobat Automation,你需要获取字段名列表,并通过导航列表找到你所需要的字段。一旦在列表中找到该字段,您可以设置新的值。然而,Aspose.Pdf for .NET中的Aspose.Pdf.Facades 命名空间仅使用了一个简单的FillField方法便为你提供了所有的功能。

Adobe Acrobat Automation

  下面的代码展示了如何使用Adobe Acrobat Automation来填充一个PDF表单域。

[C#]


String FORM_NAME = Application.StartupPath + "\\..\\..\\..\\..\\..\\TestFiles\\input.pdf";

// Initialize Acrobat by cretaing App object.

CAcroApp acroApp = new AcroAppClass();

 

// Show Acrobat Viewer

acroApp.Show();

 

// Create an AVDoc object

CAcroAVDoc avDoc = new AcroAVDocClass();

 

// Open the pdf

if(!avDoc.Open (FORM_NAME, ""))

{

string szMsg = "Cannot open" + FORM_NAME + ".\n";

Console.WriteLine(szMsg);

return;

}

 

// Create a IAFormApp object, so that we can access the form fields in

// the open document

IAFormApp formApp = new AFormAppClass();

 

// Get the IFields object associated with the form

IFields myFields = (IFields)formApp.Fields;

 

// Get the IEnumerator object for myFields

IEnumerator myEnumerator = myFields.GetEnumerator();

 

bool bFound = false;

 

// Fill the ";Name" field with value "John Doe"

while(myEnumerator.MoveNext())

{

// Get the IField object

IField myField = (IField)myEnumerator.Current;

 

// If the field is "Name", set it's value to "John Doe"

if(myField.Name == "Name")

{

bFound = true;

myField.Value = "John Doe";

break;

}

}

 

if(bFound)

Console.WriteLine("Sucessfully changed the \"Name\" field value to \"John Doe\".");

else
Console.WriteLine("Failed to locate the \"Name\" field in the form.");

[VB.NET]


Dim FORM_NAME As String = Application.StartupPath & "\..\..\..\..\..\TestFiles\input.pdf"

' Initialize Acrobat by cretaing App object.

Dim acroApp As CAcroApp = New AcroAppClass()

 

' Show Acrobat Viewer

acroApp.Show()

 

' Create an AVDoc object

Dim avDoc As CAcroAVDoc = New AcroAVDocClass()

 

' Open the pdf

If (Not avDoc.Open (FORM_NAME, "")) Then

Dim szMsg As String = "Cannot open" & FORM_NAME & "." & Constants.vbLf

Console.WriteLine(szMsg)

Return

End If

 

' Create a IAFormApp object, so that we can access the form fields in

' the open document

Dim formApp As IAFormApp = New AFormAppClass()

 

' Get the IFields object associated with the form

Dim myFields As IFields = CType(formApp.Fields, IFields)

 

' Get the IEnumerator object for myFields

Dim myEnumerator As IEnumerator = myFields.GetEnumerator()

 

Dim bFound As Boolean = False

 

' Fill the "Name" field with value "John Doe"

Do While myEnumerator.MoveNext()

' Get the IField object

Dim myField As IField = CType(myEnumerator.Current, IField)

 

' If the field is "Name", set it's value to "John Doe";

If myField.Name = "Name" Then

bFound = True

myField.Value = "John Doe"

Exit Do

End If

Loop

 

If bFound Then

Console.WriteLine("Sucessfully changed the ""Name"" field value to ""John Doe"".")

Else

Console.WriteLine("Failed to locate the ""Name"" field in the form.")

End If

Aspose.Pdf for .NET

下面的代码展示了如何使用Aspose.Pdf for .NET来填充PDF表单域。

[C#]


//create Form object and open PDF file

Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form("input.pdf", "output.pdf");

//set the value of the particular field

pdfForm.FillField("Name", "John Doe");

//save the output PDF file

pdfForm.Save();

[VB.NET]


'create Form object and open PDF file

Dim pdfForm As New Aspose.Pdf.Facades.Form("input.pdf", "output.pdf")

'set the value of the particular field

pdfForm.FillField("Name", "John Doe")

'save the output PDF file

pdfForm.Save()

Console.WriteLine("Failed to locate the \"Name\" field in the form.");



(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP