Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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.</p> <p>At the end of this post is a complete ASHX handler code example.</p> <p>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.</p> <pre><code>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; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload