PDFBox:将高清图像写入 PDF
PDFBox: Writing HD images to 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
?
这个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)
我正在尝试将使用 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
?
这个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)