Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multipage PDF document from predefined template

I have a requirement to generate invoice reports in PDF using some predefined company templates. I am able to create/generate SINGLE PAGE PDF reports using iTextSharp.

Problem: The problem comes when the invoice statement spans MULTIPLE PAGES. I am not able to extend the report(invoice statement) to next(2nd) page. If all the data can not be accommodated on one page it should be written on 2nd page, while still using the company template.

The template is present at following path:

HostingEnvironment.MapPath("~/Content/InvoiceTemplate/invoiceTemplate.pdf")

I am using iTextSharp library to create documents. Below is the code used to generate the PDF:

public class pdfStatementController : Controller {

        Models.DYNAMICS_EXTEntities _db = new Models.DYNAMICS_EXTEntities();

        //
        // GET: /pdfStatement/


        public ActionResult SendPdfStatement(string InvoiceNumber) {
            try {
                InvoiceNumber = InvoiceNumber.Trim();

                ObjectParameter[] parameters = new ObjectParameter[1];
                parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

                List<Models.Statement> statementList = new List<Models.Statement>();
                statementList = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();
                pdfStatementController.WriteInTemplate(statementList);

                return RedirectToAction("Invoice", "Invoice", new { id = statementList.FirstOrDefault().Customer_ID.ToString().Trim() });
            } catch (Exception e) {
                return View("Error");
            }
        } 

    public static void WriteInTemplate(List<Models.Statement> statementList) {
        string invoiceNumber = statementList.FirstOrDefault().Invoice.ToString().Trim();
        string month = null;
        string day = null;
        string year = null;

        PdfReader pdfReader = new PdfReader(
                                  HostingEnvironment.MapPath(
                                       "~/Content/InvoiceTemplate/invoiceTemplate.pdf"));
        FileStream fileStream = new FileStream(
                                   HostingEnvironment.MapPath(
                                      "~/Content/reports/" + invoiceNumber + ".pdf"),
                                      FileMode.Create);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
        AcroFields pdfFields = pdfStamper.AcroFields;

        pdfFields.SetField("BillToCompany", statementList.FirstOrDefault().BillToCompany.ToString().Trim().ToUpper());
        pdfFields.SetField("BillToContact", statementList.FirstOrDefault().BillToContact.ToString().Trim().ToUpper());
        pdfFields.SetField("CustomerId", statementList.FirstOrDefault().Customer_ID);
        pdfFields.SetField("InvoiceNumber", statementList.FirstOrDefault().Invoice.ToString().Trim());
        pdfFields.SetField("JobNumber", statementList.FirstOrDefault().JobNumber.ToString().Trim());
        pdfFields.SetField("Caller", statementList.FirstOrDefault().Caller.ToString().Trim());

        pdfStamper.FormFlattening = true; // generate a flat PDF 
        pdfStamper.Close();
        pdfReader.Close();
    }
}
like image 873
14578446 Avatar asked Dec 15 '11 07:12

14578446


1 Answers

Your code looks good and is only missing a couple of intermediate steps.

Since you're using the same PDF template for every page (when two or more pages need to be generated), instead of using a PdfStamper to add content directly to the Document, you use a PdfSmartCopy or PdfCopy object.

The PdfStamper 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 Models.Statement collection.

In other words, PdfSmartCopy/PdfCopy maintains your statements as a whole, (total pages) and PdfStamper 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):

<%@ WebHandler Language="C#" Class="copyFillTemplate" %>
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<Statement> 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<Statement> GetStatements() {
      List<Statement> s = new List<Statement>();
      for (int i = 0; i < 5; ++i) {s.Add(new Statement());}
      return s;
    }
  }
}

Hopefully the inline comments help. And you obviously need to remove replace some parts I used to test the example code.

like image 174
kuujinbo Avatar answered Oct 26 '22 11:10

kuujinbo