我正在使用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)
打印的图像如下: 替代文字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)
Dim thisImage As 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()方法根据纵横比调整宽度和高度,并保持最大宽度和最大高度限制。
如果我需要提供更多信息,请告诉我。谢谢