We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Image Not found in XImageCollection, Image Replacement Fails


I have been trying to achieve image replacement using aspose PDF for .Net and I have been unsuccesful in all ways. I have tried both approaches using Aspose.Pdf.Facades and Aspose.Pdf yet no success. Below Attaching snippet for image replacement and the concerned pdf for the same.

Using Aspose.Pdf:

            byte[] ImageBytes = System.Convert.FromBase64String(ImageDataUrl);
            MemoryStream ms = new MemoryStream(ImageBytes );
            doc.Pages[1].Resources.Images.Replace(1, ms); 

the above snippet fails as it is not able to locate the images in XImageCollection , although I verified the pdf across various editors and the image is detected well.

Using Aspose Pdf Facades:

            byte[] ImageBytes = System.Convert.FromBase64String(ImageDataUrl);
            File.WriteAllBytes("temp.png", ImageBytes );
            pdfContentEditor.ReplaceImage(1, 1, "temp.png");

The above code does not fail but results in the output with no image replacement.

Certificate1d.pdf (428.5 KB)


We managed to replicate the problem of not being able to replace images in our environment. An investigation has been logged under the ticket ID PDFNET-44920 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Any update on this?. I need this urgently. Even a workaround would help.



Aspose.PDF API does not recognize images in the PDF document. As a workaround, you can place an image at the same position with the help of a floating box. Please try the code as follows:

string dataDir = @"C:\Pdf\test829\";
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
//Create a floating box to contain the image
Aspose.Pdf.FloatingBox imagebox = new FloatingBox();
//Set the width/height of the floating box(container)
//The same as the image’s width and height
imagebox.Width = 200;
imagebox.Height = 300;
//Positioning the box
imagebox.Left = 100;
imagebox.Top = 250;
//Add the image into the box
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
img.File = "C:/pdf/test832/icon.png";
img.FileType = ImageFileType.Unknown;
Aspose.Pdf.Page page = pdf.Pages.Add();

Thank you for the quick reply but I actually need an image replacement code only as the image placement will be dynamic. I have tried multiple editors to create pdf. Could you direct me to which pdf editor would be the best for the same.


We are afraid there may not be any workaround at the moment. We will be able to share any workaround or update as soon as the ticket will be investigated. Please be patient and spare us little time.