Click or drag to resize
Pdf Library for .NET

Pdf Image Element

Images can be added to a pdf document using PdfImageElement objects. Here are some of the setting that can be used to customized how an image will look in pdf:

Position of the image element in the destination pdf page can be customize using X and Y properties.

Size of the image in the destination page can be customized using Width and Height properties.

Image object - the underlying system image object can be customized using Image property. The image can be specified as a file name if a constructor with the specific parameter is used: PdfImageElement(Single, Single, String)

Transparency can be customized using Transparency property.

Sample Code

Sample code that shows how to create image elements with different settings. Read the comments from the code for customization details.

// create a new pdf document
PdfDocument doc = new PdfDocument();

// add a new page to the document
PdfPage page = doc.AddPage();

// get image path
string imgFile = Server.MapPath("~/files/logo.png");

// define a rendering result object
PdfRenderingResult result;

// create image element from file path with real image size
PdfImageElement img1 = new PdfImageElement(0, 0, imgFile);
result = page.Add(img1);

// create image element from system Image object with fixed size in pdf 
// (aspect ratio not preserved)
System.Drawing.Image img = System.Drawing.Image.FromFile(imgFile);
PdfImageElement img2 = new PdfImageElement(0, 
    result.PdfPageLastRectangle.Bottom + 50, 100, 100, img);
result = page.Add(img2);

// add text element
PdfFont font1 = doc.AddFont(PdfStandardFont.Helvetica);
font1.Size = 10;
PdfTextElement text1 = new PdfTextElement(0, 
    result.PdfPageLastRectangle.Bottom + 50, Helper.SomeText(), font1);

// add image over text (no transparency)
PdfImageElement img3 = new PdfImageElement(0, 
    result.PdfPageLastRectangle.Bottom + 50, imgFile);
result = page.Add(img3);

// add image over text (with transparency) next to the previous image
PdfImageElement img4 = new PdfImageElement(
    result.PdfPageLastRectangle.Right + 30, 
    result.PdfPageLastRectangle.Top, imgFile);
img4.Transparency = 50;
result = page.Add(img4);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
See Also