Getting Started with ExpertPdf PDF Split Library for .NET. PDF pages extractor tool. |
ExpertPdf PDF Split Library for .NET extracts pages from PDF documents into new PDF documents. It can be used in any type of .NET application: console and Windows Forms applications, Windows services, ASP.NET and ASP.NET Core web applications.
The library is one assembly, epsplitpdf.dll. The main class is PDFSplitManager.
Extract a range of pages into a new document.
Split a document into a number of documents of the same size, or into documents of a given number of pages up to the end of the document.
Read the source document from a file or from memory, and save the new documents to files or keep them in memory.
Split password protected documents, opened with their user or owner password.
Set the document information (title, subject, author, keywords) and the security (passwords, permissions, RC4 or AES encryption with 40, 128 or 256 bit keys) of the new documents.
Split a document at its top level bookmarks: one document per chapter, named after the bookmark.
Extract a list of pages ("1-3,7,10-"), or the odd or even pages.
Split a document into documents under a maximum file size.
Get the page count of a document.
.NET Framework 4.0 or later. The download also has a build for .NET Framework 2.0 - 3.5 applications.
.NET Core 2.0 or later and .NET 5 to .NET 11, through the .NET Standard 2.0 build in the NuGet package.
Windows 10, Windows 11, Windows Server 2016 or later, 32-bit or 64-bit.
The easiest way to install the library is the ExpertPdf.SplitPdf package (https://www.nuget.org/packages/ExpertPdf.SplitPdf/). In Visual Studio open the NuGet Package Manager and install the package, or run:
dotnet add package ExpertPdf.SplitPdf
The package works for .NET Framework and for .NET Core / .NET 5 to .NET 11 projects. It is for AnyCPU and x86 applications. ExpertPdf.SplitPdf.x64 (https://www.nuget.org/packages/ExpertPdf.SplitPdf.x64/) is the x64 optimized build, for applications that target the x64 platform and run in a 64-bit process.
The download contains:
Bin\.NET_4.0\epsplitpdf.dll - the library for .NET Framework 4.0 and later
Bin\epsplitpdf.dll - the library for .NET Framework 2.0 - 3.5
Samples - samples for .NET Framework 4.6.2, in C# and VB.NET
Samples_NET_10 - the same samples for .NET 10 and .NET 11, using the NuGet package
Add a reference to the epsplitpdf.dll for your framework to your project.
The page numbers start at 1. A range of pages is given by the number of the first page and the number of pages; when the range goes past the end of the document, the new document ends with the last page.
using ExpertPdf.PDFSplit; PDFSplitManager splitManager = new PDFSplitManager(); // splitManager.LicenseKey = "your license key"; // pages 2 to 4 into a new document splitManager.ExtractPagesFromFileToFile(@"C:\Documents\report.pdf", @"C:\Documents\pages-2-4.pdf", 2, 3); Console.WriteLine("Pages: " + splitManager.GetPdfPageCount(@"C:\Documents\report.pdf"));
The methods with a repeat count extract that number of documents of the same size; the methods with a "repeat to end" flag go on to the end of the document. A file name given for several documents gets the index of each document: part.pdf becomes part_1.pdf, part_2.pdf and so on. The memory versions return an array of PDFSplitDoc objects: Buffer has the bytes of the document and SaveToFile(String) saves it.
PDFSplitManager splitManager = new PDFSplitManager(); // documents of 1 page up to the end: part_1.pdf, part_2.pdf, ... splitManager.ExtractPagesFromFileToFile(@"C:\Documents\report.pdf", @"C:\Documents\part.pdf", 1, 1, true); // two documents of 3 pages, starting with page 1: half_1.pdf and half_2.pdf splitManager.ExtractPagesFromFileToFile(@"C:\Documents\report.pdf", @"C:\Documents\half.pdf", 1, 3, 2); // documents of 2 pages up to the end, in memory byte[] pdfBytes = File.ReadAllBytes(@"C:\Documents\report.pdf"); PDFSplitDoc[] parts = splitManager.ExtractPagesFromMemoryToMemory(pdfBytes, 1, 2, true); foreach (PDFSplitDoc part in parts) { Console.WriteLine("{0} bytes, {1} pages", part.Buffer.Length, splitManager.GetPdfPageCount(part.Buffer)); } parts[0].SaveToFile(@"C:\Documents\first-part.pdf");
Every combination exists: ExtractPagesFromFileToFile, ExtractPagesFromFileToMemory, ExtractPagesFromMemoryToFile and ExtractPagesFromMemoryToMemory, each for one range, a repeat count or "repeat to end".
SplitByBookmarksFromFileToFolder(String, String) splits a document at its top level bookmarks: each chapter goes from the page of its bookmark to the page before the next chapter, and is saved in the folder as a file named after the bookmark (the characters that are not allowed in file names become '_', a repeated title gets a number: "Title (2).pdf"). The pages before the first chapter are saved as "Pages 1-N.pdf"; a document without bookmarks gives one file with all its pages. The methods that keep the documents in memory (SplitByBookmarksFromFileToMemory(String), SplitByBookmarksFromMemoryToMemory(Byte)) give the bookmark title in Title and the source page numbers in PageNumbers.
PDFSplitManager splitManager = new PDFSplitManager(); string[] files = splitManager.SplitByBookmarksFromFileToFolder(@"C:\Documents\manual.pdf", @"C:\Documents\Chapters"); foreach (PDFSplitDoc chapter in splitManager.SplitByBookmarksFromFileToMemory(@"C:\Documents\manual.pdf")) Console.WriteLine("{0}: {1} page(s)", chapter.Title, chapter.PageNumbers.Length);
The ExtractPages methods that take a page list string extract the listed pages into one new document, in the order of the list: page numbers start at 1 and are separated by commas, "a-b" is a range (taken backwards when a is greater than b), "a-" goes to the last page and "-b" starts with the first page. The overloads that take a PageSelection extract the odd or the even pages.
PDFSplitManager splitManager = new PDFSplitManager(); splitManager.ExtractPagesFromFileToFile(@"C:\Documents\report.pdf", @"C:\Documents\selected.pdf", "1-3,7,10-"); splitManager.ExtractPagesFromFileToFile(@"C:\Documents\report.pdf", @"C:\Documents\odd.pdf", PageSelection.OddPages); PDFSplitDoc even = splitManager.ExtractPagesFromFileToMemory(@"C:\Documents\report.pdf", PageSelection.EvenPages);
SplitBySizeFromFileToFile(String, String, Int64) splits a document into documents of consecutive pages that are each at most the given number of bytes - for example to stay under an e-mail attachment limit. Each document takes as many pages as fit, with the security and document information of the new documents included in the size. A page that is larger than the limit on its own makes a document of its own. The files are saved as <name>_1.pdf, <name>_2.pdf..., and the method returns their paths; SplitBySizeFromFileToMemory(String, Int64) keeps them in memory.
PdfDocumentInfo and PdfSecurityOptions apply to every new document. SourcePdfPassword opens a password protected source document.
PdfDocumentOptions options = new PdfDocumentOptions(); options.PdfCompressionLevel = PDFCompressionLevel.Best; PDFSplitManager splitManager = new PDFSplitManager(options); // the password of the source document (its user or owner password) splitManager.SourcePdfPassword = "source password"; // the new documents: document information, open password, no printing splitManager.PdfDocumentInfo.Title = "Report - first part"; splitManager.PdfDocumentInfo.AuthorName = "Sample Company"; splitManager.PdfSecurityOptions.UserPassword = "secret"; splitManager.PdfSecurityOptions.OwnerPassword = "owner"; splitManager.PdfSecurityOptions.CanPrint = false; splitManager.PdfSecurityOptions.KeySize = EncryptionKeySize.EncryptKey256Bit; splitManager.ExtractPagesFromFileToFile(@"C:\Documents\protected.pdf", @"C:\Documents\first-part.pdf", 1, 3);
The Samples (.NET Framework 4.6.2) and Samples_NET_10 (.NET 10 and .NET 11) folders have the same samples, in C# and VB.NET:
SplitPdfCommandLine - a command line tool: start page, split size, repeat count or repeat to the end, password of the source document; or split at the bookmarks, extract a page list or the odd/even pages, split by size.
SplitPdfFeatures - page ranges and chunks from and to files or memory, the page count, document information and security of the new documents, splitting a password protected document, one document per chapter, page lists, odd and even pages, split by size.
They split the 6 page document that comes with them and need no arguments.
The SplitPdfCommandLine sample is also a ready to use tool. The syntax is:
SplitPdfCommandLine /pdf:source_file [/startpage:number] [/splitsize:pages]
[/repeatcount:count] [/password:source_password] [/out:file]
SplitPdfCommandLine /pdf:source_file {/bookmarks | /pages:list | /odd | /even | /maxsize:KB}
[/password:source_password] [/out:file]By default the split starts with the first page, makes documents of 1 page and repeats to the end of the document. /bookmarks saves one document per chapter in the folder named after the out file; /pages, /odd and /even save one document with those pages; /maxsize saves documents of at most that many kilobytes. Example - two documents of 2 pages, from page 3:
SplitPdfCommandLine /pdf:"C:\My Documents\Report.pdf" /startpage:3 /splitsize:2 /repeatcount:2 /out:Part.pdf
SourcePdfPassword: split password protected documents.
PdfSecurityOptions and PdfDocumentInfo: security and document information of the new documents.
Split at the bookmarks, one document per chapter: SplitByBookmarksFromFileToFolder(String, String) and the other SplitByBookmarks methods.
Page lists and odd/even pages: the ExtractPages overloads with a page list string or a PageSelection.
Split by maximum file size: SplitBySizeFromFileToFile(String, String, Int64) and the other SplitBySize methods.
Title and PageNumbers on the documents kept in memory.
Version 10 is part of the ExpertPdf 22.0 release and needs a version 10 license key.
New samples for .NET Framework 4.6.2, .NET 10 and .NET 11, in C# and VB.NET.
A license key is emailed to you when you purchase the product. Set it with the LicenseKey property before you split documents. Without a license key the library adds an evaluation notice to the pages of the new documents.