在C#开发中,操作PDF文件是常见的需求,其中创建和填充PDF域是表单类场景的核心操作。下面我们将基于iText7库一步步实现相关功能。

一、准备工作
首先需要安装iText7的相关NuGet包,在Visual Studio的包管理控制台中执行以下命令:
// 安装iText7核心包和PDF操作扩展包 Install-Package itext7 Install-Package itext7.licensekey
安装完成后,需要在代码头部引入对应的命名空间:
using iText.Kernel.Pdf; using iText.Forms; using iText.Forms.Fields; using iText.Kernel.Pdf.Annot; using iText.Layout; using iText.Layout.Element; using System.IO;
二、创建新的PDF并添加域
我们可以通过代码创建一个新的PDF文件,并在其中添加不同类型的表单域,比如文本域、下拉框域等。
2.1 创建文本域
文本域是最常用的PDF域类型,用于用户输入文本内容,以下是创建文本域的示例代码:
public void CreatePdfWithTextField()
{
// 定义生成的PDF文件路径
string outputPath = "created_pdf_with_field.pdf";
// 初始化PDF写入器
PdfWriter writer = new PdfWriter(outputPath);
// 初始化PDF文档
PdfDocument pdfDoc = new PdfDocument(writer);
// 创建文档布局对象
Document document = new Document(pdfDoc);
// 获取PDF的表单对象,如果不存在则创建
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
// 创建文本域,参数分别是域名称、文档对象、域位置(左下角x,y,右上角x,y)
PdfTextFormField textField = PdfTextFormField.CreateText(
pdfDoc,
new iText.Kernel.Geom.Rectangle(50, 700, 200, 30),
"user_name", // 域名称,后续填充时需要通过这个名称定位
"" // 域默认值
);
// 设置域的提示文本
textField.SetDefaultValue("请输入用户名");
// 将文本域添加到表单中
form.AddField(textField);
// 添加说明文字
document.Add(new Paragraph("用户名称:"));
// 关闭文档,释放资源
document.Close();
}2.2 创建下拉框域
下拉框域可以让用户从预设的选项中选择内容,创建方式如下:
public void CreatePdfWithComboBoxField()
{
string outputPath = "created_pdf_with_combobox.pdf";
PdfWriter writer = new PdfWriter(outputPath);
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
// 创建下拉框域
string[] options = new string[] { "选项一", "选项二", "选项三" };
PdfChoiceFormField comboBoxField = PdfChoiceFormField.CreateComboBox(
pdfDoc,
new iText.Kernel.Geom.Rectangle(50, 650, 200, 30),
"select_option",
options,
0 // 默认选中的选项索引,0表示第一个
);
form.AddField(comboBoxField);
document.Add(new Paragraph("选择对应选项:"));
document.Close();
}三、填充已有的PDF域
如果已经有一个包含表单域的PDF文件,我们可以通过域名称定位到对应的域,然后填充内容。
public void FillPdfFields()
{
// 原始包含域的PDF路径
string sourcePath = "created_pdf_with_field.pdf";
// 填充后生成的PDF路径
string targetPath = "filled_pdf.pdf";
PdfReader reader = new PdfReader(sourcePath);
PdfWriter writer = new PdfWriter(targetPath);
// 以追加模式打开PDF,避免覆盖原有内容
PdfDocument pdfDoc = new PdfDocument(reader, writer);
// 获取PDF中的表单对象
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false);
if (form != null)
{
// 获取所有域的映射
IDictionary<string, PdfFormField> fields = form.GetFormFields();
// 填充名称为user_name的文本域
if (fields.ContainsKey("user_name"))
{
fields["user_name"].SetValue("张三");
}
// 如果表单有下拉框域,也可以同样方式填充
if (fields.ContainsKey("select_option"))
{
fields["select_option"].SetValue("选项二");
}
}
pdfDoc.Close();
}四、常见问题说明
- 如果填充后域内容不显示,需要检查域名称是否和PDF中的域名称完全一致,区分大小写
- 创建域时需要确保位置坐标在PDF页面范围内,否则域可能无法正常显示
- 操作完成后一定要关闭PdfDocument和Document对象,避免文件被占用
- 如果需要让填充后的域不可编辑,可以调用
field.SetReadOnly(true)方法设置只读属性
五、总结
通过iText7库,我们可以很方便地在C#中实现PDF域的创建和填充操作,不管是生成新的带表单的PDF,还是处理已有的PDF表单,都可以通过上述方法快速实现。实际开发中可以根据需求扩展更多类型的域,比如复选框域、单选框域等,核心逻辑和上述示例类似,只需要调用对应的创建方法即可。