C # Detailed introduction to Creating and Populating fields in PDF (graphic)

Source: Internet
Author: User
This article focuses on the knowledge of C # in creating and populating a domain in PDF. Have a good reference value, follow the small series together to see it





As we all know, PDF documents are often not editable and modified. If you need to sign a PDF document or fill in other content, you'll need an editable field in the PDF document. Developers also often encounter the need to populate the PDF template domain programmatically with data. The following two questions need to be addressed:


    1. How do I create an editable field in a PDF?

    2. How do I fill in these fields exactly with the content?


Here I'll show you how to do this with the C # and free spire.pdf components.



Free Spire.pdf Components Overview



Free Spire.pdf is a complimentary professional PDF component for creating, editing, processing, and reading PDF documents in. NET Applications. Supports rich PDF document processing operations such as PDF document merging/splitting, conversion (such as HTML to pdf,pdf to image, etc.), printing (including silent printing), compression, adding annotations, security settings (including digital signatures), creating and populating fields, image insertion and extraction, text extraction and highlighting, and more. does not rely on Adobe Acrobat, and supports Chinese.



There are many channels for installation, including the official website and the NuGet methods that developers like and use most often. Enter the following PowerShell command in the NuGet package Manager console in Visual Studio, and the component's DLLs are automatically referenced to the project:



Pm> Install-package Freespire.pdf



Create and populate the implementation of a domain



1. Create a domain



This component provides a number of corresponding classes through which we can create multiple PDF domains. Because there are a lot of kinds, I'll just list some common fields and the class names that the domain corresponds to in the component.


Domain name

Class name

Text field

Pdftextboxfield

Signature fields

Pdfsignaturefield

check box

Pdfcheckboxfield

Combo box

Pdfcomboboxfield

list box

Pdflistboxfield

Button

Pdfradiobuttonlistfield (radio button)

Pdfbuttonfield (normal button)


Here I choose two of the most common editable fields in a PDF document: Text fields and Signature fields.



1.1 Text fields



First, I created a simple text field. You need to specify the name of the domain when you create it, and the advantage is that if there are multiple text fields in the document, when we programmatically fill in the fields, you can quickly and accurately fill in the specified fields based on the name of the domain. Note that the domain name is not duplicated, otherwise the content will be filled in all domains corresponding to that domain name.


// Create PDF document
PdfDocument pdf = new PdfDocument ();
// Add a new page
PdfPageBase page = pdf.Pages.Add (PdfPageSize.A4, new PdfMargins ());
// Add text to the page
PdfTrueTypeFont font = new PdfTrueTypeFont (new Font ("Arial Unicode MS", 10f), true);
page.Canvas.DrawString ("Age:", font, PdfBrushes.DeepSkyBlue, 10, 50);
// Create a text field and specify the name of the text field
PdfTextBoxField textbox = new PdfTextBoxField (page, "Age");
// Set the size, position, and font of the text field
textbox.Bounds = new RectangleF (40, 50, 50, 12);
textbox.Font = font;
// Add text field to the document
pdf.Form.Fields.Add (textbox);
// Save the document
pdf.SaveToFile ("Fields.pdf");





Of course, most of the time our needs may be more than just creating a simple text field and other settings such as setting the border, background color, font color, and font arrangement. Even the input of the specified text field, such as only dates or numbers within a range.



Set Format:


// Set the border
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;
textbox.BorderColor = Color.Black;
// Set the background color
textbox.BackColor = Color.Yellow;
// Set font color
textbox.ForeColor = Color.Red;
// Set the font arrangement
textbox.TextAlignment = PdfTextAlignment.Center;





Specify the input for the text field:



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


Describe

Example

Javascript

Method

Date

01/31/2008

Afdate_formatex ("mm/dd/yyyy");
Afdate_keystrokeex ("mm/dd/yyyy");

Getdateformatstring ("mm/dd/yyyy");
Getdatekeystrokestring ("mm/dd/yyyy");

Date

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

12345-1234

Afspecial_format (1);
Afspecial_keystroke (1);

Getspecialformatstring (1);
Getspecialkeystrokestring (1);

Phone number

(123) 456-7890

Afspecial_format (2);
Afspecial_keystroke (2);

Getspecialformatstring (2);
Getspecialkeystrokestring (2);

Money

$12,345.00
-$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

1≤input value≤10

Afrange_validate (true,1,true,10)

Getrangevalidatestring (True, 1, true, 10);


Example:





// Specify the input data between 1-100
string js = PdfJavascript.GetRangeValidateString (true, 1, true, 100);
PdfJavascriptAction jsAction = new PdfJavascriptAction (js);
textbox.Actions.Validate = jsAction;





1.2 Signature Domain



Creating a signature field is similar to a text field, and you can also set the border, size, location, and so on for a field. We will not repeat it here.





// Create a signature domain and specify the domain name
PdfSignatureField signaturefield = new PdfSignatureField (page, "Signature");
// Set the border of the domain
signaturefield.BorderWidth = 1.0f;
signaturefield.BorderStyle = PdfBorderStyle.Solid;
signaturefield.BorderColor = new PdfRGBColor (System.Drawing.Color.Black);
// Set highlight mode
signaturefield.HighlightMode = PdfHighlightMode.Outline;
// Set size and position
signaturefield.Bounds = new RectangleF (40, 150, 200, 100);
// Add the signature field to the page
pdf.Form.Fields.Add (signaturefield);





2. Populating the domain



When you populate a domain, you need to get all the fields in the document before populating the specified fields individually. If the same type of domain is more, you can quickly populate it with the name of the domain.





// Load PDF document
PdfDocument pdf = new PdfDocument ();
pdf.LoadFromFile ("Fields.pdf");
// Get the first page
PdfPageBase page = pdf.Pages [0];
// Get all fields of the document
PdfFormWidget form = pdf.Form as PdfFormWidget;
// Fill the first text field
PdfTextBoxFieldWidget textboxField = form.FieldsWidget [0] as PdfTextBoxFieldWidget;
textboxField.Text = "25";
// Fill the second signature field
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;
// Save the document
pdf.SaveToFile ("Fill.pdf"); 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.