How to delete an image from a PDF file in C#

With the help of Spire.PDF for .NET, we can easily add a new image to an existing PDF file. Now starts from Spire.PDF v3.5.80, we can delete an image via the index of image from a PDF file in C# easily. Firstly, check the original PDF file with an image at the page and the other image as background image.

How to delete an image from a PDF file in C#

The following code snippet shows how to delete an image from a PDF file:

Step 1: Create a new instance of PdfDocument and load a PDF document from file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

Step 2: Get the first page of the loaded PDF file.

PdfPageBase page=pdf.Pages[0];

Step 3: Delete an image via the index of image.

page.DeleteImage(1);

Step 4: Save the document to file.

pdf.SaveToFile("result.pdf");

Effective screenshot:

How to delete an image from a PDF file in C#

Full codes:

class Program
{
    static void Main(string[] args)
    {
        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromFile("Sample.pdf");
        PdfPageBase page=pdf.Pages[0];
        page.DeleteImage(1);
        pdf.SaveToFile("result.pdf");
    }
}