This article mainly introduces the knowledge related to C# creating and filling fields in PDF. It has a very good reference value. Let’s take a look at it with the editor.
As we all know, PDF documents usually cannot be edited or modified. If the user needs to sign or fill in other content in the PDF document, the PDF document needs to have editable fields. Developers often encounter the need to populate data into PDF template fields in a programming way. At this time, you need to solve the following two questions:
How to create an editable field in PDF?
How to accurately fill in the content into these fields?
Here I will introduce how to use C# and Free Spire.PDF components to achieve this function.
Free Spire.PDFComponent Overview
Free Spire.PDF is a free professional PDF component for use in .NET applications Create, edit, process and read PDF documents. Supports rich PDF document processing operations, such as PDF document merging/splitting, conversion (such as HTML to PDF, PDF to images, etc.), printing (including silent printing), compression, adding comments, security settings (including digital signatures), creation and fill fields, image insertion and extraction, text extraction and highlighting, etc. Does not depend on Adobe Acrobat and supports Chinese.
Regarding installation, there are many channels, including the official website and NuGet, the most favorite and commonly used method by developers. Enter the following PowerShell command in the NuGet Package Manager Console of Visual Studio and press Enter, the component dll will be automatically referenced into the project:
PM> Install-Package FreeSpire.PDF
Implementation of creating and filling fields
1. Create a domain
This component provides many corresponding classes through which we can create a variety of PDF domains. Because there are many types, I only list some common fields and the class names corresponding to the fields in the components.
Domain name |
Class name |
||||||||||||||||||||||||||||||||
TextField |
PdfTextBoxField |
||||||||||||||||||||||||||||||||
##SignatureField | PdfSignatureField | ||||||||||||||||||||||||||||||||
##PdfCheckBoxField | |||||||||||||||||||||||||||||||||
PdfComboBoxField | |||||||||||||||||||||||||||||||||
PdfListBoxField | |||||||||||||||||||||||||||||||||
Button##PdfRadioButtonListField(radio button) | PdfButtonField (normal button) |
Description | Example | Javascript | Method |
##Date | ##01 /31/2008AFDate_FormatEx("mm/dd/yyyy"); |
AFDate_KeystrokeEx("mm/dd/yyyy"); |
GetDateKeystrokeString("mm/dd/yyyy");|
##1/31/2008 | AFDate_FormatEx("m/d/yyyy"); AFDate_KeystrokeEx("m/d/yyyy"); |
| |
12345 | AFSpecial_Format(0); AFSpecial_Keystroke(0); | GetSpecialFormatString(0); | |
AFSpecial_Format(1); | AFSpecial_Keystroke(1); | GetSpecialFormatString(1);
GetSpecialKeystrokeString(1); |
|
AFSpecial_Format(2); | AFSpecial_Keystroke(2); | GetSpecialFormatString(2);
GetSpecialKeystrokeString(2); |
|
-$12,345.00 | AFNumber_Format(2, 0, 0, 0, "$", true);
AFNumber_Keystroke(2, 0, 0, 0, "$", true); | GetNumberFormatString(2, 0, 0, 0, "$", true);
GetNumberKeystrokeString(2, 0, 0, 0, "$", true); |
|
AFRange_Validate(true,1,true,10) | ##GetRangeValidateString(true, 1, true, 10); | Example: |
##1.2 Signature field
Creating a signature field is similar to a text field. You can also set the border, size, position and other attributes of the field
. I won’t go into details here.//创建签名域并指定域名
PdfSignatureField signaturefield = new PdfSignatureField(page, "Signature");
//设置域的边框
signaturefield.BorderWidth = 1.0f;
signaturefield.BorderStyle = PdfBorderStyle.Solid;
signaturefield.BorderColor = new PdfRGBColor(System.Drawing.Color.Black);
//设置高亮模式
signaturefield.HighlightMode = PdfHighlightMode.Outline;
//设置大小与位置
signaturefield.Bounds = new RectangleF(40, 150, 200, 100);
//将签名域添加到页面
pdf.Form.Fields.Add(signaturefield);
When filling the field, you need to first obtain all the fields in the document fields, and then fill in the specified fields one by one. If there are many domains of the same type, you can quickly fill in the domain names.
//加载PDF文档 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Fields.pdf"); //获取第一页 PdfPageBase page = pdf.Pages[0]; //获取文档的所有域 PdfFormWidget form = pdf.Form as PdfFormWidget; //填充第一个文本域 PdfTextBoxFieldWidget textboxField = form.FieldsWidget[0] as PdfTextBoxFieldWidget; textboxField.Text = "25"; //填充第二个签名域 PdfSignatureFieldWidget signatureField = form.FieldsWidget[1] as PdfSignatureFieldWidget; String pfxPath = @"gary.pfx"; PdfCertificate digi = new PdfCertificate(pfxPath, "123456"); PdfSignature signature = new PdfSignature(pdf, page, digi, "demo", signatureField); signature.IsTag = true; signature.DigitalSigner = "Gary"; signature.ConfigGraphicType = ConfiguerGraphicType.TextSignInformation; //保存文档 pdf.SaveToFile("Fill.pdf");
The above is the detailed content of Detailed introduction to creating and filling fields in PDF using C# (picture and text). For more information, please follow other related articles on the PHP Chinese website!