有一个超载的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)
注意:在您的情况下,即使将图像缩放到四分之一大小,图像仍然不适合;您可以通过以下方式获得比例因子
float widthRatio = bitmap.getWidth() / pageSize.getWidth();
float heightRatio = bitmap.getHeight() / pageSize.getHeight();
scaleDownRatio = (widthRatio<heightRatio)? widthratio:heightRatio;