You may have noticed that in the previous examples, we started adding content to the document right after creating the C1PdfDocument object. This is possible because when you create the C1PdfDocument, it automatically adds an empty page to the document, ready to receive any type of content.
When you are done filling up the first page, you can add a new one using the NewPage method.
By default, all pages in the document have the same size and orientation. These parameters can be specified in the C1PdfDocument constructor. You can also change the page size and orientation at any time by setting the C1PdfDocument.PaperKind, C1PdfDocument.PageSize, and C1PdfDocument.Landscape properties. For example, the code below creates a document with all paper sizes defined by the PaperKind enumeration:
Dim font As New Font("Arial", 9)
Dim sf As New StringFormat()
sf.Alignment = HorizontalAlignment.Center
sf.LineAlignment = VerticalAlignment.Center
' Create one page with each paper size.
Dim firstPage As Boolean = True
Dim pk As PaperKind
For Each pk In System.Enum.GetValues(GetType(PaperKind))
' Skip custom size.
If pk = PaperKind.Custom Then
GoTo ContinueForEach1
End If
' Add new page for every page after the first one.
If Not firstPage Then
_c1pdf.NewPage()
End If
firstPage = False
' Set paper kind.
_c1pdf.PaperKind = pk
' Draw some content on the page.
_c1pdf.DrawString("PaperKind: " + pk.ToString(), font, Colors.Black, _c1pdf.PageRectangle(), sf)
ContinueForEach1:
Next pk
•C#
Font font = new Font("Arial", 9);
StringFormat sf = new StringFormat();
sf.Alignment = HorizontalAlignment.Center;
sf.LineAlignment = VerticalAlignment.Center;
// Create one page with each paper size.
bool firstPage = true;
foreach (PaperKind pk in Enum.GetValues(typeof(PaperKind)))
{
// Skip custom size.
if (pk == PaperKind.Custom)
{
continue;
}
// Add new page for every page after the first one.
if (!firstPage)
{
_c1pdf.NewPage();
}
firstPage = false;
// Set paper kind.
_c1pdf.PaperKind = pk;
// Draw some content on the page.
_c1pdf.DrawString("PaperKind: " + pk.ToString(), font, Colors.Black, _c1pdf.PageRectangle(), sf);
}
You are not restricted to writing on the last page that was added to the document. You can use the C1PdfDocument.CurrentPage property to select which page you want to write to, and then use the regular drawing commands as usual. This is useful for adding content to pages after you are done rendering a document. For example, the code below adds footers to each page containing the current page number and the total of pages in the document (page n of m):
Private Sub AddFooters()
Dim font As New Font("Helvetica", 7, FontStyle.Bold)
Dim sf As New StringFormat()
sf.Alignment = HorizontalAlignment.Center
Dim page As Integer
For page = 0 To _c1pdf.Pages.Count - 1
' Select page.
_c1pdf.CurrentPage = page
' Build rectangle for rendering the footer.
Dim rect As Rect = _c1pdf.PageRectangle()
rect.Y = rect.Bottom
' Write the footer.
Dim text As String
text = String.Format("Page {0} of {1}", page + 1, _c1pdf.Pages.Count)
_c1pdf.DrawString(text, font, Colors.Gray, rect, sf)
Next page
End Sub
•C#
private void AddFooters()
{
Font font = new Font("Helvetica", 7, FontStyle.Bold);
StringFormat sf = new StringFormat();
sf.Alignment = HorizontalAlignment.Center;
for (int page = 0; page < _c1pdf.Pages.Count; page++)
{
// Select page.
_c1pdf.CurrentPage = page;
// Build rectangle for rendering the footer.
Rect rect = _c1pdf.PageRectangle();
rect.Y = rect.Bottom – 36;
// Write the footer.
string text = string.Format("Page {0} of {1}", page+1, _c1pdf.Pages.Count);
_c1pdf.DrawString(text, font, Colors.Gray, rect, sf);
}
}
Note that the code uses the Pages property to get the page count. Pages is a collection based on the ArrayList class, and has methods that allow you to count and enumerate pages, as well as add and remove pages at specific positions. You can use the Pages collection to remove pages from certain locations in the document and re-insert them elsewhere.