Guide
How to insert, append, and delete PDF pages in C#
Use AppendPageAsync(...) to add pages to the end, InsertPageAsync(...) to place a
page at a specific 1-based position, and DeletePageAsync(...) to remove one.
The main thing to remember is that page positions are 1-based, and insert/delete operations shift the page numbers that come after them.
Append, insert, then delete
The three methods can be mixed in the same edit path before you save.
using ZingPDF;
using ZingPDF.Syntax.CommonDataStructures;
using var pdf = Pdf.Load(File.OpenRead("input.pdf"));
await pdf.AppendPageAsync(options =>
{
options.MediaBox = Rectangle.FromDimensions(595, 842);
});
await pdf.InsertPageAsync(1, options =>
{
options.MediaBox = Rectangle.FromDimensions(595, 842);
});
await pdf.DeletePageAsync(2);
await pdf.SaveAsync(File.Create("pages-updated.pdf"));
Append adds to the end, insert uses a 1-based page number
Use AppendPageAsync(...) when page order is simple and the new page belongs at the end of the
document.
Use InsertPageAsync(pageNumber, ...) when you need a cover page, separator page, or a new page
in the middle of an existing document.
Both methods return the new Page, so you can write content to it before saving.
Delete a page after the document is in the right order
DeletePageAsync(pageNumber) removes the requested page and shifts later page numbers down.
If you are doing several structure edits at once, it is usually easier to get the insert/append order right first and then delete anything you no longer want.
Choose the size of the new page when you create it
Both append and insert accept page creation options, so set the media box as you create the page instead of fixing it afterward.
That is useful when the document mixes cover pages, inserts, and imported content with different page sizes.
Need to export or split pages after reordering them?
Once the page order is right, the next step is often to save only a subset of pages or break the document into smaller files.