我使用在这里另一个问题的答案中由Nick Russler提供的代码,向文档添加了图片https://dev59.com/a2ox5IYBdhLWcg3w3X5S#20618152
/**
* Draw an image to the specified coordinates onto a single page. <br>
* Also scaled the image with the specified factor.
*
* @author Nick Russler
* @param document PDF document the image should be written to.
* @param pdfpage Page number of the page in which the image should be written to.
* @param x X coordinate on the page where the left bottom corner of the image should be located. Regard that 0 is the left bottom of the pdf page.
* @param y Y coordinate on the page where the left bottom corner of the image should be located.
* @param scale Factor used to resize the image.
* @param imageFilePath Filepath of the image that is written to the PDF.
* @throws IOException
*/
public static void addImageToPage(PDDocument document, int pdfpage, int x, int y, float scale, String imageFilePath) throws IOException {
// Convert the image to TYPE_4BYTE_ABGR so PDFBox won't throw exceptions (e.g. for transparent png's).
BufferedImage tmp_image = ImageIO.read(new File(imageFilePath));
BufferedImage image = new BufferedImage(tmp_image.getWidth(), tmp_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
image.createGraphics().drawRenderedImage(tmp_image, null);
PDXObjectImage ximage = new PDPixelMap(document, image);
PDPage page = (PDPage)document.getDocumentCatalog().getAllPages().get(pdfpage);
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
contentStream.drawXObject(ximage, x, y, ximage.getWidth()*scale, ximage.getHeight()*scale);
contentStream.close();
}
通常,通过XObjectImage将图像添加到PDF页面中,但我发现相同的代码根据使用的PDF文件会有不同的结果。我猜测这似乎存在一些比例或变换,但我无法找到或纠正它。
该页面报告(来自MediaBox PDRectangle)其尺寸为大约600x800(页面单位)。但是,当我放置我的500px图像时,根据使用的PDF,它以不同的方式显示。在一个PDF中,它以页面宽度的大小显示(这是生成的PDF - 即文本和对象等)。在另一个PDF中,该图像的宽度约为原始图像的一半至三分之一(此PDF是扫描的A4 TIF图像放在PDF页面上 - 该图像约为1700x2300px - 这与正在发生的缩小比率相吻合),最后,在PDF页面上的另一个TIF图像中,我的添加的图像还会旋转90度。
对我来说,显然需要添加或修改一种变换 - 页面具有默认值 - 或者记住上次使用的变换,我只想要1:1的比例和0度的旋转,但我不知道如何做到这一点?
我已经阅读了有关Matrix和AffineTransformations的内容,但对我来说没有太多意义。
是否有一种方法可以将文档或drawXObject设置为非常1:1的比例和0度旋转?