Scaled image blurry in PDFBox

前端 未结 1 1105
抹茶落季
抹茶落季 2021-02-14 09:33

I\'m trying to scaling an image with size = 2496 x 3512 into a PDF document. I\'m using PDFBox to generate it but the scaled image ends up blurred.

Here are some snippet

相关标签:
1条回答
  • 2021-02-14 10:10

    Ok, I found a way to add images without losing the quality.

    Actually to make the image not be blurred I let PDFBox to resize the image by giving it the desired size. Like the code below:

    PDXObjectImage ximage = new PDJpeg(doc, new FileInputStream(new File("/usr/gyo/my_large_image.jpg")), 1.0f);
    PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false);
    Dimension scaledDim = getScaledDimension(new Dimension(ximage.getWidth(),  ximage.getHeight()), page.getMediaBox().createDimension());
    contentStream.drawXObject(ximage, 1, 1, scaledDim.width, scaledDim.height);
    contentStream.close();
    

    Thank you,

    Gyo

    0 讨论(0)
提交回复
热议问题