Paginate PDF File in C#/VB.NET

PDF pagination not only enables users to add page numbers in PDF file but also can divide PDF document into sections so that some additional information such as PDF file cover, introduction or reference can be added. So PDF pagination provides great convenience for managing large files or organizing books. This section will show you a solution to paginate PDF pages via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET, a PDF component for managing PDF files, enables you to paginate PDF pages quickly with C#, VB.NET. Before expounding the main code, I want to show the screenshot of the output PDF file as below:

Set PDF Pagination

Here you can download Spire.PDF for .NET and install it on your system. After adding Spire.Pdf dll, let us start to paginate PDF file together.

In the whole solution, there are three main steps to paginate PDF file. One is to draw page number; another is to draw content; the last one is to draw PDF cover so that you can add additional information. Let us see them one by one.

Paginate PDF - Draw PDF Page Number

When drawing page number, page label is also set in below method. By calling this method: DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format). You can not only set page label font, color, position and format. Please see detail code below:

[C#]
            foreach (PdfPageBase page in section.Pages)
            {
                page.Canvas.SetTransparency(0.5f);
                PdfBrush brush = PdfBrushes.Black;
                PdfPen pen = new PdfPen(brush, 0.75f);
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
                format.MeasureTrailingSpaces = true;
                float space = font.Height * 0.75f;
                float x = margin.Left;
                float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
                float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
                page.Canvas.DrawLine(pen, x, y, x + width, y);
                y = y + 1;
                String numberLabel
                    = String.Format("{0} of {1}", startNumber++, pageCount);
                page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
                page.Canvas.SetTransparency(1);
            }
[VB.NET]
         For Each page As PdfPageBase In section.Pages
            page.Canvas.SetTransparency(0.5!)
            Dim brush As PdfBrush = PdfBrushes.Black
            Dim pen As PdfPen = New PdfPen(brush, 0.75!)
            Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 9!, FontStyle.Italic), true)
            Dim format As PdfStringFormat = New PdfStringFormat(PdfTextAlignment.Right)
            format.MeasureTrailingSpaces = true
            Dim space As Single = (font.Height * 0.75!)
            Dim x As Single = margin.Left
            Dim width As Single = (page.Canvas.ClientSize.Width  _
                        - (margin.Left - margin.Right))
            Dim y As Single = ((page.Canvas.ClientSize.Height - margin.Bottom)  _
                        + space)
            page.Canvas.DrawLine(pen, x, y, (x + width), y)
            y = (y + 1)
            Dim numberLabel As String = String.Format("{0} of {1}", startNumber++, pageCount)
            page.Canvas.DrawString(numberLabel, font, brush, (x + width), y, format)
            page.Canvas.SetTransparency(1)
        Next

Paginate PDF - Draw PDF Content

PDF content depends on your own like, so here I avoid coding them in detail.

Paginate PDF - Draw PDF Cover

Cover page is always the best place for adding additional information about this PDF file. In below method, I ignore adding reference content and document title, you can add cover image a class PdfImage in the PDF cover page. Please see code below:

[C#]
            //cover
            PdfBrush brush3 = PdfBrushes.Black;
            PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9));
            PdfImage image
                = PdfImage.FromFile(@"..\potala palace1.jpg");
            String text = paginate_PDF.Properties.Resources.ImageDescription;
            float r = image.PhysicalDimension.Height / image.Height;
            PdfPen pen = new PdfPen(brush1, r);
            SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2);
            PdfTemplate template
                = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4,
                    image.PhysicalDimension.Height + 4 * r + 7 + size.Height);
            template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height);
[VB.NET]
        'cover
        Dim brush3 As PdfBrush = PdfBrushes.Black
        Dim brush4 As PdfBrush = New PdfSolidBrush(New PdfRGBColor(249, 249, 249))
        Dim image As PdfImage = PdfImage.FromFile("..\potala palace1.jpg")
        Dim text As String = paginate_PDF.Properties.Resources.ImageDescription
        Dim r As Single = (image.PhysicalDimension.Height / image.Height)
        Dim pen As PdfPen = New PdfPen(brush1, r)
        Dim size As SizeF = font1.MeasureString(text, (image.PhysicalDimension.Width - 2))
        Dim template As PdfTemplate = New PdfTemplate((image.PhysicalDimension.Width  _
                        + ((4 * r)  _
                        + 4)), (image.PhysicalDimension.Height  _
                        + ((4 * r) + (7 + size.Height))))
        template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height)

Spire.PDF for .NET is a professional PDF component to generate, edit, read, and manipulate PDF documents in your .NET applications with C#, VB.NET.