原創|其它|編輯:郝浩|2011-09-08 10:56:10.000|閱讀 849 次
概述:當操作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來填充一個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來填充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@fc6vip.cn
文章轉載自:慧都控件網