PDFBox:将高清图像写入PDF



我正在尝试使用cameraa将映像转换为A4 PDF

    // Read image width and height
    float imageWidth = bitmap.getWidth();
    float imageHeight = bitmap.getHeight();
    // Read page width and height ignoring the margins
    float pageWidth = PDRectangle.A4.getWidth() - (margin_left_right * 2);
    float pageHeight = PDRectangle.A4.getHeight() - (margin_top_bottom * 2);
    Log.d("Image Resize", String.valueOf(imageWidth) + " x " + String.valueOf(imageHeight));
    Log.d("Image Resize", String.valueOf(pageWidth) + " x " + String.valueOf(pageHeight));
    # Image: 4160.0 x 3120.0
    # Page: 575.27563 x 821.8898
    Log.d("Image Resize", String.valueOf(requiredRatio));
    Log.d("Image Resize", String.valueOf(newImageWidth) + " x " + String.valueOf(newImageHeight));
    # Required ratio: 0.13828741
    # After scaling: 575 x 431

问题是带有4160 x 3120大小的图像,在调整到575 x 431大小后,看起来非常像素化。

PDF通常如何解决此问题?使用contentStream.drawImage()写作时,有没有办法在600 dpi上说4160 x 3120


这个问题还支持这样一个事实,即如果在不调整大小的大小不等于图像的高度和宽度的情况下,OP只能以15%的速度看到图像,如何将其缩小而不会损失质量?<<?/p>

有一个超载的drawImage()方法

contentStream.drawImage(PDImageXObject, float x, float y)
contentStream.drawImage(PDImageXObject, float x, float y, float height, float width)

通过指定contentStream.drawImage()上的显式宽度和高度,可以实现这一点。例如,如果您想显示图像较小4倍但仍然保留相同的分辨率,则可以使用

来完成。
scaleDownRatio = 0.25;
pageSize = PDRectangle.A4;
# this will draw image at bottom left corner
PDImageXObject pageImageXObject = LosslessFactory.createFromImage(document, bitmap);
contentStream.drawImage(pageImageXObject, 0, 0, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)
# to center it you can use something like
contentStream.drawImage(pageImageXObject, (pageSize.getWidth() - bitmap.getWidth() * ratio) / 2, (pageSize.getHeight() - bitmap.getHeight() * ratio) / 2, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)

最新更新