How to add a hidden text field in a PDF file 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 add a hidden text box field in a PDF using C# and VB.NET.
Steps to add a hidden textbox field in PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports System.Drawing
- Use the following code snippet to add a hidden textbox field in a PDF file.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Add a new page to the PDF document PdfPage page = document.Pages.Add(); //Draw string page.Graphics.DrawString("Textbox field is hidden in the location (10,40)", new PdfStandardFont(PdfFontFamily.Helvetica,10), PdfBrushes.Black, new PointF(10, 20)); //Create a textbox field and add the properties PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name"); textBoxField.Bounds = new RectangleF(10, 40, 200, 20); textBoxField.ToolTip = "Name"; //Set the visibility of textbox field to hidden textBoxField.Visibility = PdfFormFieldVisibility.Hidden; //Add the hidden textbox field to the document document.Form.Fields.Add(textBoxField); //Save the document document.Save("HiddenTextboxField.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("HiddenTextboxField.pdf");
VB.NET
'Create a new PDF document Dim document As New PdfDocument() 'Add a new page to the PDF document Dim page As PdfPage = document.Pages.Add() 'Draw string page.Graphics.DrawString("Textbox field is hidden in the location (10,40)", New PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.Black, New PointF(10, 20)) 'Create a text box field and add the properties Dim textBoxField As New PdfTextBoxField(page, "Name") textBoxField.Bounds = New RectangleF(10, 40, 200, 20) textBoxField.ToolTip = "Name" 'Set the visibility of text box field to hidden textBoxField.Visibility = PdfFormFieldVisibility.Hidden 'Add the hidden text box field to the document document.Form.Fields.Add(textBoxField) 'Save the document document.Save("HiddenTextboxField.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("HiddenTextboxField.pdf")
A complete working sample can be downloaded from PDFFormSample.zip.
Take a moment to peruse the documentation, where you can find other options like add 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.
Refer here to explore the rich set of Syncfusion Essential PDF features.
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.