.NET PDF生成器:一次單擊
本教程將演示如何以程式方式與PDF檔案中的表單互動。
市面上有多個.NET程式庫允許我們使用C#以程式方式填寫PDF表單。 其中一些難以理解,一些需要付費。
IronPDF是最好的.NET Core程式庫,因為它易於理解並且開發免費。 除了填寫PDF表單外,IronPDF還允許從HTML字串、HTML檔案和URL建立新的PDF。
讓我們看看如何使用C#以程式方式填寫PDF表單。 首先,將建立一個Console應用程式進行演示,但您可以根據需要使用任何應用程式。
建立Visual Studio專案
打開Microsoft Visual Studio。 點擊建立新專案 > 從範本中選擇Console應用程式 > 點擊下一步 > 為專案命名。 點擊下一步 > 選擇目標框架。 點擊建立按鈕。 將建立如下面所示的專案。
在Visual Studio中新建立的Console應用程式
安裝IronPDF程式庫
如前所述,本教程將使用IronPDF程式庫。 使用此.NET程式庫的主要原因是開發免費,並且在單一程式庫中提供所有功能。
轉到套件管理器控制台。 輸入以下命令:
Install-Package IronPdf
該命令將為我們安裝IronPDF程式庫。 接下來,讓我們開始編碼。
讀取PDF文件
填寫PDF表單的第一步是讀取PDF文件。 顯然,如果不先讀取它,我們怎麼能填寫表單呢?以下的PDF文件將用於演示。 您可以從Google Drive Link下載,或者您可以使用自己的文件。
範例PDF文件用於填寫表單
讀取此檔案的程式碼如下:
using IronPdf;
// Load the PDF document from the file path
PdfDocument doc = PdfDocument.FromFile(@"D:\myPdfForm.pdf");
using IronPdf;
// Load the PDF document from the file path
PdfDocument doc = PdfDocument.FromFile(@"D:\myPdfForm.pdf");
Imports IronPdf
' Load the PDF document from the file path
Private doc As PdfDocument = PdfDocument.FromFile("D:\myPdfForm.pdf")
在FromFile方法中傳遞PDF文件的完整路徑。 這將從您的本地系統讀取PDF文件。
獲取PDF表單
編寫以下程式碼行以從載入的PDF文件中獲取表單。
var form = doc.Form;
var form = doc.Form;
Dim form = doc.Form
獲取表單欄位
要獲取表單欄位以設置其值,IronPDF使這變得非常簡單,可以通過兩種方法存取表單欄位:按欄位名稱或通過索引。 讓我們逐一討論。
按名稱獲取表單欄位
以下程式碼將按名稱獲取表單欄位:
// Retrieve the form field using its name
var field = form.FindFormField("First Name");
// Retrieve the form field using its name
var field = form.FindFormField("First Name");
' Retrieve the form field using its name
Dim field = form.FindFormField("First Name")
FindFormField方法將欄位名稱作為參數。 這是容錯的,並將嘗試匹配大小寫錯誤和部分欄位名稱。
按索引獲取表單欄位
我們還可以使用索引獲取PDF表單欄位。 索引從零開始。 以下範例程式碼用於通過索引獲取表單欄位。
// Retrieve the form field using its index
var field = form.Fields[0];
// Retrieve the form field using its index
var field = form.Fields[0];
' Retrieve the form field using its index
Dim field = form.Fields(0)
填寫PDF表單
接下來,讓我們將所有程式碼結合起來以填寫PDF表單。
using IronPdf;
class Program
{
static void Main()
{
// Load the PDF document from the file path
PdfDocument doc = PdfDocument.FromFile(@"D:\myPdfForm.pdf");
// Access the PDF form
var form = doc.Form;
// Fill out the form fields using their index
form.Fields[0].Value = "John";
form.Fields[1].Value = "Smith";
form.Fields[2].Value = "+19159969739";
form.Fields[3].Value = "John@email.com";
form.Fields[4].Value = "Chicago";
// Save the modified PDF document
doc.SaveAs(@"D:\myPdfForm.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Load the PDF document from the file path
PdfDocument doc = PdfDocument.FromFile(@"D:\myPdfForm.pdf");
// Access the PDF form
var form = doc.Form;
// Fill out the form fields using their index
form.Fields[0].Value = "John";
form.Fields[1].Value = "Smith";
form.Fields[2].Value = "+19159969739";
form.Fields[3].Value = "John@email.com";
form.Fields[4].Value = "Chicago";
// Save the modified PDF document
doc.SaveAs(@"D:\myPdfForm.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Load the PDF document from the file path
Dim doc As PdfDocument = PdfDocument.FromFile("D:\myPdfForm.pdf")
' Access the PDF form
Dim form = doc.Form
' Fill out the form fields using their index
form.Fields(0).Value = "John"
form.Fields(1).Value = "Smith"
form.Fields(2).Value = "+19159969739"
form.Fields(3).Value = "John@email.com"
form.Fields(4).Value = "Chicago"
' Save the modified PDF document
doc.SaveAs("D:\myPdfForm.pdf")
End Sub
End Class
上面的範例程式碼將通過索引值填寫表單欄位。 您也可以使用先前提到的欄位名稱進行相同的操作。 讓我們運行程式以查看輸出。
填寫的PDF表單
範例PDF文件中的填寫表單
您可以看到該程式庫可以用最簡單的程式碼填寫PDF表單,而不需要複雜的邏輯。 這就是推薦IronPDF的原因。
假設您還沒有任何包含表單的PDF文件—別擔心,IronPDF提供完整支援,可生成PDF表單。 請遵循以下步驟:
生成新的PDF表單文件
建立新的HTML文件
建立一個新的HTML文件並粘貼以下程式碼:
<!DOCTYPE html>
<html>
<body>
<h2>PDF Forms</h2>
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br>
<label for="contact">Contact #:</label><br>
<input type="text" id="contact" name="contact"><br>
<label for="email">Email:</label><br>
<input type="text" id="email" name="email"><br>
<label for="city">City:</label><br>
<input type="text" id="city" name="city"><br>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>PDF Forms</h2>
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"><br>
<label for="contact">Contact #:</label><br>
<input type="text" id="contact" name="contact"><br>
<label for="email">Email:</label><br>
<input type="text" id="email" name="email"><br>
<label for="city">City:</label><br>
<input type="text" id="city" name="city"><br>
</form>
</body>
</html>
保存此範例HTML文件。您可以根據表單需求自訂此HTML。
接下來,在您的C#程式中寫入以下程式碼。
using IronPdf;
class Program
{
static void Main()
{
// Create an instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render the HTML file as a PDF
var pdfDocument = renderer.RenderHtmlFileAsPdf(@"D:\myForm.html");
// Save the PDF document to the specified file path
pdfDocument.SaveAs(@"D:\myForm.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Create an instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render the HTML file as a PDF
var pdfDocument = renderer.RenderHtmlFileAsPdf(@"D:\myForm.html");
// Save the PDF document to the specified file path
pdfDocument.SaveAs(@"D:\myForm.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Create an instance of ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Render the HTML file as a PDF
Dim pdfDocument = renderer.RenderHtmlFileAsPdf("D:\myForm.html")
' Save the PDF document to the specified file path
pdfDocument.SaveAs("D:\myForm.pdf")
End Sub
End Class
運行程式以查看生成的PDF表單文件。
從HTML文件生成的PDF表單
總結
自動和以程式方式填寫PDF表單是非常重要的。 在本文中,使用IronPDF在C#中填寫PDF表單的最簡單方法建議。 此外,您還學會了如何從頭生成新的PDF表單。
此外,IronPDF還為開發者提供了從PDF中提取文字和內容的方法,在PDF中渲染圖表方法,插入條碼,通過密碼提高安全性以及以程式方式加水印。
還有其他許多有用的程式庫,例如IronBarcode可以處理條碼,IronXL可以處理Excel檔案,而IronOCR可以處理OCR。 您可以通過購買完整的Iron Suite以僅需兩個的價格獲得所有五個程式庫。請存取Iron Software授權頁面了解更多詳情。
常見問題
我如何使用C#以程式方式填寫PDF表單?
IronPDF允許您通過在C#中以程式方式填寫PDF表單,這是通過使用PdfDocument.FromFile載入文件和存取表單字段doc.Form.Fields以設置其值。
設置C#專案以填寫PDF表單涉及哪些步驟?
首先,在Visual Studio中建立一個控制台應用程式。然後,使用套件管理器控制台以Install-Package IronPdf安裝IronPDF。使用PdfDocument.FromFile載入您的PDF並按照需要操控表單字段。
IronPDF是否可以用於從HTML在C#中生成新的PDF表單?
是的,IronPDF可以通過使用ChromePdfRenderer類將HTML表單呈現為PDF文件來生成新的PDF表單。這允許基於網頁表單輸入動態生成PDF。
在.NET應用程式中使用IronPDF處理PDF表單的主要優勢是什麼?
IronPDF提供了一種使用者友好的方式來整合PDF表單處理到.NET應用程式中。它支持表單填寫、文字提取和文件安全,具有易於整合和免費開發的能力。
如何使用IronPDF從PDF表單中提取文字?
IronPDF提供了從PDF表單中提取文字的方法。在用PdfDocument.FromFile載入文件後,您可以使用pdfDocument.ExtractAllText()之類的方法存取並提取文字內容。
是否可以使用.NET庫確保PDF文件的安全性?
是的,IronPDF提供了增強PDF安全性的功能,包括數位簽名、編輯和加密,以保護您的PDF文件中的敏感資訊。
如果我的PDF表單字段未按預期更新,我可以採取哪些故障排除步驟?
確保使用IronPDF正確識別和存取表單字段。確認字段名稱或索引,並使用doc.Form.FindFormField('FieldName').Value = 'New Value'來更新字段值。
如何在C#中填寫表單字段後保存修改過的PDF文件?
使用IronPDF修改表單字段後,使用pdfDocument.SaveAs('path/to/newfile.pdf')保存更新的文件以持久化更改。
IronPDF是否能處理其他文件操作,除了PDF表單處理之外?
是的,IronPDF非常多才多藝,可以處理各種PDF操作,包括文字提取、圖表渲染和文件安全增強,使其成為PDF管理的綜合工具。
IronPDF對於處理PDF表單的開發者有什麼好處?
IronPDF提供了一個簡單的API來填寫和操控PDF表單,通過提供如HTML到PDF渲染和.NET應用程式整合等功能,提高了開發者的生產力。
IronPDF是否支持.NET 10,這對於使用IronPDF在C#中填寫PDF表單意味著什麼?
是的,IronPDF支持現代.NET版本,包括.NET 8和.NET 9,並已符合即將於2025年11月發佈的.NET 10版本。這意味著您可以繼續使用IronPDF進行表單填寫和PDF操作,在.NET 10中具備完全相容性。




