Fill a PDF form using C# console application
We will be using the SyncFusion Package to fill out the PDF Form Fields. To add SyncFusion to your project follow these steps :
1. Go to Tool -> NuGet Package Manager -> Manage NuGet Packages For Solution
2. Then search Syncfusion.Pdf.WinForms and install it.
Steps to be followed to fill the PDF form fields :
1) Save the PDF File in the project
2) Include the namespaces in the Program.cs
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Drawing;
3) Load the PDF Document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(“PdfName.pdf”);
4) Get the first page of the document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
5) Get the loaded form
PdfLoadedForm form = loadedDocument.Form;
6) Fill the fields from the first page
form.Fields["Name"] as PdfLoadedTextBoxField).Text = "Raman";
7) Save the document
loadedDocument.Save(“PdfName.pdf”);
8) Close the document
loadedDocument.Close(true);
9) Open the PDF file so that the result will be seen in default PDF viewer
Process.Start("Form.pdf");
Complete Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Drawing; namespace ConsoleApp { class Program { static void Main(string[] args) { PdfLoadedDocument loadedDocument = new PdfLoadedDocument("FormTemplate.pdf"); PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; PdfLoadedForm form = loadedDocument.Form; (form.Fields["Name"] as PdfLoadedTextBoxField).Text = "Raman"; (form.Fields["Email address"] as PdfLoadedTextBoxField).Text = "raman@123.com"; loadedDocument.Save("Form.pdf"); loadedDocument.Close(true); Process.Start("Form.pdf"); } } }