# Extract pages from a PDF in C#

> Copy a page range or a list of pages from a PDF into a new file in .NET, in any order. Shared resources are copied once. MIT licensed.

Available since 1.0.

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

## Short answer

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.

```csharp title="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");
```

```bash
dotnet add package PdfNetStack
```

## Notes

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.

## Related

- [Edit pages](https://www.jadev-corp.com/guides/edit-pages/)
- [Command line](https://www.jadev-corp.com/cli/): `pdfnetstack extract in.pdf out.pdf 1-3,7,9-` takes ranges in any order, repeats allowed
- API: [PdfPageCollection](https://www.jadev-corp.com/api/pdfnetstack/pdfpagecollection/)

Source: https://www.jadev-corp.com/pdfnetstack/recipes/extract-pdf-pages/
