Table of Contents

Splitting & Merging

PdfDocument exposes page-level operations for splitting a PDF apart, extracting individual pages or page ranges, and merging documents back together. The API is identical in both the Chromium (CobaltPDF) and WebKit (CobaltPDF.WebKit) libraries.

These operations are pure in-process post-processing — no browser is involved. That means they work on any PDF, including files CobaltPdf never produced, on any OS. Pages are deep-copied with their resources (fonts, images, vector content), so extracted pages render identically to the original.

Note

This is distinct from the render-time pageRanges option, which selects which pages of the HTML get rendered. The methods on this page operate on the finished PDF after rendering (or on any PDF loaded from disk).

API Reference

Member Description
PageCount Number of pages in the document
ExtractPage(int pageIndex) Extract a single page (0-based index)
ExtractPages(int startIndex, int endIndex) Extract an inclusive range of pages
ExtractPages(IEnumerable<int> pageIndices) Extract an arbitrary subset of pages
SplitIntoPages() Split into an IReadOnlyList<PdfDocument>, one document per page
static FromFile(string path) Load any existing PDF from disk
static FromBytes(byte[] data) Load any existing PDF from a byte array
static Merge(params PdfDocument[] documents) Combine multiple documents into one

Page indices are 0-based. Out-of-range indices throw ArgumentOutOfRangeException; an empty selection throws ArgumentException.

Render, Then Split Into One File Per Page

var pdf = await renderer.RenderHtmlAsPdfAsync(html);

for (int i = 0; i < pdf.PageCount; i++)
    pdf.ExtractPage(i).SaveAs($"page_{i + 1}.pdf");

Or with SplitIntoPages:

var pdf   = await renderer.RenderUrlAsPdfAsync("https://example.com/report");
var pages = pdf.SplitIntoPages();   // IReadOnlyList<PdfDocument>, one per page

for (int i = 0; i < pages.Count; i++)
    pages[i].SaveAs($"report_page_{i + 1}.pdf");

Extract a Page Range

var pdf = await renderer.RenderUrlAsPdfAsync("https://example.com/contract");

// Pages 2-4 (0-based, inclusive range)
var excerpt = pdf.ExtractPages(1, 3);
excerpt.SaveAs("contract-excerpt.pdf");

// Arbitrary subset — first, third, and last page
var subset = pdf.ExtractPages(new[] { 0, 2, pdf.PageCount - 1 });
subset.SaveAs("contract-summary.pdf");

Merge Several PDFs

var cover    = await renderer.RenderHtmlAsPdfAsync(coverHtml);
var report   = await renderer.RenderUrlAsPdfAsync("https://example.com/report");
var appendix = PdfDocument.FromFile("appendix.pdf");   // not rendered by CobaltPdf — that's fine

var combined = PdfDocument.Merge(cover, report, appendix);
combined.SaveAs("complete-report.pdf");

Load a PDF From Disk and Split It

FromFile and FromBytes load any existing PDF — it does not need to have been produced by CobaltPdf:

var pdf = PdfDocument.FromFile("multipage.pdf");

Console.WriteLine($"{pdf.PageCount} pages");

var pages = pdf.SplitIntoPages();
for (int i = 0; i < pages.Count; i++)
    pages[i].SaveAs($"split_{i + 1}.pdf");
Tip

To render only specific pages of the HTML in the first place, use the render-time pageRanges option instead — these page operations are for slicing up the finished PDF.