PDFBox的:写入HD图像PDF

我想转换形象使用cameraa到A4 PDFPDFBox的:写入HD图像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


这question还支持的事实,如果写不调整大小等于图像的高度和宽度的页面,OP只能看到图像的15%,应该如何是按比例缩小而不会损失质量?

回答:

有一个重载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)

以上是 PDFBox的:写入HD图像PDF 的全部内容, 来源链接: utcz.com/qa/265728.html

回到顶部