How to set appearance to the PDF form fields using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can set appearance to the PDF form fields using C# and VB.NET.
After filling the form fields in the PDF document, it may appear empty due to the absence of the appearance dictionary. By setting false to SetDefaultAppearance method in PdfForm class, you can create the appearance dictionary. By this, the text will be visible in all PDF Viewers.
Steps to set appearance to the PDF form fields programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.Mvc NuGet package as a reference to your .NET Framework applications from NuGet.org.
- A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- A default action method named Index will be present in HomeController.cs. Right-click this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
- Add new button in Index.cshtml as follows.
<h2>Click the button to generate PDF</h2> @using (Html.BeginForm("CreateDocument ", "Home", FormMethod.Post)) { <input type="submit" value="GeneratePDF" /> }
- Add a new action method named CreateDocument in HomeController.cs and include the following code snippet to set appearance to the PDF form fields.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Server.MapPath("Form.pdf")); //Get the loaded form PdfLoadedForm loadedForm = loadedDocument.Form; //Set the default appearance loadedForm.SetDefaultAppearance(false); //Get the loaded text box field and fill it PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[0] as PdfLoadedTextBoxField; loadedTextBoxField.Text = "1"; //Open the document in browser after saving it loadedDocument.Save("Sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save); //Close the document loadedDocument.Close(true);
VB.NET
'Load the PDF document Dim loadedDocument As New PdfLoadedDocument(Server.MapPath("Form.pdf")) 'Get the loaded form Dim loadedForm As PdfLoadedForm = loadedDocument.Form 'Set the default appearance loadedForm.SetDefaultAppearance(False) 'Get the loaded text box field and fill it Dim loadedTextBoxField As PdfLoadedTextBoxField = TryCast(loadedForm.Fields(0), PdfLoadedTextBoxField) loadedTextBoxField.Text = "1" 'Open the document in browser after saving it loadedDocument.Save("Sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save) 'Close the document loadedDocument.Close(True)
A complete working sample can be downloaded from FormFieldsAppearanceSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find options like form fields to PDF, modifying existing form fields, filling form fields, removing editing capability, removing form fields, importing FDF file to PDF, export PDF file to FDF, adding actions to form fields, and features like XFA Form and annotation with code examples.
An online link to ASP.NET MVC sample browser.
Refer here to explore PDF forms in Syncfusion Essential PDF.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.