【发布时间】:2011-02-01 05:30:51
【问题描述】:
我正在使用 iTextSharp 打印 PDF 文档。一切正常,直到我必须在其中打印公司徽标。
首先我注意到徽标的质量很差,但在测试了几张图片后,我意识到这是 iTextSharp 渲染它的质量很差。 我所做的测试是使用我的代码打印 PDF,然后使用 Acrobat 8.0 编辑文档并绘制图像。然后打印了两份文件,看到了明显的不同。 我的问题是,如果有人知道这是否是由于缩放问题造成的,我没有告诉 iTextSharp 它必须如何渲染图像或者是 iTextSharp 限制。
渲染图像的代码如下:
Dim para As Paragraph = New Paragraph
para.Alignment = Image.RIGHT_ALIGN
para.Add(text)
Dim imageFile As String = String.Format("{0}{1}", GetAppSetting("UploadDirectory"), myCompany.LogoUrl)
Dim thisImage As Image = Image.GetInstance(imageFile)
thisImage.Alignment = Image.LEFT_ALIGN
para.Add(thisImage)
打印出来的图片如下: alt text http://img710.imageshack.us/img710/4199/sshot2y.png
直接用 iTextSharp 打印的图像
alt text http://img231.imageshack.us/img231/3610/sshot1z.png
使用 Acrobat 8 编辑和打印的图像
编辑: 这些徽标图像是从用户上传任何他想要的徽标图像的上传页面加载的,我正在使用以下代码缩放该图像:
Dim graph As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
graph.CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver
graph.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
graph.InterpolationMode = Drawing.Drawing2D.InterpolationMode.Bicubic
graph.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
graph.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
graph.DrawImage(newImage, 0, 0, newWidth, newHeight)
graph.Dispose()
graph = Nothing
这会导致原始图像中的信息丢失,因此当在 pdf 中打印时,信息丢失非常明显,因为不知何故,iTextSharp 绘制的尺寸比原来大,无论我在那里进行缩放。 因此,我尝试按原样存储图像,防止用户不上传大于 200K 的图像并调整图像大小,以便我可以保持纵横比,并在打印之前将调整大小与 iTextSharp Image 对象一起使用。 这解决了我的问题,即这些较大图像的图像打印质量较差,但导致 pdf 文档出现分页符或不适合页面,这很奇怪,因为图片看起来尺寸不错,但表现得好像更大. 这是新图像的屏幕截图: alt text http://img38.imageshack.us/img38/5756/sshot3tc.png
编辑 2:
检查发送打印的 iTextSharp 图像时,使用 ScaleAbsolute 缩放后显示没有任何变化,这就是分页的原因。但显示正确,就像图像已成功缩放,但背景“纸张”却没有。 目前使用的代码如下:
Dim imageFile As String = String.Format("{0}{1}", GetAppSetting("UploadDirectory"), myCompany.LogoUrl)
将 thisImage 调暗为 Image = Image.GetInstance(imageFile) thisImage.Alignment = Image.LEFT_ALIGN
Dim newWidth As Integer = myCompany.LogoWidth
Dim newHeight As Integer = myCompany.LogoHeight
ResizeImageToMaxValues(newWidth, newHeight)
thisImage.ScaleAbsolute(newWidth, newHeight)
para.Add(thisImage)
pdf.PdfDocument.Add(para)
ResizeImage() 方法根据纵横比调整宽度和高度,并保持最大宽度和最大高度限制。
如果我需要提供更多信息,请告诉我。谢谢
【问题讨论】:
-
我的第一个想法是这是一个分辨率问题,当我从文件创建图像并设置它的大小时,我做错了,所以它的分辨率低于 itextSharp需要以公平的质量渲染它,但我想知道你的想法。
标签: asp.net vb.net itextsharp