I'm using iTextSharp to create a PDF, how can I add a textField into PdfPCell?
3 Answers
You wouldn't really add a 'text field' to a PdfPCell, you'd create a PdfPCell and add text (or other stuff) to that.
mikesdotnetting.com might have the clearest example and there's always the iTextSharp tutorial.

- 53,046
- 9
- 139
- 151
-
I Know this link, but are you sure that i can't add a TextField into PdfPCell? I am looking at this link http://old.nabble.com/PdfPTable-TextField-in-PdfPCell:-questions-td17984107.html, but I don't understand whether to do. I need to add a TextField into PdfPCell, because after pdf creation i need to insert a information into these field. – gigiot Apr 20 '10 at 15:44
Give this a try. It 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 full working console application that puts a TextBox in a table cell. I tried to keep the code to a bare 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();
}
}
}
Bon chance

- 9,470
- 4
- 39
- 66
-
Nothing ... after of this istruction myTable.AddCell(tbCell); i check with debug my object tbCell, but i see that the size of rectangle are 0x0.. is normal? – gigiot Apr 20 '10 at 16:22
-
Thanks Dave, you seaved me!!! i tried your code and works perfectly. Now i'm trying to merge 3 file pdf, but after merge all TextField are disappear .. there is a property of the document that i must plan for avoiding this? – gigiot Apr 21 '10 at 08:13
DaveB's answer works, but the problem is that you have to know the coordinates to place the textfield into, the (67, 585, 140, 800). The more normal method of doing this is to create the table cell and add a custom event to the cell. When the table generation calls the celllayout event it passes it the dimensions and coordinates of the cell which you can use to place and size the textfield.
First create this call, which is the 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'll use the event like this:
PdfPCell cell = new PdfPCell()
{
CellEvent = new CustomCellLayout(fieldname)
// set borders, or other cell options here
};
If you want to different kinds of textfields you can either make additional custom events, or you could add extra properties to the CustomCellLayout class like "fontsize" or "multiline" which you'd set with the class constructor, and then check for in the CellLayout code to adjust the textfield properties.

- 554
- 5
- 13
-
Oh, yeah! This is awesomeness personified, err...codified. – B. Clay Shannon-B. Crow Raven Apr 14 '15 at 17:28
-
What if you wanted to create Checkboxes - how would the code above need to be modified for that event (no pun intended)? – B. Clay Shannon-B. Crow Raven Apr 14 '15 at 20:13
-
1@B.ClayShannon you'd add a RadioCheckField instead of a TextField. The Rectangle passed into the RadioCheckField constructor should be made using the rectangle passed into the CellLayout adjusted with the offset and dimensions of the checkbox that you want. – Jeff S Apr 15 '15 at 05:05
-
CellLayout is not called directly, though; I guess I can create a Rectangle within CellLayout() and use that instead of the default/magic Rectangle arg named rectangle...for more detail on how I'm trying to accomplish this, please see http://www.codeproject.com/Questions/896783/How-can-I-add-text-to-my-checkbox-on-a-PDF-form-us – B. Clay Shannon-B. Crow Raven Apr 15 '15 at 15:37
-
I now added a question here on SO diesbzg.: http://stackoverflow.com/questions/29655682/how-can-i-add-text-to-a-checkbox-i-create-using-itextsharp – B. Clay Shannon-B. Crow Raven Apr 15 '15 at 16:25