iTextSharp将图像缩放为整页 [英] iTextSharp Scaling image to be full-page

查看:50
本文介绍了iTextSharp将图像缩放为整页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像缩放为整页在PDF文档上.我正在使用iTextSharp生成文档.该图像具有正确的页面长宽比,但是理想情况下,我希望图像变形而不是不填充所有可用区域.

I'm trying to scale an image to be full-page on a PDF document. I'm generating the document using iTextSharp. The image has the correct aspect ratio for the page but I'd ideally prefer that the image distort rather than not fill all available area.

我目前有:

Dim Document As New Document(PageSize, 0, 0, 0, 0)
...
Dim ContentImage = '''Method call to get image'
Dim Content = iTextSharp.text.Image.GetInstance(ContentImage, New BackgroundColor)
Content.SetAbsolutePosition(0, 0)
Content.ScaleToFit(Document.PageSize.Width, Document.PageSize.Height)
Document.Add(Content)

不幸的是,这并不能说明打印机的边距...

Unfortunately, this doesn't account for printer margins...

我需要图像适合可打印区域(最好在pdf中定义)

I need the image to fit the printable area (as best as can be defined in a pdf)

预先感谢

推荐答案

如果您确定凭经验进行此操作,请使用代码将页面打印出来,因为它会缩放到页面边框,从而图像会被涂成黑色如果可以到达边缘,则保证金的前半英寸.测量每个边缘到黑色的距离(以英寸为单位),然后将其除以72.0.

If you're determined to do it empirically, then take print a page with your code as is that scales to page border such that the image would paint black in the first half inch of margin, if it could go to the edge. Measure the distance from each edge to black in inches and divide each by 72.0.

我们将其命名为:lm,rm,tm,bm(左上右下边缘.

Let's name them: lm, rm, tm, bm (left right top bottom margins.

Dim pageWidth = document.PageSize.Width - (lm + rm);
Dim pageHeight = document.PageSize.Height - (bm + tm);
Content.SetAbsolutePosition(lm, bm);
Content.ScaleToFit(pageWidth, pageHeight);
Document.Add(Content)

这篇关于iTextSharp将图像缩放为整页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆