Click or drag to resize

Image Element

The Image Element allows adding images to a PDF document and is implemented by the ImageElement class. The ImageElement offers many constructors that basically call the following two constructors with more or less default values:

public ImageElement(float x, float y, float destWidth, float destHeight, string filePath);
public ImageElement(float x, float y, float destWidth, float destHeight, System.Drawing.Image imageObj);

The first constructor creates an ImageElement from the specified file that will be rendered at the position (x,y) with the (destWidth,destHeight) size.

The second constructor creates an ImageElement from the specified System.Drawing.Image object that will be rendered at the position (x,y) with the (destWidth,destHeight) size.

  • x - The X location where this element will be rendered.

  • y - The Y location where this element will be rendered.

  • destWidth - The destination rectangle width.

  • destHeight - The destination rectangle height.

  • filePath - The image file path.

  • imageObj - The System.Drawing.Image object.

Sample Code

Below there is a sample code showing how to add images to a PDF document:

 // add a page to the PDF document
PdfPage firstPage = document.AddPage();

string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), "Images");

// display image in the available space in page and with a auto determined height to keep the aspect ratio
ImageElement imageElement1 = new ImageElement(0, 0, System.IO.Path.Combine(imagesPath, "html-to-pdf-box-250.PNG"));
AddElementResult addResult = firstPage.AddElement(imageElement1);

// display image with the specified width and the height auto determined to keep the aspect ratio
// the images is displayed to the right of the previous image and the bounds of the image inside the current page
// are taken from the AddElementResult object
ImageElement imageElement2 = new ImageElement(addResult.EndPageBounds.Right + 10, 0, 100, 
        System.IO.Path.Combine(imagesPath, "html-to-pdf-box-250.PNG"));
addResult = firstPage.AddElement(imageElement2);

// Display image with the specified width and the specified height. It is possible for the image to not preserve the aspect ratio 
// The images is displayed to the right of the previous image and the bounds of the image inside the current page
// are taken from the AddElementResult object
ImageElement imageElement3 = new ImageElement(addResult.EndPageBounds.Right + 10, 0, 200, 100, 
        System.IO.Path.Combine(imagesPath, "html-to-pdf-box-250.PNG"));
addResult = firstPage.AddElement(imageElement3);