iTextSharp – Adding Images


Maple leaves in Autumn.

Last week I showed how to use form fields to control placement of dynamic data.

But what if you want to dynamically place images in your PDF?  You can stuff them into a form field like you can with text.

However, one of the items you can retrieve from the form field is its location on the screen.  Using this, a little math, and some iTextSharp image code, we can place images in our PDF where the form field was located.  Here’s how I do it.

First, this code builds on the previous code I’ve already demonstrated in previous articles.  If this is your first time here, you’ll want to scroll to the bottom of this post where it says, “Other Posts in iTextSharp” and read them first.

To retrieve the position of the form field, you’ll need to call the field’s GetFieldPositions() method.  It is plural because it is going to get the position of every field in the document with the name you specify.  For our purposes, we will assume that the field only exists once and is on the first page.

float[] topImagePosition = null;
topImagePosition =
    fields.GetFieldPositions("m_topPicture");

The float array that is returned has five elements:

  • Page Number (starting at 1)
  • Left
  • Bottom
  • Right
  • Top

 

If the page rotation is 90 degrees, you’ll want to rotate the position information.  You can check the rotation using

rotation = pdfReader.GetPageRotation(1);

Where GetPageRotation() takes a parameter representing the page number, starting at 1.

You can rotate the positions using this code.

left = topImagePosition[1];
right = topImagePosition[3];
top = topImagePosition[4];
bottom = topImagePosition[2];
if (rotation == 90)
{
    left = topImagePosition[2];
    right = topImagePosition[4];
    top = pageSize.Right - topImagePosition[1];
    bottom = pageSize.Right - topImagePosition[3];
}

Next, you’ll want to retrieve the image you want to display on the PDF.  There are APIs for retrieving the image from a web site, but most of the time you’ll be retrieving the image from your hard drive.  Here is the code to do that.

iTextSharp.text.Image topImage =
    iTextSharp.text.Image.GetInstance(
    Server.MapPath(imagelocation +
    productRow.imagefilename));

Before we place the image on the page, the next thing you’ll want to do is scale the image so that it fits in the rectangle

topImage.ScaleToFit(right - left, top - bottom);

And then we position it

topImage.SetAbsolutePosition(
    left + ((right - left) - topImage.ScaledWidth) /
    2, top - topImage.ScaledHeight);

and then we put it into the PDF

PdfContentByte contentByte = stamp.GetOverContent(1);
contentByte.AddImage(topImage);

Notice the contextByte thing.  That’s the critical part of how we get the images into the PDF.

Finally, you’ll want to remove the field from the form so that it doesn’t show up.

fields.RemoveField("m_topPicture");

I’m not sure this is really needed, but it doesn’t hurt to clean up things you no longer need.

Ads by Lake Quincy Media

Other Related Items:

Beltronics V955 Vector High Performance Radar Detector (Black/Silver)Beltronics V955 Vector High Performance Radar Detector (Black/Silver)With the best radar performance under $200, drive safely and save money with the Vector 955. Includes features usually found on more expensive detecto... Read More >
Well behaved women rarely make history - funny bumper stickers (Medium 10x2.8 in.)Well behaved women rarely make history - funny bumper stickers (Medium 10x2.8 in.)Size: Medium 10 x 2.8 inches. Perfect for your car, truck, motorhome, office cubicle, etc.
Adobe Photoshop CS4 Classroom in a BookAdobe Photoshop CS4 Classroom in a BookFourteen lessons in Adobe Photoshop CS4 Classroom in a Book cover basic and advanced techniques in Adobe Photoshop, the world's best image-editing sof... Read More >

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

Comments are closed.

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor