Creating documents from scratch with iTextSharp can be very time consuming. As an alternative, you can create (or reuse) PDF "template" documents by adding form fields to them (if necessary). The easiest way to do this is with a full version of Adobe Acrobat, but you can also add fillable form fields using nothing but iTextSharp.
For example, for an award or diploma, you find, create, or modify a PDF containing all of the text, graphics, fancy borders and fonts for the document, then add a form field for the recipient's name. You might add other fields for dates, signature lines, type of award, etc.
Then it's very easy to use iTextSharp from your web application to fill in the form, flatten it, and stream it back to the user.
At the end of this post is a complete ASHX handler code example.
Also remember that iTextSharp (or just iText) is also useful for combining PDF documents or pages from different documents. So, for an annual report that has a fixed design for a cover or explanation page but dynamically generated content, you can open the cover page, open a template page for the report, generate the dynamic content onto a blank area of the template, open the back page "boilerplate", then combine them all into a single document to return to the user.
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextFormFillerDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DemoForm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/pdf";
//This line will force the user to either open or save the
//file instead of it appearing on its own page - if you remove it,
//the page will appear in the browser in the same window.
context.Response.AddHeader("Content-Disposition",
"attachment; filename=DemoForm_Filled.pdf");
FillForm(context.Response.OutputStream);
context.Response.End();
}
// Fills the form and pushes it out the output stream.
private void FillForm(System.IO.Stream outputStream)
{
//Need to get the proper directory (dynamic path) for this file.
//This is a filesystem reference, not a web/URL reference...
//The PDF reader reads in the fillable form
PdfReader reader = new PdfReader("C:/DemoForm_Fillable.pdf");
//The PDF stamper creates an editable working copy of the form from the reader
//and associates it with the response output stream
PdfStamper stamper = new PdfStamper(reader, outputStream);
//The PDF has a single "form" consisting of AcroFields
//Note that this is shorthand for writing out
//stamper.AcroFields.SetField(...) for each set
AcroFields form = stamper.AcroFields;
//Set each of the text fields this way: SetField(name, value)
form.SetField("txtFieldName", "Field Value");
form.SetField("txtAnotherFieldName", "AnotherField Value");
//Set the radio button fields using the names and string values:
form.SetField("rbRadioButtons", "Yes"); //or "No"
//Form flattening makes the form non-editable and saveable with the
//form data filled in
stamper.FormFlattening = true;
//Closing the stamper flushes it out the output stream
stamper.Close();
//We're done reading the file
reader.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}