The Syncfusion ASP.NET core PDF library that allows users to split a PDF file into multiple sub-documents quickly and accurately. It also allows you to split, combine, import, and append PDFs.
Here is an example of how to split PDF files in C# using the Syncfusion ASP.NET Core PDF library. You can split a PDF file into a single page or multiple page PDF document with just a few lines of code.
//Load PDF document as stream
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Create zip file
ZipArchive zipArchive = new ZipArchive();
//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);
//Save the document
MemoryStream stream = new MemoryStream();
outputDocument.Save(stream);
//Add the files you want to zip
zipArchive.AddItem(pageIndex + ".pdf", stream, false, (Syncfusion.Compression.FileAttributes)FileAttributes.Normal);
}
}
//Zips the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
return File(memoryStream.ToArray(), "application/zip", "SplitFiles.zip");
A range of pages can be split into a separate PDF document.
//Load PDF document as stream
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
int[,] values = new int[,] { { 0,2 }, { 2, 4 } };
ZipArchive zipArchive = new ZipArchive();
MemoryStream memoryStream = new MemoryStream();
document.DocumentSplitEvent += (object sender, PdfDocumentSplitEventArgs args) =>
{
zipArchive.AddItem(Guid.NewGuid().ToString() + ".pdf", args.PdfDocumentData, false, (Syncfusion.Compression.FileAttributes)FileAttributes.Normal);
};
//Split the pages into fixed number
document.SplitByRanges(values);
//Close the document
document.Close(true);
//Add the files you want to zip
zipArchive.Save(memoryStream, false);
return File(memoryStream.ToArray(), "application/zip", "SplitFilesZ.zip");
Users can get specific pages or delete certain pages from a PDF document.
//Load PDF document as stream
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load an existing PDF document
using (PdfLoadedDocument document = new PdfLoadedDocument(docStream))
using (PdfDocument outputDocument = new PdfDocument())
{
ZipArchive zipArchive = new ZipArchive();
//Remove the first page in the PDF document
document.Pages.RemoveAt(0);
//Import certain pages to the new PDF document
outputDocument.ImportPage(document, 0);
//Save the document
MemoryStream stream = new MemoryStream();
outputDocument.Save(stream);
//Add the files you want to zip
zipArchive.AddItem("SplitResult.pdf", stream, false, (Syncfusion.Compression.FileAttributes)FileAttributes.Normal);
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
return File(memoryStream.ToArray(), "application/zip", "SplitFiles.zip");
}
Split a PDF document based on bookmarks.
//Load PDF document as stream
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document from stream
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream))
{
//Create bookmark object
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
ZipArchive zipArchive = new ZipArchive();
//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);
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Add the files you want to zip
zipArchive.AddItem(bookmark.Title + ".pdf", stream, false, (Syncfusion.Compression.FileAttributes)FileAttributes.Normal);
}
}
}
}
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
return File(memoryStream.ToArray(), "application/zip", "SplitFiles.zip");
}
}
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.