C#/VB.NET – Split a Single PDF Page into Multiple Pages

In a certain circumstance, you may get a scanned PDF document that has a very large page size, which prevents you from reading it comfortably. It is a good practice that you divide the large page into two or more smaller pages. In this article, I am going to introduce how to split a single PDF page into multiple pages in C# and VB.NET using Spire.PDF for .NET.

Installing Spire.PDF

First of all, you need to download the latest version of Spire.PDF from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet.

Now, let’s take a look at the code part.

Using the code

Spire.PDF offers the PdfPageBase.CreateTemplate().Draw() method to draw content of a source page to another page. To split a single PDF into two or multiple pages, we can actually draw the content of the source page on multiple smaller pages using this method. So, it is required that the page size of the resulting PDF document is a fraction (e.g. one half, one third or one quarter) of that of the original. The following code snippet shows you how to split a single page into two pages.

[C#]

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace SplitPdfPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a sample PDF document
            PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\SinglePage.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a new PDF document
            PdfDocument newDoc = new PdfDocument();

            //Remove page margins
            newDoc.PageSettings.Margins.All = 0;

            //Set the page width
            newDoc.PageSettings.Width = page.Size.Width;

            //Set the page height to half of that of the original page
            newDoc.PageSettings.Height = page.Size.Height / 2;

            //Add a new page to the new PDF
            PdfPageBase newPage = newDoc.Pages.Add();

            //Create layout format
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            //Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            //Save and close
            newDoc.SaveToFile("TwoPages.pdf");
            newDoc.Close();
            doc.Close();
        }
    }
}

[VB.NET]

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
 
Namespace SplitPdfPage
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Load a sample PDF document
            Dim doc As PdfDocument =  New PdfDocument("C:\Users\Administrator\Desktop\SinglePage.pdf") 
 
            'Get the first page
            Dim page As PdfPageBase =  doc.Pages(0) 
 
            'Create a new PDF document
            Dim NewDoc As PdfDocument =  New PdfDocument() 
 
            'Remove page margins
            NewDoc.PageSettings.Margins.All = 0
 
            'Set the page width
            NewDoc.PageSettings.Width = page.Size.Width
 
            'Set the page height to half of that of the original page
            NewDoc.PageSettings.Height = page.Size.Height / 2
 
            'Add a new page to the new PDF
            Dim NewPage As PdfPageBase =  NewDoc.Pages.Add() 
 
            'Create layout format
            Dim format As PdfTextLayout =  New PdfTextLayout() 
            format.Break = PdfLayoutBreakType.FitPage
            format.Layout = PdfLayoutType.Paginate
 
            'Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format
            page.CreateTemplate().Draw(NewPage,New PointF(0,0),format)
 
            'Save and close
            NewDoc.SaveToFile("TwoPages.pdf")
            NewDoc.Close()
            doc.Close()
        End Sub
    End Class
End Namespace

Leave a comment