pdfbox在旋转缩放图像时,尺寸比不旋转时小

6g8kf2rb  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(369)

我面临一个问题,我画一个图像与pdfbox,当我画它没有旋转我得到了正确的尺寸,但当我画它与旋转的尺寸将更小。
以下是图片和代码:
不旋转

旋转45度

代码:

public void placeImage(String imagePath, float x, float y, float rotation, float width, float height) throws Exception {
        File imageFile;
        boolean isTemp = false;
        if (imagePath.contains("http")) {
            imageFile = saveImageFromUrlToTempFile(imagePath);
            isTemp = true;
        } else {
            imageFile = new File(imagePath);
        }

        PDImageXObject imageToPutInside = PDImageXObject.createFromFileByExtension(imageFile, outputDocument);

        float newWidth;
        float newHeight;
        float imageHeight = imageToPutInside.getHeight();
        float imageWidth = imageToPutInside.getWidth();

        if (imageHeight > imageWidth) {
            newHeight = height > imageHeight ? imageHeight : height;
            newWidth = (newHeight * imageToPutInside.getWidth()) / imageToPutInside.getHeight();
        } else {
            newWidth = width > imageWidth ? imageWidth : width;
            newHeight = (newWidth * imageToPutInside.getHeight()) / imageToPutInside.getWidth();
        }

        x = x + ((width - newWidth) / 2);

        y = y + ((height - newHeight) / 2);

        y = calculateYAxisFromTop(y) - newHeight;

        outputPageContentStream.saveGraphicsState();

        AffineTransform transform = new AffineTransform(newWidth, 0, 0, newHeight, x, y);

        if(rotation != 0) {
            transform.concatenate(AffineTransform.getRotateInstance(rotation, 0.5, 0.5));
        }

        outputPageContentStream.drawImage(imageToPutInside, new Matrix(transform));

        outputPageContentStream.restoreGraphicsState();

        if (isTemp) {
            imageFile.delete();
        }
    }

你能帮我做这个吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题