tags:

views:

971

answers:

2

Hi,

I'm using iTextSharp to create a PDF, how can I add a textField into PdfPCell?

Thank you so much

+2  A: 

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.

Jay Riggs
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
+1  A: 

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

DaveB
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
I have updated my answer.
DaveB
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
i've resolved the problem .. thank's a lot Dave!!bye
gigiot