81628

Backgroundimage in landscape and cover whole pdf with iTextSharp

Question:

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, 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?</li> </ul>

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

Recommend

  • How to prevent imageView of UIButton from taking up entire UIButton?
  • iText - adding Image element generates a corrupt PDF file
  • Image scaling does not work when original image height & width is smaller the scaling height &am
  • Is libpq more like a JDBC driver or JavaSE's java.sql package?
  • cannot use raw_input if monkeyrunner is included in python script
  • Three.js - how can I update an arrowHelper?
  • adding a legend to a scatterplot with fitted line and prediction interval
  • TinyXML - any way to skip problematic DOCTYPE tag?
  • Can someone explain how Postgresql roles, Postgresql users and Linux users relate to each other?
  • How to add a fadeIn effect while changing background image using .css in Jquery
  • Problem with sockets and OutOfMemory error
  • jQuery .load() but exclude script tags
  • Why does my form throw an OutOfMemory exception while trying to load image?
  • draw9patch just hangs on Mac 10.7.5
  • Android SAXParser, parse into array and get child nodes
  • getting an Int/short/byte structure byte representation with C#
  • What is equivalent of VB6.0's MaskColor property in C#
  • Difference between File>Close and :close in Vim
  • How can i create a big file in google drive with google app script?
  • How to add header and Footer to each activity in android
  • C Pointer confusion
  • jQuery & CSS - Cut text by height, no truncate
  • How does sizeof work for int types?
  • Aligning a Stack pointer 8 byte from 4 byte in ARM assembly
  • How to convert ImageBox.Image to Image
  • Is storing an OAuth token in cookies bad practise?
  • How can I encode a filename according to RFC 2231?
  • Underlying type for Tuple in Swift
  • How to correctly get a WinForms Button control to draw custom text
  • C++ String tokenisation from 3D .obj files
  • System call time out?
  • Scala split a multi line string by lines that contain all hyphens
  • Insert records if not exist SQL Server 2005
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • How to create a Unix-domain socket with specific access permissions
  • GAE: Way to get reference to an HttpSession from its ID?
  • Cannot upload to OneDrive using the new SDK
  • Moving mysql files across servers
  • Spray.io: When (not) to use non-blocking route handling?
  • Cant find why the layout is getting smaller