I have a image, that i like to use as a background on my pdf. My pdf is in LANDSCAPE, so the background image must fit into my landscape pdf.
How can i do that? This is my code, but the image doesnt show right in landscape :(
string imageFilePath = Server.MapPath(".") + "/images/test.jpg"; iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath); Document pdfDucment = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10); pdfDucment.SetPageSize(iTextSharp.text.PageSize.A4.Rotate()); jpg.ScaleToFit(1500, 1500); jpg.Alignment = iTextSharp.text.Image.UNDERLYING; jpg.SetAbsolutePosition(0,5);Answer1:
Please take a look at the <a href="http://itextpdf.com/sandbox/images/BackgroundImage" rel="nofollow">BackgroundImage</a> example and its <a href="http://itextpdf.com/sites/default/files/background_image.pdf" rel="nofollow">resulting pdf</a>.
Now let's take a look at your code and list all the errors.<ul><li>It is unclear what you expect as page size. First you create a document using a rectangle of 288 by 144 pt. You also define margins. However, you then change the page size to a rotated A4 page. As your code snippet doesn't show when you actually open the document, it's hard to tell what the page size will be. If
Open()is invoked after changing the page size to A4, it will be A4. If it's triggered before changing the page size, it will be 288 by 144.</li> <li>You resize the image using the
ScaleToFit()method. You want the image to fit inside a square of 1500 by 1500 pt. If your image is also a square, the image will indeed be rescaled to 1500 by 1500 pt. However, if the image is a rectangle, it will be smaller than 1500 by 1500, because
ScaleToFit()preserves the aspect ratio while trying to fit the image inside the given dimensions. In my example, I use absolute scaling. Sure, this may distort your image, but then again: you want the image to cover the complete page.</li> <li>You are adding the image using
Image.UNDERLYING, but what if there are other images in your document? How will you force the image being added underneath them and not covering them? The safest way to do this, is by adding the image as direct content to the layer under the regular content.</li> <li>Finally, you use
0, 5as absolute position. Why not
0, 0? Do you want to see a margin? If so, how can you assure that the margin is 5 pt on each side of the page?</li> </ul>
If you fix these errors one by one, using my Java example as inspiration, you'll be able to solve your problem.