Programmatically Insert Editable Textbox in PDF

Textbox is one type of form fields in PDF, which allows users to enter data into PDF without any editing software. It is commonly used in a document to obtain information of the readers. This article presents how to insert editable textbox in a PDF programmatically using free PDF API in C#.
This PDF library provides a class of PdfTextBoxField, initialize a new object of the class and set it properties likeBoundsBounderWidth, then you can add the textbox to PDF document by Form.Fields.Add() method.
Using the code
//Create a new object of PdfDoument, insert a page
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());
//Set font, brush
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
PdfBrush brush = PdfBrushes.Black;
float x = 50;
float y = 50;
float tempX = 0;
//Draw sting in Pdf
string text = "Phone Number: ";
page.Canvas.DrawString(text, font, brush, x, y);
//Insert textbox
tempX = font.MeasureString(text).Width + x + 15;
PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");
textbox.Bounds = new RectangleF(tempX, y, 100, 15);
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;doc.Form.Fields.Add(textbox);
//Add a tooltip to textbox
doc.Form.Fields["TextBox"].ToolTip = "Please insert a valid phone Number";
doc.SaveToFile("sample.pdf", FileFormat.PDF); 
Output


2016-04-06_163155

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender