Freely fill PDF form with the help of iText or iTextSharp

iText(java version) and iTextSharp(dotnet version) are very powerful libraries to create and manipulate PDF, but this time, I just want to talk about how to fill PDF form with the help of iText or iTextSharp.

for java version, please visit http://itextdocs.lowagie.com/tutorial/#part5, there are many examples about fill or create PDF form.

for dotnet version(C#),
the easiest example is
PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
PdfStamper stamp1 = new PdfStamper(reader, new FileStream("registered.pdf",FileMode.Create));
AcroFields form1 = stamp1.AcroFields;

form1.SetField("name", "Steven");
form1.SetField("address", "http://blog.rubypdf.com");
form1.SetField("postal_code", "200051");
form1.SetField("email", "rocsky@gmail.com");
stamp1.Close();
reader.Close();

and if you want to fill CJK(Chinese, Japanese and Korean) characters to PDF from, have a look the following code,

BaseFont.AddToResourceSearch("iTextAsian-1.0.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps-1.0.dll");
BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//if you want to use truetype fonts
//BaseFont font = BaseFont.CreateFont("c:\\windows\\fonts\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
PdfStamper stamp1 = new PdfStamper(reader, new FileStream("registered.pdf",FileMode.Create));

AcroFields form1 = stamp1.AcroFields;

//if the field you want to fill CJK characters, but the font it used does not support cjk, you need modify it before fill.
form1.SetFieldProperty("name","textfont",font,null);

//fill the form now
form1.SetField("name", "小李");
form1.SetField("address", "http://blog.rubypdf.com");
form1.SetField("postal_code", "200051");
form1.SetField("email", "rocsky@gmail.com");
stamp1.Close();
reader.Close();

related resources from my Chinese Blog and my free asp.net hosting,

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride

One Response to “Freely fill PDF form with the help of iText or iTextSharp

  • 1
    manojitc
    July 19th, 2011 15:01

    we can fil any pdf using the following code.
    The FillCell() fuction has few parameters which embed the text in the specified co-ordinate.
    I am using the edit function to stamp text in the existing Doc.pdf.

    public void EditPDF()
    {

    try
    {
    PdfReader reader = new PdfReader(Path.Combine(Server.MapPath(“~/pdf”), “Doc.pdf”));
    string sPdfName = Path.Combine(Server.MapPath(“~/pdf”), “Doc221.pdf”);
    //int n = reader.GetPageSize();

    PdfStamper stamp = new PdfStamper(reader, new FileStream(
    sPdfName, FileMode.Create));

    PdfContentByte over;
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);

    over = stamp.GetOverContent(1);
    //over = stamp.getUnderContent(i);
    /*Image img = Image.getInstance(“Blank.JPG”);
    img.setAbsolutePosition(170, 279);
    over.addImage(img);
    */
    over.BeginText();
    //over.setFontAndSize(bf, 18);
    //over.setTextMatrix(30, 30);
    //over.showText(“page ” + i);

    over.SetFontAndSize(bf, 10);
    FillCell(over, “xyzabc”, 280F, 692F);

    over.EndText();

    stamp.Close();
    reader.Close();
    return;

    }
    catch (Exception e)
    {
    //e.printStackTrace();
    }

    }

    public void FillCell(PdfContentByte over,String sText,float x,float y)
    {
    try
    {

    //System.out.println(sText.length());

    float fx=x;;
    over.ShowTextAligned(1, sText, fx , y, 0f);
    return;

    }
    catch(Exception e)
    {
    //e.printStackTrace();
    }

    }

Leave a Reply

You must be logged in to post a comment.