0

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
KEVIN
  • 155
  • 2
  • 13
  • *the image doesnt show right* - what exactly do you mean, in which way is it wrong? – mkl Feb 12 '14 at 15:00
  • Its not full screen in landscape – KEVIN Feb 12 '14 at 15:02
  • Your code is wrong in many places. (It's as if you've never read any documentation about iTextSharp.) I'm making you an example to show you how it's done. Give me a minute or five. – Bruno Lowagie Feb 12 '14 at 15:04

1 Answers1

0

Please take a look at the BackgroundImage example and its resulting pdf.

Now let's take a look at your code and list all the errors.

  • 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.
  • 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.
  • 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.
  • Finally, you use 0, 5 as 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?

If you fix these errors one by one, using my Java example as inspiration, you'll be able to solve your problem.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Latest link for this BackgroundImage: https://itextpdf.com/en/resources/examples/itext-7/background-images – S Kumar Oct 22 '19 at 07:19