
.NET PDF 生成器一键生成
您的企业在 PDF 安全性和合规性方面的年度订阅费用过高。请考虑IronSecureDoc,它为管理数字签名、节录、加密和保护等 SaaS 服务提供解决方案,只需一次性支付。探索 IronSecureDoc 文档。
本教程将演示如何以程序化方式与 PDF 文件中的表单进行交互。
市场上有多个 .NET 库允许我们以 C# 填写 PDF 表单。 其中一些难以理解,还有一些需要付费。
IronPDF 是最好的 .NET Core 库,因为它易于理解且开发免费。 除了填写 PDF 表单,IronPDF 还允许从 HTML 字符串、HTML 文件和 URL 创建新的 PDF。
让我们看看如何使用 C# 程序化地填写 PDF 表单。 首先,将创建一个控制台应用程序进行演示,但您可以根据需要使用任何应用。
创建Visual Studio项目
打开Microsoft Visual Studio。 点击创建新项目 > 从模板中选择控制台应用程序 > 按下一步 > 为您的项目命名。 按下一步 > 选择目标框架。 点击创建按钮。 项目创建如下所示。
在 Visual Studio 中新创建的控制台应用程序
安装IronPDF库
如前所述,本教程中将使用 IronPDF 库。 使用这个 .NET 库的主要原因是它对开发免费,并在单个库中提供了所有功能。
转到包管理器控制台。 输入以下命令:
此命令将为我们安装 IronPDF 库。 接下来,让我们开始编码。
读取 PDF 文档
填写 PDF 表单的第一步是读取 PDF 文档。 显然,我们怎么能不先阅读就填写表单呢?以下 PDF 文档将用于演示。 您可以从 Google 驱动器链接 下载,或者使用您的文档。
填写表单的示例 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")将PDF文档的完整路径传入FromFile方法。 这将从您的本地系统中读取 PDF 文件。
获取 PDF 表单
编写以下代码行以从加载的 PDF 文档中获取表单。
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
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
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");
}
}Imports IronPdf
Module Program
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 Module上面的示例代码将按索引值填写表单字段。 您也可以使用前面提到的字段名称来做相同的事情。 让我们运行程序以查看输出。
填写的 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>
保存此示例 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");
}
}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 表单是很重要的。 在本文中,介绍了在 C# 中使用 IronPDF 填写 PDF 表单的最简单方法。 此外,您还了解了如何从头开始生成新的 PDF 表单。
此外,IronPDF 还为开发者提供了从 PDF 中提取文本和内容的方法,在 PDF 中呈现图表,插入条形码,通过密码增强安全性和添加水印。
还有其他许多有用的库,例如用于处理条形码的 IronBarcode,用于处理 Excel 文档的 IronXL,以及用于 OCR 的 IronOCR。 您可以通过购买完整的 Iron Suite,以仅两个的价格获得所有五个库。请访问 Iron Software 许可页面 以了解更多详细信息。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


