Image Element |
The image element adds images to a PDF document and is implemented by the ImageElement class. The image is given as the path of an image file or as the bytes of an image file (a byte[]), in one of the PNG, JPEG, GIF, BMP, TIFF and WebP formats. The ImageElement offers many constructors that call the following two constructors with default values:
public ImageElement(float x, float y, float destWidth, float destHeight, string filePath); public ImageElement(float x, float y, float destWidth, float destHeight, byte[] imageObj);
The first constructor creates an ImageElement from the specified file, rendered at the position (x,y) with the (destWidth,destHeight) size.
The second constructor creates an ImageElement from the bytes of an image file, rendered at the position (x,y) with the (destWidth,destHeight) size. The bytes can come from a file, a database, a web request or an image produced by another part of the library, like the HTML to image converter.
x - The X location where this element is rendered.
y - The Y location where this element is rendered.
destWidth - The destination rectangle width.
destHeight - The destination rectangle height.
filePath - The image file path.
imageObj - The bytes of the image file.
The constructors without a width use the width available in the page, and the constructors without a height determine the height from the width so the image keeps its aspect ratio. An image taller than the room left on the page continues on the next pages while Paginate is true. The frames of a multi-page TIFF or of an animated GIF or WebP are each rendered on a page of their own while RenderMultiFrame is true, and RenderTransparentImage keeps or removes the transparency of the image. The images are JPEG-compressed as set by the JpegCompressionEnabled and JpegCompressionLevel properties of the document; transparent and very small images are kept lossless.
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 imagePath = Path.Combine("Images", "logo.png"); // display the image in the available space in the page, with the height determined // automatically to keep the aspect ratio ImageElement imageElement1 = new ImageElement(0, 0, imagePath); AddElementResult addResult = firstPage.AddElement(imageElement1); // display the image with the specified width and the height determined // automatically; the image is placed below the previous image, whose bounds inside // the page are taken from the AddElementResult object ImageElement imageElement2 = new ImageElement(0, addResult.EndPageBounds.Bottom + 10, 100, imagePath); addResult = firstPage.AddElement(imageElement2); // display the image from bytes, with the specified width and height; the image does // not keep its aspect ratio byte[] imageBytes = File.ReadAllBytes(imagePath); ImageElement imageElement3 = new ImageElement(addResult.EndPageBounds.Right + 10, addResult.EndPageBounds.Top, 200, 100, imageBytes); addResult = firstPage.AddElement(imageElement3);