Apache PDFBox旋转PDImageXObject [英] Apache PDFBox rotate PDImageXObject

查看:899
本文介绍了Apache PDFBox旋转PDImageXObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩2.0.0-SNAPSHOT,我想将页面设置为横向并旋转图片.所以我已经完成了page.setRotation(90);

I'm playing around with the 2.0.0-SNAPSHOT, and I want to set the page to landscape and also rotate my picture. So I've done page.setRotation(90);

使用PDFBox和AffineTransform似乎有一个错误

There seems to be a bug with using PDFBox and AffineTransform

此代码没有执行我期望的任何操作:

This code doesn't do anything like I'd expect:

AffineTransform at = new AffineTransform(w, 0, 0, h, 20, 20);
at.translate(0.5, 1);
at.rotate(Math.toRadians(90));

宽度和高度必须很小,才能将图像保留在页面上,自身旋转挤压图像,然后平移,直到旋转看起来会放大图像为止.

Width and Height have to be tiny to keep the image on the page, rotate by itself squishes the image, and translate before rotate seems to scale the image huge.

这是一个错误,还是我不了解PDFBox?

Is this a bug, or am I just not understanding PDFBox?

推荐答案

不要做额外的翻译,而是在创建AT时放置翻译.请记住,旋转是围绕左下轴进行的,因此将宽度w添加到x位置.

Don't do an extra translation, instead put the translation when creating the AT. Remember that the rotation is around the bottom-left axis, so add the width w to the x-position.

    PDPage page = new PDPage();
    document.addPage(page);
    page.setRotation(90);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    int x = 150;
    int y = 300;

    // draw unrotated
    contentStream.drawXObject(ximage, x, y, ximage.getWidth() / 2, ximage.getHeight() / 2);

    // draw 90° rotated, placed on the right of the first image
    AffineTransform at = new AffineTransform(ximage.getHeight() / 2, 0, 0, ximage.getWidth() / 2, x + ximage1.getWidth(), y);
    at.rotate(Math.toRadians(90));
    contentStream.drawXObject(ximage, at);

这将绘制两次图像,一次正常旋转一次,旋转90°并位于右侧. "/2"用于缩放50%,您当然可以使用其他因子.请注意,"/2"不用于初始x位置,因为(缩放)宽度需要两次.一次定位到旧位置(由于轴!),一次定位到右侧,以使图像不重叠.

This will draw the image twice, once normally and once rotated 90°, and positioned to the right. "/2" is used to scale 50%, you can of course use another factor. Note that "/2" is not used for the initial x position, because the (scaled) width is needed twice. Once to position to the old position (because of the axis!), and once to move it to the right so that the images don't overlap.

还要注意,对于页面旋转,getHeight()和getWidth()是相反的.

Note also that getHeight() and getWidth() are reversed, for the page rotation.

这篇关于Apache PDFBox旋转PDImageXObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆