ComponentOne PDF for .NET
Creating Pages and Overlays
Using ComponentOne PDF for .NET > Creating Pages and Overlays

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 C1PdfDocumentBase.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 PaperKind, PageSize, and Landscape properties. For example, the code below creates a document with all paper sizes defined by the PaperKind enumeration:

To write code in Visual Basic

Visual Basic
Copy Code
Dim font As New Font("Arial", 9)
Dim sf As New StringFormat()
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.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, Brushes.Black, _c1pdf.PageRectangle, sf)
     ContinueForEach1:

To write code in C#

C#
Copy Code
Font font = new Font("Arial", 9);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.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, Brushes.Black, _c1pdf.PageRectangle, sf);
}

You are not restricted to writing on the last page that was added to the document. You can use the 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):

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub AddFooters()
    Dim font As New Font("Tahoma", 7, FontStyle.Bold)
    Dim sf As New StringFormat()
    sf.Alignment = StringAlignment.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 RectangleF = _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, Brushes.Gray, rect, sf)
    Next page
End Sub

To write code in C#

C#
Copy Code
private void AddFooters()
{
    Font font = new Font("Tahoma", 7, FontStyle.Bold);
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    for (int page = 0; page < _c1pdf.Pages.Count; page++)
    {
        // Select page.
        _c1pdf.CurrentPage = page;
         // Build rectangle for rendering the footer.
        RectangleF 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, Brushes.Gray, rect, sf);
    }
}

Note that the code uses the C1PdfDocumentBase.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.