Recursive AI

Add a blank page to an existing PDF in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use report.pdf from the download or your own PDF.

Terminal
dotnet add package RecursiveAI.OnePdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf;
 
using var doc = PdfDocument.Open("report.pdf",
    new() { AllowEditing = true });
doc.InsertBlankPage(1, 612, 792);
doc.Save("out.pdf");
Console.WriteLine($"Pages: {doc.PageCount}");

Unzip the example, then dotnet run.

Writes out.pdf with a blank US Letter page between “Report: Summary” and “Report: Details,” the two pages in the sample. US Letter is 612 × 792 points; A4 is approximately 595 × 842 points.

Text
Pages: 3

If you omit AllowEditing = true, InsertBlankPage throws InvalidOperationException.

Variations

Append a blank page the size of the final page. Replace the InsertBlankPage line with this. MediaBox is the page’s physical boundary, before any crop or rotation.

C#
var size = doc.Pages[^1].MediaBox;
doc.InsertBlankPage(doc.PageCount, size.Width, size.Height);

The short answer

Open the existing PDF with AllowEditing = true, call InsertBlankPage(index, width, height), then save to a new file. The index is zero-based and the dimensions are in PDF points. There’s no second PDF to create or import: OnePDF builds the empty page directly in the document you opened.

How do I add a blank page to an existing PDF at a specific position?

Give InsertBlankPage the index you want the new page to occupy, not the index of the page it should follow. In a two-page report, index 0 puts the blank page before both originals, 1 puts it between them, and 2 puts it after both. The original pages stay in the same order. For a file of any length, use doc.PageCount rather than the last page’s index to append.

An index below zero or beyond doc.PageCount throws ArgumentOutOfRangeException. If you’ve already read doc.Pages, take it again after inserting: the page operations guide notes that the earlier page objects are out of date. This matters when you keep editing the PDF before saving it.

How do I create a blank PDF page that matches the other pages?

InsertBlankPage doesn’t copy a neighboring page’s size automatically. The snippet uses 612 × 792 points, the size of the two sample pages. To match the physical size of a page in your own file, take its MediaBox.Width and MediaBox.Height as in the append variation. Both dimensions have to be finite and greater than zero.

If a neighboring page has been cropped or rotated, decide whether to match the paper or what you see on screen. The page geometry guide distinguishes the physical MediaBox, the visible CropBox, and rotation. Reading the MediaBox size copies neither the crop nor the rotation. It also doesn’t copy the page’s text or graphics: the inserted page has no content of its own.

All onepdf recipes