Backgroundimage in landscape and cover whole pdf with iTextSharp

感情迁移 提交于 2019-12-12 01:05:15

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/21731027/backgroundimage-in-landscape-and-cover-whole-pdf-with-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!