C#/VB.NET: Delete Pages from PDF

2022-06-13 01:50:00 Written by  support iceblue
Rate this item
(0 votes)

A PDF document can contain multiple pages with different text content, images, or other objects. Occasionally, users may need to delete certain pages with incorrectly drawn objects or pages that are irrelevant to the topic of the document. This article will demonstrate how to programmatically delete/remove pages from an existing PDF document using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Delete/Remove Pages from PDF

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Get the pages in the PDF document using PdfDocument.Pages property.
  • Delete a specified page by index using PdfPageCollection.RemoveAt(int index)method.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RemovePage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument document = new PdfDocument();

            //Load a sample PDF document
            document.LoadFromFile(@"E:\Files\input.pdf");

            //Remove the second page
            document.Pages.RemoveAt(1);

            //Save the result document
            document.SaveToFile("RemovePDFPage.pdf");
        }
    }
}

C#/VB.NET: Delete Pages from PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 20 June 2023 01:54