Home  >  Article  >  Backend Development  >  Detailed introduction to creating and filling fields in PDF using C# (picture and text)

Detailed introduction to creating and filling fields in PDF using C# (picture and text)

黄舟
黄舟Original
2017-03-31 15:05:531858browse

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:

  1. How to create an editable field in PDF?

  2. 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.

##PdfCheckBoxFieldComboBoxPdfComboBoxFieldList BoxPdfListBoxField

Here I select the two most common editable fields in PDF documents for introduction: text fields and signature fields.

1.1 Text Field

First, I created a simple text field. You need to specify the name of the field when creating. The advantage of this is that if there are multiple text fields in the document at the same time, when we fill in the fields programmatically, we can quickly and accurately fill in the content to the specified field according to the name of the field. in the domain. It should be noted that the domain name should not be repeated, otherwise the content will be filled in all fields corresponding to the domain name.

//创建PDF文档
PdfDocument pdf = new PdfDocument();
//添加一个新页面
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins());
//添加文本到页面
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);
page.Canvas.DrawString("年龄:", font, PdfBrushes.DeepSkyBlue, 10, 50);
//创建文本域并指定文本域的名称
PdfTextBoxField textbox = new PdfTextBoxField(page, "Age");  
//设置文本域的大小、位置、字体
textbox.Bounds = new RectangleF(40, 50, 50, 12);  
textbox.Font = font;
//添加文本域到文档
pdf.Form.Fields.Add(textbox);
//保存文档
pdf.SaveToFile("Fields.pdf");

Detailed introduction to creating and filling fields in PDF using C# (picture and text)

Of course, most of the time our needs may not only be to create a simple text field, but also need to make some other settings, such as setting borders, background colors, font colors, fonts Arrangement. Even the input content of a specified text field, such as only dates or numbers within a certain range, can be entered.

Set format:

//设置边框
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;
textbox.BorderColor = Color.Black;
//设置背景色
textbox.BackColor = Color.Yellow;
//设置字体颜色
textbox.ForeColor = Color.Red;
//设置字体排列方式 
textbox.TextAlignment = PdfTextAlignment.Center;

Detailed introduction to creating and filling fields in PDF using C# (picture and text)

Specify the input content of the text field:

Adobe Acrobat supports developers to use Javascript to pre-define the format, type, etc. of text field input content. This component also supports this type of script and provides corresponding methods to implement these functions. The following table lists some Javascript and methods:

Domain name

Class name

TextField

PdfTextBoxField

##SignatureField

PdfSignatureField

CheckBox

Button

##PdfRadioButtonListField(radio button)
PdfButtonField (normal button)

##01 /31/2008AFDate_FormatEx("mm/dd/yyyy"); GetDateKeystrokeString("mm/dd/yyyy");Date##12345-1234AFSpecial_Format(1);(123) 456-7890AFSpecial_Format(2);$12,345.00##1≤ input value≤10
//指定输入数据在1-100之间
string js = PdfJavascript.GetRangeValidateString(true, 1, true, 100);
PdfJavascriptAction jsAction = new PdfJavascriptAction(js);
textbox.Actions.Validate = jsAction;

Description

Example

Javascript

Method

##Date

AFDate_KeystrokeEx("mm/dd/yyyy");


##GetDateFormatString("mm/dd/yyyy");


##1/31/2008

AFDate_FormatEx("m/d/yyyy");

AFDate_KeystrokeEx("m/d/yyyy");


GetDateFormatString("m/d/yyyy");

GetDateKeystrokeString("m/d/yyyy");


##Zip code
12345

AFSpecial_Format(0);

AFSpecial_Keystroke(0);

GetSpecialFormatString(0);
GetSpecialKeystrokeString(0);


Zip+4

AFSpecial_Keystroke(1);

GetSpecialFormatString(1);

GetSpecialKeystrokeString(1);


Phone number

AFSpecial_Keystroke(2);

GetSpecialFormatString(2);

GetSpecialKeystrokeString(2);


Money

-$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);


Validate

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 Detailed introduction to creating and filling fields in PDF using C# (picture and text) 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);

2. Fill in the field


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.

Detailed introduction to creating and filling fields in PDF using C# (picture and text)

//加载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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn