Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code looks good and is only missing a couple of intermediate steps. </p> <p>Since you're using the same PDF template for every page (when two or more pages need to be generated), instead of using a <code>PdfStamper</code> to add content directly to the <code>Document</code>, you use a <code>PdfSmartCopy</code> or <code>PdfCopy</code> object. </p> <p>The <code>PdfStamper</code> is still needed. However, in this case it's used to create an in-memory (single) page filled with data as you as you iterate over your <code>Models.Statement</code> collection. </p> <p>In other words, <code>PdfSmartCopy/PdfCopy</code> maintains your statements as a whole, (total pages) and <code>PdfStamper</code> is used as a buffer that adds your individual statements page-by-page to your PDF. Here's a simple working example HTTP hander (.ashx):</p> <pre><code>&lt;%@ WebHandler Language="C#" Class="copyFillTemplate" %&gt; using System; using System.Collections.Generic; using System.IO; using System.Web; using iTextSharp.text; using iTextSharp.text.pdf; public class copyFillTemplate : IHttpHandler { public void ProcessRequest (HttpContext context) { HttpServerUtility Server = context.Server; HttpResponse Response = context.Response; Response.ContentType = "application/pdf"; // template used to test __this__ example; // replace with __your__ PDF template string pdfTemplatePath = Server.MapPath( "~/app_data/template.pdf" ); // this example's test data; replace with __your__ data collection List&lt;Statement&gt; statementList = Statement.GetStatements(); // COPY FROM HERE using (Document document = new Document()) { // PdfSmartCopy reduces PDF file size by reusing parts // of the PDF template, but uses more memory. you can // replace PdfSmartCopy with PdfCopy if memory is an issue using (PdfSmartCopy copy = new PdfSmartCopy( document, Response.OutputStream) ) { document.Open(); // used to test this example int counter = 0; // generate one page per statement foreach (Statement statment in statementList) { ++counter; // replace this with your PDF form template PdfReader reader = new PdfReader(pdfTemplatePath); using (var ms = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(reader, ms)) { AcroFields form = stamper.AcroFields; // replace this with your field data for each page form.SetField("title", counter.ToString()); stamper.FormFlattening = true; } reader = new PdfReader(ms.ToArray()); // add one page at a time; assumes your template is only one page. // if your template is more than one page you will need to // call GetImportedPage() for each page in your template copy.AddPage(copy.GetImportedPage(reader, 1)); } } } // COPY TO HERE } } public bool IsReusable { get { return false; } } public class Statement { public string FieldName, FieldValue; public static List&lt;Statement&gt; GetStatements() { List&lt;Statement&gt; s = new List&lt;Statement&gt;(); for (int i = 0; i &lt; 5; ++i) {s.Add(new Statement());} return s; } } } </code></pre> <p>Hopefully the inline comments help. And you obviously need to remove replace some parts I used to test the example code.</p>
    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