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

Leave a Reply

You must be logged in to post a comment.