简体   繁体   English

itext 中的图像质量不佳

[英]Poor image quality in itext

I am using itext7 to create pdf file ,and at the footer I am trying to add png images as follows:我正在使用 itext7 创建 pdf 文件,并在页脚处尝试添加 png 图像,如下所示:

PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
PdfPage page = documentEvent.getPage();
PdfCanvas canvas = new PdfCanvas(page);
byte[] signature = null; // retrieved from database
PngImageData imageData = (PngImageData) ImageDataFactory.createPng(signature);
canvas.addImageAt(imageData, 5f, 25f, false);

the original image itself has good resolution, but when the image is added to the pdf it appears with poor quality although I am adding the image without any changes or scaling.原始图像本身具有良好的分辨率,但是当将图像添加到 pdf 时,它看起来质量很差,尽管我在添加图像时没有进行任何更改或缩放。

how can I improve the image quality in the final pdf ?如何提高最终 pdf 的图像质量?

This is snippet of my code for adding image to PDF:这是我将图像添加到 PDF 的代码片段:

PdfContentByte cbLogo = writer.getDirectContent();

if (instanceSettings.getDocumentHeaderImageLocation() != null) {
    try {
        String encodedString = instanceSettings.getDocumentHeaderImageLocation();
        byte[] decodedBytes = Base64
                .getDecoder()
                .decode(encodedString);
        Image imgLogo = Image.getInstance(decodedBytes);
        imgLogo.scaleToFit(220f, 150f);
        imgLogo.setAbsolutePosition((writer.getPageSize().getWidth() / 4) - (imgLogo.getScaledWidth() / 2),
                writer.getPageSize().getHeight() - imgLogo.getScaledHeight() - 30);
        cbLogo.addImage(imgLogo);
    } catch (Exception e){
        ErrorHandler.handle(e);
    }
}

And here is another:这是另一个:

    Image qr = Image.getInstance(file.getAbsolutePath());
    qr.scaleToFit(70f, 70f);
    qr.setSpacingBefore(0);
    qr.setSpacingAfter(0);
    qr.setPaddingTop(0);
    PdfPCell cell = new PdfPCell();
    cell.setPadding(0f);
    cell.setUseAscender(true);
    cell.setUseDescender(true);
    cell.addElement(qr);

Image is of type:图像类型:

import com.itextpdf.text.Image;

You can give it a try with this code.您可以使用此代码尝试一下。 For me, both works as expected but as I said in the comments, I sometimes have dificulties viewing it in PDF application.对我来说,两者都按预期工作,但正如我在评论中所说,我有时在 PDF 应用程序中查看它有困难。 As far as I know, itext has nothing to do with it.据我所知,itext 与它无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM