Tutorials > VSPrinter Examples > Page n of m |
This example shows two methods for getting headers that indicate the total number of pages in the document (Page n of m).
This method is simple and adequate for short documents. It consists of creating the document twice: once just for counting the pages, and once for creating the complete document.
Assuming the VSPrinter control is called vp and that there is a routine called CreateDocument that creates the document, the following code would create the headers we want:
Example Title |
Copy Code
|
---|---|
' pass 1: count the pages vp.Header = "Page %d of ?" 'just a placeholder for now vp.StartDoc CreateDocument vp.EndDoc ' pass 2: set the header, then create the document again vp.Header = "Page %d of " & vp.PageCount ' actual page count vp.StartDoc CreateDocument vp.EndDoc |
This method is simple and works well, but it requires the document to be generated twice. This can be slow if the document is long (over 30 pages or so) or if generating the document requires extensive work such as database access or retrieving data from a network. For these situations, try the single-pass method, described below.
This method uses overlays to accomplish the same tasks in a single pass. The headers are drawn on each page after the document is complete. Here is the code:
Example Title |
Copy Code
|
---|---|
' pass 1: create the document with no header vp.Header = " " vp.StartDoc CreateDocument vp.EndDoc ' pass 2: add the headers (not really a "pass"...) vp.FontName = "Courier" vp.FontSize = 10 For i = 1 To vp.PageCount vp.StartOverlay i vp.CurrentX = vp.MarginLeft vp.CurrentY = vp.MarginTop - 300 vp = "Page " & i & " of " & vp.PageCount vp.EndOverlay Next |
Try both methods with a document over 20 pages long and you should notice the difference in speed. The single pass method is substantially faster, especially if the document is complex (with pictures, tables, and RTF text).