iTextSharp在PDF中呈现质量较差的图像

Seb*_*ian 13 vb.net asp.net itextsharp

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

打印的图像如下: 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
Run Code Online (Sandbox Code Playgroud)

这导致原始图像丢失信息,所以当在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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

ResizeImage()方法根据纵横比调整宽度和高度,并保持最大宽度和最大高度限制.

如果我需要提供更多信息,请告诉我.谢谢

Atr*_*ige 7

除了打印机问题(见上文),Your Friend的3 X提示是最终的解决方案.

因此,换句话说,如果您希望图像在PDF上为100 X 100,请确保您的图像为300px X 300px或更大.

我尝试也使用300dpi图像,我没有测试较低质量的图像.

这是我的图片添加代码:

try
{
    string uri = Environment.CurrentDirectory + "/" + "pdfwithimage_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
    string strImgJpg = Environment.CurrentDirectory + "/HeaderImage.jpg";

    Image imgJpg = Image.GetInstance(System.Drawing.Image.FromFile(strImgJpg), new BaseColor(System.Drawing.Color.White));

    using (Document pdf = new Document(PageSize.A4, 20, 20, 20, 20))
    {
        if (pdf == null)
        {
            throw new NullReferenceException("PDF has not been instanciated");
        }

        if (File.Exists(uri))
        {
            File.Delete(uri);
        }

        using (PdfWriter pdfwriter = PdfWriter.GetInstance(pdf, new FileStream(uri, FileMode.Create)))
        {
            pdf.Open();

            imgJpg.SetDpi(300, 300);

            imgJpg.ScaleToFit(100f, 100f);

            pdf.Add(imgJpg);

            pdf.Close();
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*bob 6

我也有很好的使用iTextSharp渲染非常清晰和清晰图像的经验.我尝试将图像直接添加到文档中,然后将其添加到段落中.两者都给出非常明确的结果

Dim document As Document = New Document(pSize, 20, 20, 20, 20)
PdfWriter.GetInstance(document, New FileStream(myPath & "Test.pdf", FileMode.Create))
document.Open()

Dim png As Image = Image.GetInstance(myPath & "myImageFile.png")
document.Add(png)

Dim pgr As New Paragraph
pgr.Add(png)
document.Add(pgr)
document.Close()
Run Code Online (Sandbox Code Playgroud)

我通常使用.png图像,但我在jpeg,gif等方面取得了同样的成功.

您确定当您在iTextSharp中检索图像时,它与您在Acrobat 中时检索的图像完全相同吗?我问,因为不清楚你的代码中发生了什么exaclty:

Dim imageFile As String=String.Format(.....
Run Code Online (Sandbox Code Playgroud)

编辑

此外,为确保图像以您期望的尺寸显示,请以72dpi保存图像.72 dpi是iTextSharp用于所有内容(图像,边距等).这样,100px x 100px图像将在您的pdf文档中显示为100x100.您不必担心缩放或重新调整大小.无论何时缩放图像(向上或向下),都存在引入锯齿伪像(模糊)的风险.