java – Poor image quality in itext

java – Poor image quality in itext

This is snippet of my code for adding image to 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. As far as I know, itext has nothing to do with it.

java – Poor image quality in itext

Leave a Reply

Your email address will not be published.