使用java中的PDFBox在其中心周围旋转PDF [英] Rotate PDF around its center using PDFBox in java

查看:329
本文介绍了使用java中的PDFBox在其中心周围旋转PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  PDDocument document = PDDocument.load(new File(input)); 
PDPage page = document.getDocumentCatalog()。getPages()。get(0);
PDPageContentStream cs = new PDPageContentStream(document,page,PDPageContentStream.AppendMode.PREPEND,false,false);
cs.transform(Matrix.getRotateInstance(Math.toRadians(45),0,0));

我使用上面的代码来旋转PDF。





对于上面的图片,我正在获取以下输出





从该代码开始,内容页面已移出框架,并且旋转不会围绕其中心发生。但我希望得到输出





请给我一些选择。在此先感谢。

解决方案

有两种主要方法可以旋转页面内容并使其在查看器中显示,就像旋转一样发生在可见页面的中间:通过将旋转与平移连接或者移动裁剪框以使页面区域中心跟随旋转,任何一个实际上都围绕其中间旋转。



实际围绕中心旋转



为此,我们将两次平移之间的旋转包围起来,第一次将坐标系的原点移动到页面中心,第二个将它再次移回。

  PDDocument document = PDDocument.load(resource); 
PDPage page = document.getDocumentCatalog()。getPages()。get(0);
PDPageContentStream cs = new PDPageContentStream(document,page,PDPageContentStream.AppendMode.PREPEND,false,false);
PDRectangle cropBox = page.getCropBox();
float tx =(cropBox.getLowerLeftX()+ cropBox.getUpperRightX())/ 2;
float ty =(cropBox.getLowerLeftY()+ cropBox.getUpperRightY())/ 2;
cs.transform(Matrix.getTranslateInstance(tx,ty));
cs.transform(Matrix.getRotateInstance(Math.toRadians(45),0,0));
cs.transform(Matrix.getTranslateInstance(-tx,-ty));
cs.close();


  1. 实际上围绕中心旋转

  2. 在旋转后缩小内容以适应

  3. 拉动裁剪框

  4. 更改裁剪框以使所有以前的页面区域保持可见

图像4与其他页面的比例不同,它应该显示更大。


PDDocument document = PDDocument.load(new File(input));
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.PREPEND, false, false); 
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));

I am using the above code to rotate the PDF.

For the above image, i am getting following output

From that code, the content of the page has been moving out of the frame and the rotation is not happening around its center. But i want to get the output as

Please suggest me some options. Thanks in advance.

解决方案

There are two major ways to rotate the page content and make it appear in a viewer as if the rotation happened around the middle of the visible page: Either one actually does rotate around the middle of it by concatenating the rotation with translations or one moves the crop box so that the page area center follow the rotation.

Actually rotating around the center

To do this we envelop the rotation between two translations, the first one moves the origin of the coordinate system to the page center and the second one moves it back again.

PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false); 
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();

(RotatePageContent test testRotateCenter)

Obviously you can multiply the matrices and only add a single transformation to the PDF.

Pulling the crop box along

To do this we calculate the move of the page center and move the boxes accordingly.

PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
cs.transform(matrix);
cs.close();

PDRectangle cropBox = page.getCropBox();
float cx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float cy = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
Point2D.Float newC = matrix.transformPoint(cx, cy);
float tx = (float)newC.getX() - cx;
float ty = (float)newC.getY() - cy;
page.setCropBox(new PDRectangle(cropBox.getLowerLeftX() + tx, cropBox.getLowerLeftY() + ty, cropBox.getWidth(), cropBox.getHeight()));
PDRectangle mediaBox = page.getMediaBox();
page.setMediaBox(new PDRectangle(mediaBox.getLowerLeftX() + tx, mediaBox.getLowerLeftY() + ty, mediaBox.getWidth(), mediaBox.getHeight()));

(RotatePageContent test testRotateMoveBox)

Scaling the content down to fit after rotation

If one wants to scale down the rotated content to make it all fit, one can do this as an easy extension of the first variant:

PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);

Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;

Rectangle rectangle = cropBox.transform(matrix).getBounds();
float scale = Math.min(cropBox.getWidth() / (float)rectangle.getWidth(), cropBox.getHeight() / (float)rectangle.getHeight());

cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(matrix);
cs.transform(Matrix.getScaleInstance(scale, scale));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();

(RotatePageContent test testRotateCenterScale)

Changing the crop box to make all former page area remain visible

If one wants instead to change the crop box to make everything fit without scaling, one can do this as an easy extension of the second variant:

PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
cs.transform(matrix);
cs.close();

PDRectangle cropBox = page.getCropBox();
Rectangle rectangle = cropBox.transform(matrix).getBounds();
PDRectangle newBox = new PDRectangle((float)rectangle.getX(), (float)rectangle.getY(), (float)rectangle.getWidth(), (float)rectangle.getHeight());
page.setCropBox(newBox);
page.setMediaBox(newBox);

(RotatePageContent test testRotateExpandBox)

Sample results

The following image shows an output for each of the methods above:

  1. Actually rotating around the center
  2. Scaling the content down to fit after rotation
  3. Pulling the crop box along
  4. Changing the crop box to make all former page area remain visible

Image 4 is not at the same scale as the others, it should show larger.

这篇关于使用java中的PDFBox在其中心周围旋转PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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