跳至页脚内容
使用IRONPDF
如何使用IronPDF编程化填充PDF

以编程方式在C#中填写PDF表单(编码教程)

role="alert">您的企业在 PDF 安全性和合规性的年度订阅上花费过多。考虑使用 IronSecureDoc,它提供用于管理 SaaS 服务(如数字签名、编辑、加密和保护)的解决方案,全部以一次性付款方式提供。探索 IronSecureDoc 文档

本教程将演示如何以程序化方式与 PDF 文件中的表单进行交互。

市场上有多个 .NET 库允许我们以 C# 填写 PDF 表单。 其中一些难以理解,还有一些需要付费。

IronPDF 是最好的 .NET Core 库,因为它易于理解且开发免费。 除了填写 PDF 表单,IronPDF 还允许从 HTML 字符串、HTML 文件和 URL 创建新的 PDF。

让我们看看如何使用 C# 程序化地填写 PDF 表单。 首先,将创建一个控制台应用程序进行演示,但您可以根据需要使用任何应用。

创建 Visual Studio 项目

打开 Microsoft Visual Studio。 点击创建新项目 > 从模板中选择控制台应用程序 > 按下一步 > 为您的项目命名。 按下一步 > 选择目标框架。 点击 创建 按钮。 项目创建如下所示。

在 C# 中以编程方式填写 PDF 表单(编码教程),图 1: 在 Visual Studio 中新创建的控制台应用程序 在 Visual Studio 中新创建的控制台应用程序

安装IronPDF库

如前所述,本教程中将使用 IronPDF 库。 使用这个 .NET 库的主要原因是它对开发免费,并在单个库中提供了所有功能。

转到包管理器控制台。 输入以下命令:

Install-Package IronPdf

此命令将为我们安装 IronPDF 库。 接下来,让我们开始编码。

读取 PDF 文档

填写 PDF 表单的第一步是读取 PDF 文档。 显然,我们怎么能不先阅读就填写表单呢?以下 PDF 文档将用于演示。 您可以从 Google 驱动器链接 下载,或者使用您的文档。

在 C# 中以编程方式填写 PDF 表单(编码教程),图 2: 填写表单的示例 PDF 文件 填写表单的示例 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")
$vbLabelText   $csharpLabel

FromFile 方法内部传递 PDF 文档的完整路径。 这将从您的本地系统中读取 PDF 文件。

获取 PDF 表单

编写以下代码行以从加载的 PDF 文档中获取表单。

var form = doc.Form;
var form = doc.Form;
Dim form = doc.Form
$vbLabelText   $csharpLabel

获取表单字段

要设置其值的表单字段,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")
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

填写 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
$vbLabelText   $csharpLabel

上面的示例代码将按索引值填写表单字段。 您也可以使用前面提到的字段名称来做相同的事情。 让我们运行程序以查看输出。

填写的 PDF 表单

在 C# 中以编程方式填写 PDF 表单(编码教程),图 3: 示例 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 文件。您可以根据表单要求自定义此 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
$vbLabelText   $csharpLabel

运行程序以查看生成的 PDF 表单文档。

在 C# 中以编程方式填写 PDF 表单(编码教程),图 3:从 HTML 文件生成的 PDF 表单 从 HTML 文件生成的 PDF 表单

摘要

自动和编程方式填写 PDF 表单是很重要的。 在本文中,介绍了在 C# 中使用 IronPDF 填写 PDF 表单的最简单方法。 此外,您还了解了如何从头开始生成新的 PDF 表单

Additionally, IronPDF also offers developers methods to extract text and content from a PDF, render charts in PDFs, insert barcodes, enhance security with passwords and watermark programmatically.

还有其他许多有用的库,例如用于处理条形码的 IronBarcode,用于处理 Excel 文档的 IronXL,以及用于 OCR 的 IronOCR。 您可以通过购买完整的 Iron Suite,以仅两个的价格获得所有五个库。请访问 Iron Software 许可页面 以了解更多详细信息。

常见问题解答

我如何使用 C# 以编程方式填充 PDF 表单?

IronPDF 允许您通过加载文档与 PdfDocument.FromFile 和使用 doc.Form.Fields 访问表单字段来以编程方式在 C# 中填充 PDF 表单。

设置 C# 项目以填充 PDF 表单涉及哪些步骤?

首先,在 Visual Studio 中创建一个控制台应用程序。然后,使用包管理器控制台安装 IronPDF,命令为 Install-Package IronPdf。使用 PdfDocument.FromFile 加载您的 PDF,并根据需要操作表单字段。

IronPDF 可以用于从 HTML 生成新的 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 8 和 .NET 9 在内的现代 .NET 版本,并且已经兼容即将发布的 .NET 10(计划于 2025 年 11 月发布)。这意味着您可以继续使用 IronPDF 在 C# 中进行表单填写和 PDF 操作,并且与 .NET 10 完全兼容。

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。