2011-02-08 60 views
6

我想缩放图像是在PDF文档整版。我使用iTextSharp生成文档。图像具有正确的页面高宽比,但我最好选择图像扭曲而不是填充所有可用区域。iTextSharp的缩放图像以整版

我目前有:

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) 

不幸的是,这并没有考虑打印机边距...

我需要的图像以适合打印区域(是最好的,可以在被定义PDF)

在此先感谢

+0

@plinth谢谢,还没有发现一个 – Basic 2012-07-05 14:12:41

回答

8

如果你决心经验去做,然后采取打印页面与你的代码是扩展到页面边框,使得图像能画出黑色的保证金上半年英寸,如果它可以去边缘。以英寸为单位测量每个边缘到黑色的距离,然后分别除以72.0。

让他们的名字:LM,RM,以旧换新,BM(左右顶底边距

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) 
+0

有道理,谢谢 – Basic 2011-02-08 14:08:08

3

可打印区域是打印机的依赖,PDF文件一无所知。 PDF页面可以包含从保证金到保证金的内容。您可以使用“适合打印机边距”选项打印PDF文件,以便将整个PDF页面打印为打印机可打印区域的缩放比例。

+0

知道了 - 但我使用自动打印(最初):`Writer.AddJavaScript( “this.print(假)”,FALSE) `并且宁愿避免手动设置缩放选项。这可以实现自动化吗?在这种情况下,它可以在单个打印机上内部打印文档,因此我也可以接受在生成期间必须知道相应的边距并将其设置在PDF上。 – Basic 2011-02-08 11:41:19

1

您可以缩放图像,通过使用下面的代码片段,以适应PDF页面

VB。

Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream), System.Drawing.Imaging.ImageFormat.Png) 
img.SetAbsolutePosition(0, 0) 
'set the position to bottom left corner of pdf 
img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width, iTextSharp.text.PageSize.A7.Height) 
'set the height and width of image to PDF page size 

C#

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream, System.Drawing.Imaging.ImageFormat.Png); 
img.SetAbsolutePosition(0, 0); // set the position to bottom left corner of pdf 
img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width,iTextSharp.text.PageSize.A7.Height); // set the height and width of image to PDF page size 

如果你想要完整的代码(c#),你也可以参考下面的链接。完整的代码将图像添加到现有PDF的所有页面。

https://stackoverflow.com/a/45486484/6597375