iTextSharp – Adding Images
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);
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.
Other post in iTextSharp
- PDFs Using iTextSharp - June 17th, 2009
- iTextSharp – The easy way - June 24th, 2009
- iTextSharp – Adding Images - June 30th, 2009
- iTextSharp – HTML to PDF – Positioning Text - July 8th, 2009
- iTextSharp – HTML to PDF - Prerequisites - July 14th, 2009
- iTextSharp – PDF to HTML – Cleaning HTML - July 20th, 2009
- iText IN ACTION – Creating and Manipulating PDF - July 24th, 2009
- iTextSharp – HTML to PDF – Parsing HTML - July 28th, 2009
- iTextSharp – HTML to PDF – Writing the PDF - August 4th, 2009
- iTextSharp – HTML to PDF – Finishing Up - August 12th, 2009
- iTextSharp Tables - September 17th, 2009
Other Related Items:
Fujitsu ScanSnap S1500 Instant PDF Sheet-Fed Scanner for PCThe ScanSnap S1500 provides Windows users an effective way to greatly reduce paper clutter, storage space, and security risk associated with unmanaged... Read More >
Kidsongs - A Day at Old MacDonald's Farm [VHS]From the first cock-a-doodle-doo to the last twinkling little star, A Day at Old MacDonald's Farm pulses joyfully with song, dance, and a barnyard ful... Read More >
Apple iPod touch 8 GB (3rd Generation) NEWEST MODELiPod touch is a great iPod, a great pocket computer, and a great portable game player. Listen to a mix of songs automatically put together by the new ... Read More >
If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!









