ITextSharp - text field in PdfPCell

I use iTextSharp to create a PDF file, how to add a text filter to PdfPCell?

+7
pdf itextsharp textfield
source share
3 answers

You really would not add a β€œtext box” to PdfPCell, you would create a PdfPCell and add text (or other material) to it.

mikesdotnetting.com may have the most striking example , and there is always an iTextSharp tutorial .

+7
source share

Give it a try. This works for me.

Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f); MemoryStream ms = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, ms); doc.Open(); // Create your PDFPTable here.... TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox"); PdfPCell tbCell = new PdfPCell(); iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField()); tbCell.CellEvent = events; myTable.AddCell(tbCell); // More code... 

I adapted this code from this post.

Edit:

Here is a complete working console application that places a TextBox in a table cell. I tried to keep the code to a minimum.

 using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace iTextSharpTextBoxInTableCell { class Program { static void Main(string[] args) { // Create a PDF with a TextBox in a table cell BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false); Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, Color.BLACK); Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f); FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create); PdfWriter writer = PdfWriter.GetInstance(doc, fs); doc.Open(); PdfPTable myTable = new PdfPTable(1); myTable.TotalWidth = 568f; myTable.LockedWidth = true; myTable.HorizontalAlignment = 0; TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox"); PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12)); iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField()); tbCell.CellEvent = events; myTable.AddCell(tbCell); doc.Add(myTable); doc.Close(); fs.Close(); Console.WriteLine("End Of Program Execution"); Console.ReadLine(); } } } 

Random chance

+7
source share

DaveB's answer works, but the problem is that you need to know the coordinates to place the text box, (67, 585, 140, 800). A more common way to do this is to create a table cell and add a custom event to the cell. When table generation raises the celllayout event, it passes the cell sizes and coordinates to it, which you can use to place and size the text field.

First create this call, which is a custom event

 public class CustomCellLayout : IPdfPCellEvent { private string fieldname; public CustomCellLayout(string name) { fieldname = name; } public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; // rectangle holds the dimensions and coordinates of the cell that was created // which you can then use to place the textfield in the correct location // and optionally fit the textfield to the size of the cell float textboxheight = 12f; // modify the rectangle so the textfield isn't the full height of the cell // in case the cell ends up being tall due to the table layout Rectangle rect = rectangle; rect.Bottom = rect.Top - textboxheight; TextField text = new TextField(writer, rect, fieldname); // set and options, font etc here PdfFormField field = text.GetTextField(); writer.AddAnnotation(field); } } 

Then, in your code where you create the table, you will use this event as follows:

 PdfPCell cell = new PdfPCell() { CellEvent = new CustomCellLayout(fieldname) // set borders, or other cell options here }; 

If you want to use different types of text fields, you can either create additional custom events or add additional properties to the CustomCellLayout class, for example, "fontsize" or "multiline", which you would set using the class constructor, and then check in the CellLayout code to set text box properties.

+2
source share

All Articles