How to Add, Copy, and Delete Pages in PDFs Using C#

Manipulating PDF Files with Iron PDF in C#

In this tutorial, we explore the process of adding, copying, and deleting pages in PDF files using the Iron PDF library in C#. We begin by installing Iron PDF through the NuGet package manager, followed by configuring the license key for its functionality. The tutorial demonstrates importing a cover page and a content document, merging them into a single PDF, and saving the result. We then cover inserting a cover page at the start of a document using the insert PDF method. Copying pages is next, with instructions on copying a single page or multiple pages from an existing PDF into a new document. The tutorial also explains the removal of pages using the remove page and remove pages methods, allowing for the deletion of specific pages. Throughout the tutorial, confirmation messages are printed to the console to indicate successful operations. By the end of the tutorial, viewers will be able to manipulate PDF documents efficiently, enhancing their document management capabilities. A call to action encourages viewers to subscribe for more tutorials and to try Iron PDF by signing up for a trial via a provided link.

// Ensure you have installed the IronPDF NuGet package, and replace "your-license-key" with your actual license key.

using IronPdf;
using System;

class PdfManipulationTutorial
{
    static void Main()
    {
        // Register your IronPDF license key
        IronPdf.License.LicenseKey = "your-license-key";

        // Load the cover page and content document
        var coverPage = PdfDocument.FromFile("cover.pdf");
        var contentDocument = PdfDocument.FromFile("content.pdf");

        // Merge the cover page and content document into a single PDF file
        var mergedDocument = PdfDocument.Merge(coverPage, contentDocument);
        Console.WriteLine("Successfully merged cover and content into a single PDF.");

        // Save the merged PDF to a file
        mergedDocument.SaveAs("mergedDocument.pdf");

        // Insert the cover page at the start of the content document
        contentDocument.InsertPdf(coverPage, 0);
        Console.WriteLine("Inserted cover page at the start of the content document.");

        // Save the new arrangement to a file
        contentDocument.SaveAs("contentWithCover.pdf");

        // Copy a single page from the content document and save it as a new document
        var singlePageCopy = contentDocument.CopyPage(1);
        singlePageCopy.SaveAs("singlePageCopy.pdf");
        Console.WriteLine("Copied page 1 to a new PDF document.");

        // Remove a specific page from the content document
        contentDocument.RemovePage(1); 
        Console.WriteLine("Removed page 1 from the document.");

        // Save the document after removing a page
        contentDocument.SaveAs("contentWithoutPage1.pdf");

        // Example of copying multiple pages and saving
        var multiplePagesCopy = contentDocument.CopyPages(new int[] { 2, 3 });
        multiplePagesCopy.SaveAs("multiplePagesCopy.pdf");
        Console.WriteLine("Copied pages 2 and 3 to a new PDF document.");

        // Example of removing specific pages
        contentDocument.RemovePages(new int[] { 2, 3 });
        Console.WriteLine("Removed pages 2 and 3 from the document.");

        // Save the document after removing the pages
        contentDocument.SaveAs("finalDocument.pdf");
    }
}
// Ensure you have installed the IronPDF NuGet package, and replace "your-license-key" with your actual license key.

using IronPdf;
using System;

class PdfManipulationTutorial
{
    static void Main()
    {
        // Register your IronPDF license key
        IronPdf.License.LicenseKey = "your-license-key";

        // Load the cover page and content document
        var coverPage = PdfDocument.FromFile("cover.pdf");
        var contentDocument = PdfDocument.FromFile("content.pdf");

        // Merge the cover page and content document into a single PDF file
        var mergedDocument = PdfDocument.Merge(coverPage, contentDocument);
        Console.WriteLine("Successfully merged cover and content into a single PDF.");

        // Save the merged PDF to a file
        mergedDocument.SaveAs("mergedDocument.pdf");

        // Insert the cover page at the start of the content document
        contentDocument.InsertPdf(coverPage, 0);
        Console.WriteLine("Inserted cover page at the start of the content document.");

        // Save the new arrangement to a file
        contentDocument.SaveAs("contentWithCover.pdf");

        // Copy a single page from the content document and save it as a new document
        var singlePageCopy = contentDocument.CopyPage(1);
        singlePageCopy.SaveAs("singlePageCopy.pdf");
        Console.WriteLine("Copied page 1 to a new PDF document.");

        // Remove a specific page from the content document
        contentDocument.RemovePage(1); 
        Console.WriteLine("Removed page 1 from the document.");

        // Save the document after removing a page
        contentDocument.SaveAs("contentWithoutPage1.pdf");

        // Example of copying multiple pages and saving
        var multiplePagesCopy = contentDocument.CopyPages(new int[] { 2, 3 });
        multiplePagesCopy.SaveAs("multiplePagesCopy.pdf");
        Console.WriteLine("Copied pages 2 and 3 to a new PDF document.");

        // Example of removing specific pages
        contentDocument.RemovePages(new int[] { 2, 3 });
        Console.WriteLine("Removed pages 2 and 3 from the document.");

        // Save the document after removing the pages
        contentDocument.SaveAs("finalDocument.pdf");
    }
}
$vbLabelText   $csharpLabel

Further Reading: How to Add, Copy, and Delete Pages in PDFs

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Add Images in C#
NEXT >
How to Convert PDF Files to Images in C#