Skip to content

Extract pages from a PDF in C#

Since 1.0View as Markdown

Copy some pages of a PDF into a new document, in the order you choose.

Create an empty document with PdfDocument.Create, then import the pages you want with Pages.Import. Page numbers in the sample start at 1. Fonts and images shared between the pages are copied once.

recipe-extract-pdf-pages.cs
#:package PdfNetStack@1.2.0
using PdfNetStack;
PdfDocument source = PdfDocument.Open("manual.pdf");
int[] keep = [1, 2, 5]; // page numbers, from 1, in the order you want them
PdfDocument extract = PdfDocument.Create();
extract.Pages.Import(keep.Select(n => source.Pages[n - 1]));
extract.Save("manual-extract.pdf");
Console.WriteLine($"manual-extract.pdf: {extract.Pages.Count} pages");
Terminal window
dotnet add package PdfNetStack

Annotations are copied fresh for every import. Links to pages imported in the same call are remapped, and links to pages left behind become empty.