Persits Software, Inc. Knowledge Base Articles

HOWTO: Create a multi-page PDF document from a single-page template

Problem Description

The following article explains how to build a multi-page PDF document with AspPDF and AspPDF.NET based on a single-page PDF template.

Solution

The most efficient way to create a multi-page document based on a PDF template is to create a PdfGraphics object from this template and then draw it on each page of the new document being created. Use the PdfDocument method CreateGraphicsFromPage to create a graphics from a page. This method is described in detail in the sections

AspPDF's Section 9.6 - Drawing Other Documents' Pages

and

AspPDF.NET's Section 9.6 - Drawing Other Documents' Pages

The following code samples for AspPDF and AspPDF.NET create a set of theater tickets in PDF format for a family of 3. Each page of this document is an individual personalized ticket based on the same PDF template (shown below) carrying the ticket holder's name and unique serial number in the form of a barcode. Click on the image below to download the template.


The following image shows the resultant 3-page PDF document generated by the scripts below:


VB Script (AspPDF)

<%

strPath = "c:\patj\theater_template.pdf"

arrNames = Array( "John Smith", "Lynda Smith", "Annie Smith" )
arrSerials = Array( 1751945698, 1751945699, 175194570 )

Set objPdf = Server.CreateObject("Persits.PDF")
Set objTemplate = objPdf.OpenDocument(strPath)
fPageWidth = objTemplate.Pages(1).Width
fPageHeight = objTemplate.Pages(1).Height

Set objDoc = objPdf.CreateDocument()
Set objGraph = objDoc.CreateGraphicsFromPage(objTemplate, 1)
For i = 0 To UBound(arrNames)
   ' Add new page to document
   Set objPage = objDoc.Pages.Add( fPageWidth, fPageHeight)

   ' Draw template on it
   objPage.Canvas.DrawGraphics objGraph, "x=0; y=0"

   ' Draw text and barcodes
   objPage.Canvas.DrawText "William Shakespeare", "x=60; y=235; size=16", objDoc.Fonts("Courier")
   objPage.Canvas.DrawText "Much Ado About Nothing", "x=10; y=220; size=21", objDoc.Fonts("Courier")
   objPage.Canvas.DrawText "Jan 03, 2014", "x=10; y=25; size=14", objDoc.Fonts("Courier")

   objPage.Canvas.DrawText arrNames(i), "x=10; y=190; size=15; color=blue", objDoc.Fonts("Courier")
   objPage.Canvas.DrawBarcode2D arrSerials(i), "type=3; x=210; y=125; barwidth=3"
Next

objDoc.Save "c:\path\ticket.pdf", false

%>

C# (AspPDF.NET)

string strPath = @"c:\path\theater_template.pdf";

string [] arrNames = { "John Smith", "Lynda Smith", "Annie Smith" };
int [] arrSerials = { 1751945698, 1751945699, 175194570 };

PdfManager objPdf = new PdfManager();
PdfDocument objTemplate = objPdf.OpenDocument(strPath);
float fPageWidth = objTemplate.Pages[1].Width;
float fPageHeight = objTemplate.Pages[1].Height;

PdfDocument objDoc = objPdf.CreateDocument();
PdfGraphics objGraph = objDoc.CreateGraphicsFromPage(objTemplate, 1);
for (int i = 0; i < arrNames.Length; i++)
{
   // Add new page to document
   PdfPage objPage = objDoc.Pages.Add( fPageWidth, fPageHeight);

   // Draw template on it
   objPage.Canvas.DrawGraphics(objGraph, "x=0; y=0");

   // Draw text and barcodes
   objPage.Canvas.DrawText("William Shakespeare", "x=60; y=235; size=16", objDoc.Fonts["Courier"]);
   objPage.Canvas.DrawText("Much Ado About Nothing", "x=10; y=220; size=21", objDoc.Fonts["Courier"]);
   objPage.Canvas.DrawText("Jan 03, 2014", "x=10; y=25; size=14", objDoc.Fonts["Courier"]);

   objPage.Canvas.DrawText(arrNames[i], "x=10; y=190; size=15; color=blue", objDoc.Fonts["Courier"]);
   objPage.Canvas.DrawBarcode2D(arrSerials[i].ToString(), "type=3; x=210; y=125; barwidth=3");
}

objDoc.Save(@"c:\path\ticket.pdf", false);