Split a PDF into one file per page, or into chosen page ranges, in C# with OnePDF. Bookmarks, links and form fields go with their pages.
using OnePdf; using PdfDocument doc = PdfDocument.Open("report.pdf"); // One file per page. IReadOnlyList<PdfDocument> pages = PdfDocument.Split(doc, pagesPerPart: 1); for (int i = 0; i < pages.Count; i++) { using PdfDocument part = pages[i]; part.Save($"page-{i + 1}.pdf"); } // Chosen ranges: pages 1-2 and pages 3-6. PdfPageRange takes a zero-based start and a count. IReadOnlyList<PdfDocument> sections = PdfDocument.Split(doc, [new PdfPageRange(0, 2), new PdfPageRange(2, 4)]); string[] names = ["intro.pdf", "body.pdf"]; for (int i = 0; i < sections.Count; i++) { using PdfDocument part = sections[i]; part.SetInfo("Title", $"Harbor trial report, {Path.GetFileNameWithoutExtension(names[i])}"); part.Save(names[i]); Console.WriteLine($"{names[i]}: {part.PageCount} pages, {part.Outline.Count} bookmarks"); }
intro.pdf: 2 pages, 2 bookmarks body.pdf: 4 pages, 4 bookmarks
The run also wrote page-1.pdf to page-6.pdf, one page each.
Split(doc, pagesPerPart: n)cuts the document into parts ofnpages, the last one possibly shorter.Split(doc, ranges)makes one document perPdfPageRange(start, count), with a zero-based start. To extract pages, pass a single range.- Each part is a new document in memory that doesn’t refer to the source. Dispose each one after saving it.
- A part keeps the bookmarks that lead to its pages, the links between its own pages and the form fields whose widgets are on them.
- Parts carry no metadata from the source: set a title with
SetInfoas the sample does. - A resource dictionary that every page shares, common in slide decks, is copied whole into each part, so parts of such a file can be larger than their share of the original.
To put files together, see Merge PDFs. The rules are in Page operations.
A longer walk-through, into single pages, page ranges, fixed-size parts or one file per bookmark: Split a PDF in C#.