Working with Images

Delete Images from a Particular Page of PDF (Facades)

PdfContentEditor class allows you to replace image in an existing PDF file. The replaceImage method helps you achieve this goal. You need to create an object of PdfContentEditor class and bind the input PDF file using bindPdf method. After that, you need to call replaceImage method with three parameters: a page number, index of the image to replace, and path of the image to be replaced.

The following code snippet shows you how to replace an image in an existing PDF file.

public class PdfContentEditorImages {

    private static String _dataDir = "/home/aspose/pdf-examples/Samples/facades/PdfContentEditor/";

    public static void DeleteImage()
    {
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "sample.pdf"));
        editor.deleteImage(2, new int [] { 1,3 });
        editor.save(_dataDir + "PdfContentEditorDemo10.pdf");
    }

Delete All the Images from a PDF File (Facades)

All the images can be deleted from a PDF file using deleteImage method of the PdfContentEditor. Call the deleteImage method – the overload without any parametes – to delete all the images, and then save the updated PDF file using Save method.

   public static void DeleteImages()
    {
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "sample.pdf"));
        editor.deleteImage();
        editor.save(_dataDir + "PdfContentEditorDemo11.pdf");
    }

Replace Images in PDF File (Facades)

You may replace images in PDF file using replaceImage method of the PdfContentEditor.

   public static void ReplaceImage()
    {
        PdfContentEditor editor = new PdfContentEditor(new Document(_dataDir + "sample_cats_dogs.pdf"));
        editor.replaceImage(2, 4, _dataDir+"cat04.jpg");
        editor.save(_dataDir + "PdfContentEditorDemo12.pdf");
    }