Docs / Guides

Merging, splitting and rotating

Open existing PDFs with PdfReader and recombine their pages into new documents: merge files, pull out pages, change their order and rotation, or stamp new content on top.

Opening existing files

PdfReader.Open and FromBytes read classic cross-reference tables, cross-reference streams, object streams and incremental updates. Damaged files are repaired by scanning for objects when possible; WasRepaired tells you it happened and Warnings lists what was worked around.

Merging and extracting

PdfDocument.Merge combines documents in order. PdfDocument.Extract copies selected pages, in any order, repeats allowed. The result is a normal PdfDocument, so you can rotate and reorder pages before saving.

Program.cs
using SoftZenPdf;

var report = PdfReader.Open("report.pdf");
var appendix = PdfReader.Open("appendix.pdf");

var combined = PdfDocument.Merge(report, appendix);
combined.Pages[^1].Rotation = 90;
combined.MovePage(combined.Pages.Count - 1, 0);
combined.Save("combined.pdf");

PdfDocument.Extract(report, [2, 0]).Save("pages-3-and-1.pdf");

Splitting into single pages

Program.cs
using SoftZenPdf;

var source = PdfReader.Open("report.pdf");
for (var i = 0; i < source.PageCount; i++)
    PdfDocument.Extract(source, [i]).Save($"report-page-{i + 1}.pdf");

Rotating and reordering

page.Rotation accepts any multiple of 90 degrees and tells viewers how to turn the page; negative values are normalised, so -90 becomes 270. document.MovePage(from, to) moves a page, shifting the others. There is no method to delete a page: build a new document with Extract and the pages you want to keep.

Adding content to existing pages

ImportPages appends pages to a document you are building. Anything drawn on an imported page's Content is placed on top of the original, which is how you add page numbers, stamps or annotations:

Program.cs
using SoftZenPdf;

var source = PdfReader.Open("report.pdf");
var document = new PdfDocument();
var pages = document.ImportPages(source);

for (var i = 0; i < pages.Count; i++)
    pages[i].Content.DrawText($"Page {i + 1} of {pages.Count}", pages[i].Width - 110, 24, StandardFont.Helvetica, 9, PdfColor.Gray);

document.Save("report-numbered.pdf");

What is and is not carried over

  • Carried over: each page's content, resources (fonts, images), size, crop box, rotation and annotations. Fonts and images shared between pages of one source are copied once.
  • Not carried over: bookmarks, the interactive form structure, named destinations and the document metadata of the sources. Links to pages that were not copied stop working.

Encrypted sources must be opened with the owner password, or permit both copying and assembly; otherwise Merge, Extract and ImportPages throw UnauthorizedAccessException. See Encryption and permissions.

API: PdfDocument.Merge, PdfDocument.Extract, PdfDocument.ImportPages, PdfReader.