We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback


Trusted by the world’s leading companies

Syncfusion Trusted Companies

Overview

The Syncfusion Blazor PDF library allows users to split a PDF file into multiple sub-documents quickly and accurately. It also allows users to split, combine, import, and append PDFs.

Split PDF Blazor


How to split PDF files using Blazor

  1. Install the Syncfusion.Pdf.Net.Core NuGet package in your project.
  2. Create FileStream objects to represent input PDF files.
  3. Create a PdfLoadedDocument object by passing the FileStream object.
  4. Create a new PDF document using the PdfDocument class.
  5. Use the ImportPage method of the PdfDocumentBase class to split a PDF document into multiple files.
  6. Save the PdfDocument object to the FileStream object.

Here is an example of how to split PDF files using the Syncfusion Blazor PDF library. You can split a PDF file into a single page or multiple page PDF document with just a few lines of code.

//Create the FileStream object
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load a PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
//Iterate over the pages
for (int pageIndex = 0; pageIndex < loadedDocument.PageCount; pageIndex++)
{
    //Create a new PdfDocument object
    using (PdfDocument outputDocument = new PdfDocument())
    {
        //Import the page from the loadedDocument to the outputDocument
        outputDocument.ImportPage(loadedDocument, pageIndex);
        //Save the document into a filestream object
        using (MemoryStream outputStrem = new MemoryStream())
        {
            outputDocument.Save(outputStrem);
            //Download the PDF document
            await JS.SaveAs("Output" + pageIndex + ".pdf", outputStrem.ToArray());
        }
    }
}

PDF split options

Split a range of pages into a separate PDF document

A range of pages can be split into a separate PDF document.

FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load a PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream))
{
    int[,] values = new int[,] { { 0, 2 },{ 2, 4 } };
    loadedDocument.DocumentSplitEvent += (object sender, PdfDocumentSplitEventArgs args) =>
    {
        MemoryStream stream = new MemoryStream();
        args.PdfDocumentData.CopyTo(stream);
        //Download the PDF document
        JS.SaveAs(Guid.NewGuid().ToString() + ".pdf", stream.ToArray());
    };
    //Import the page from the loadedDocument to the outputDocument
    loadedDocument.SplitByRanges(values);    
}

Import or delete certain pages from a PDF file

Users can get specific pages or delete certain pages from a PDF document.

//Create the FileStream object
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream))
{
    //Creates a new PDF document
    using (PdfDocument outputDocument = new PdfDocument())
    {
        //Remove the first page in the PDF document
        loadedDocument.Pages.RemoveAt(0);
        //Import certain pages to the new PDF document
        outputDocument.ImportPage(loadedDocument, 0);
        //Save the document into a filestream object
        using (MemoryStream outputStrem = new MemoryStream())
        {
            outputDocument.Save(outputStrem);
            //Download the PDF document
            await JS.SaveAs("Output.pdf", outputStrem.ToArray());
        }
    }
}

Split a document based on bookmarks

Split a PDF document based on bookmarks.

//Create the FileStream object 
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream))
{
    //Create bookmark object
    PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
    //Load the PDF document from stream
    foreach (PdfBookmark bookmark in bookmarks)
    {
        if (bookmark.Destination != null)
        {
            if (bookmark.Destination.Page != null)
            {
                int endIndex = bookmark.Destination.PageIndex;
                //Create a new PDF document
                using (PdfDocument document = new PdfDocument())
                {
                    foreach (PdfLoadedBookmark childBookmark in bookmark)
                    {
                        endIndex = childBookmark.Destination.PageIndex;
                    }
                    //Import the pages to the new PDF document
                    document.ImportPageRange(loadedDocument, bookmark.Destination.PageIndex, endIndex);
                    //Save the document into a filestream object
                    using (MemoryStream outputStrem = new MemoryStream())
                    {
                        document.Save(outputStrem);
                        //Download the PDF document
                        await JS.SaveAs(bookmark.Title + ".pdf", outputStrem.ToArray());
                    }
                }
            }
        }
    }
}




Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.

Scroll up icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon