1) For PDF form where data fields have fixed length, it is quite easy to use iText (I use C# version iTextSharp here) to bind data to PDF Text Field:
// Read PDF template
iTextSharp.text.pdf.PdfReader pdfRd = new iTextSharp.text.pdf.PdfReader(Server.MapPath("~/SampleJS.pdf"));
iTextSharp.text.pdf.PdfStamper stamp = new iTextSharp.text.pdf.PdfStamper(pdfRd, outputStream);
// Set form Text Fields
iTextSharp.text.pdf.AcroFields fields = stamp.AcroFields;
fields.SetField("formText", value);
// Close stamp
stamp.Close();
2) For free text data, you cannot put PDF Text Field to show data. For example, if you want to bind data to this template:
"Welcome to «Institution»! Please send mail to «Person» before «Date»"
You cannot use PDF Text Field because Text Field has fixed length. If «Institution» value is short, there will be extra blank space in the statement.
So what can you do? You can put the template statement into one Text Field with “Read Only” permission. Then you can replace the string with data like below:
// Get Text Fields from PDF file
iTextSharp.text.pdf.AcroFields fields = stamp.AcroFields;
// Get template
string formText = fields.GetField("formText");
// Replace template with data
formText = formText.Replace("«Institution»", txtInstitution);
formText = formText.Replace("«Person»", txtPerson);
formText = formText.Replace("«Date»", date);
// Set back the Text Field
fields.SetField("formText", formText);
3) How to insert image? PDF Text Field can be a placeholder for image. For example, to add a logo image to a placeholder:
// Get content to make changes
PdfContentByte overContent = stamp.GetOverContent(1);
// Get logo image
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/logo.jpg"));
// Get logo placeholder position
float[] logoArea = fields.GetFieldPositions("Logo");
// Get logo rectangle
iTextSharp.text.Rectangle logoRect = new Rectangle(logoArea[1], logoArea[2], logoArea[3], logoArea[4]);
// Set logo position in the placeholder (right alignment)
logo.ScaleToFit(logoRect.Width, logoRect.Height);
logo.SetAbsolutePosition(logoArea[3] - logo.ScaledWidth + (logoRect.Width - logo.ScaledWidth) / 2, logoArea[2] + (logoRect.Height - logo.ScaledHeight) / 2);
// Add image
overContent.AddImage(logo);
4) How to add a barcode? Barcode is similar to image:
// Get content to make changes
PdfContentByte overContent = stamp.GetOverContent(1);
// Create a barcode
Barcode39 code39 = new Barcode39();
// Assign barcode value
code39.Code = barcodeValue;
code39.StartStopText = false;
// Create image from the barcode
iTextSharp.text.Image image39 = code39.CreateImageWithBarcode(overContent, null, null);
// Get barcode image placeholder
float[] barcodeArea = fields.GetFieldPositions("AppIDBarCode");
iTextSharp.text.Rectangle rect = new Rectangle(barcodeArea[1], barcodeArea[2], barcodeArea[3], barcodeArea[4]);
image39.ScaleToFit(rect.Width, rect.Height);
image39.SetAbsolutePosition(barcodeArea[1] + (rect.Width - image39.ScaledWidth) / 2, barcodeArea[2] + (rect.Height - image39.ScaledHeight) / 2);
// Add barcode image
overContent.AddImage(image39);
I still have several issues to solve, such as how to put rich text into Text Field. But for now, I have a good start for PDF generation. :)