The Syncfusion .NET MAUI PDF library that 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.
Here is an example of how to split PDF files using the Syncfusion .NET MAUI PDF library. You can split a PDF file into a single page or multiple page PDF document with just a few lines of code.
//Open an existing PDF document.
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Iterate over the pages
for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
{
//Create a new PdfDocument object
using (PdfDocument outputDocument = new PdfDocument())
{
//Import the page from the loadedDocument to the outputDocument
outputDocument.ImportPage(document, pageIndex);
string filePath = "Result" + pageIndex + ".pdf";
//Save the document
using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
outputDocument.Save(outputFileStream);
}
}
}
A range of pages can be split into a separate PDF document.
//Open an existing PDF document.
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream inputFileStream = assembly.GetManifestResourceStream("Input.pdf");
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream))
{
int[,] values = new int[,] { { 0, 2 }, { 2, 4 } };
loadedDocument.DocumentSplitEvent += (object sender, PdfDocumentSplitEventArgs args) =>
{
string filePath = Guid.NewGuid().ToString()+".pdf";
//Save the resulting document.
FileStream outputStream = new FileStream(filePath, FileMode.CreateNew);
args.PdfDocumentData.CopyTo(outputStream);
outputStream.Close();
};
//Spit the document by ranges.
loadedDocument.SplitByRanges(values);
}
Users can get specific pages or delete certain pages from a PDF document.
//Open an existing PDF document.
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//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);
//Give file path
string filePath = "Split.pdf";
//Save the document into a filestream object
using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
outputDocument.Save(outputFileStream);
}
}
}
Split a PDF document based on bookmarks.
//Open an existing PDF document.
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream docStream = assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//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);
//Give file path
string filePath = bookmark.Title +".pdf";
//Save the PDF file with the bookamark title's name.
using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
document.Save(outputFileStream);
}
}
}
}
}
}
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.