Free Support Forum -

Replac Eimages

Currently I am replacing images in the PDF with images from my DB by using this code

Image oldImage = (Image)pdf.GetObjectByID(id);
Cell cell = (Cell)pdf.GetObjectByID(“cell” + id);
Image newImage = Image.FromSystemImage(image);

cell.Paragraphs.Insert(oldImage, newImage);

I was wondering if there is a way to, skip the cell and just replace the
image directly since sometimes I would have images that are not in a cell
or maybe there is a way to figure out which containers is the image in


Hi Fernando,

Are you replacing the image in an existing PDF file? Could you please share which component are you using to replace the image?

We’re sorry for the inconvenience and looking forward to help you out.

I am using Aspose.PDF.Generator.

The PDF is being created from an xml doc that I have attached here, and the previous image has a hard coded path so I am replacing it with one from my DB

Hi Fernando,

Thanks for your interest in our products.

Working with objects placed inside Aspose.Pdf.Generator DOM(Document Object Model) is very convenient. In case you need to replace the image specified inside your XML file with some other image file placed over your system or stored inside DataBase, you can simply get the Image object using Section.GetObjectByID(....) method and it will give you direct access to the Images placed inside the whole Section object. Please take a look over the following code snippet in which I have replaced the image path with some value at runtime. For your reference, I have also attached the resultant PDF that I have generated.


//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
pdf1.BindXML(@"d:/pdftest/XMLFile1.xml", null);
//Get the section and then Image from the obtained section of the Pdf that
//is built from the XML template
Section sec1 = pdf1.Sections["MainPage"];
Aspose.Pdf.Generator.Image img = sec1.GetObjectByID("HeaderImage") as Aspose.Pdf.Generator.Image;
// specify new path information for Image object
img.ImageInfo.File = @"d:/pdftest/aspose-logo.png";
// specify the image file type
img.ImageInfo.ImageFileType = ImageFileType.Png;
//save the resultant PDF

I would recommend you to please visit the following link for more information on how to Use Xml as Template

Thanks this worked for me, I just had to replace the System image instead of the file since I am using image streams.