Persits Software, Inc. Knowledge Base Articles

HOWTO: Add a custom header and/or footer to the document

Problem Description

This article explains how to add a custom header and/or footer to a document generated from a URL with AspPDF.

Solution

The ImportFromUrl method populates the current PdfDocument object with one or multiple pages. To put a custom header and/or footer on each page of the document, the Doc.Pages collection should be used to iterate through the pages.

The following code snippet (VB Script and C# versions) places the text "Page n of nnn" in the lower-left corner of each page of the document:

VB Script:

Set Pdf = Server.CreateObject("Persits.PDF")

Set Doc = Pdf.CreateDocument
Doc.ImportFromUrl "http://www.server.com/path/script.asp"

For Each Page in Doc.Pages
  str = "Page " & Page.Index & " of " & Doc.Pages.Count
  Page.Canvas.DrawText str, "x=10, y=20", Doc.Fonts("Courier")
Next

Doc.Save "c:\path\file.pdf", False

C#:

String str;

IPdfManager objPDF;
objPDF = new PdfManager();

// Create new document
IPdfDocument objDoc = objPDF.CreateDocument(Missing.Value);

objDoc.ImportFromUrl("http://www.server.com/path/script.asp", Missing.Value, Missing.Value, Missing.Value );

foreach( IPdfPage objPage in objDoc.Pages )
{
  str = String.Format("Page {0} of {1}", objPage.Index, objDoc.Pages.Count );
  objPage.Canvas.DrawText( str, "x=10; y=20", objDoc.Fonts["Courier", Missing.Value] );
}

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