The Syncfusion UWP 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.
Here is an example of how to split PDF files in C# using the Syncfusion UWP PDF library. Users can split a PDF file into a single page or multiple page PDF document with just a few lines of code.
//Create a zip file
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//Load an existing PDF document
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//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 memory object
MemoryStream ms = new MemoryStream();
outputDocument.Save(ms);
zipArchive.AddItem("Files" + pageIndex + ".pdf", ms, false, System.IO.FileAttributes.Normal);
ms.Position = 0;
}
}
//Zip the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream,false);
Save(memoryStream, "SplitDocuments.zip");
}
A range of pages can be split into a separate PDF document.
//Create a zip file
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//Load an existing PDF document
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//Subscribe to the document split event.
loadedDocument.DocumentSplitEvent += LoadDocument_DocumentSplitEvent;
//Spit the document by ranges
loadedDocument.SplitByRanges(new int[,] { { 0, 2 }, { 2, 4 } });
}
//Zips the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
Save(memoryStream, "SplitDocuments.zip");
void LoadDocument_DocumentSplitEvent(object sender, PdfDocumentSplitEventArgs args)
{
//Save the resulting document
MemoryStream ms = new MemoryStream();
args.PdfDocumentData.CopyTo(ms);
zipArchive.AddItem(Guid.NewGuid().ToString() + ".pdf", ms, false, System.IO.FileAttributes.Normal);
}
Users can get specific pages or delete certain pages from a PDF document.
//Create a zip file
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//Load an existing PDF document
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//Create a new PdfDocument object
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 memory object
MemoryStream ms = new MemoryStream();
outputDocument.Save(ms);
zipArchive.AddItem("Files.pdf", ms, false, System.IO.FileAttributes.Normal);
ms.Position = 0;
}
//Zip the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream,false);
Save(memoryStream, "SplitDocuments.zip");
}
Split a PDF document based on bookmarks.
//Create a zip file
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//Load an existing PDF document.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Input.pdf");
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//Create a 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 PDF file with the bookamark title's name
MemoryStream ms = new MemoryStream();
loadedDocument.Save(ms);
zipArchive.AddItem(bookmark.Title, ms, false, System.IO.FileAttributes.Normal);
ms.Position = 0;
}
}
}
}
//Zips the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
Save(memoryStream, "SplitDocuments.zip");
}
CASE STUDY
Syncfusion’s file format components helped me create the reports I needed, fast. – J. Pereira, Software Developer.
The libraries have been built from scratch and refined for more than a decade to provide blazing-fast performance, comprehensive API, and compatibility across the latest and older versions of these files.
VIDEOS
Syncfusion File Format Libraries - Manipulate Excel, Word, PowerPoint, and PDF files
Read and write Excel, Word, PDF, and PowerPoint files. Also includes integrated visualization capabilities. Advanced features include support for pivot tables, pivot charts, mail-merge, and extensive formatting.
E-BOOK
Succinctly Series: Statistics Using Excel Succinctly
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.