java 使用pdfBox在风景中使用pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37550829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pdf in Landscape using pdfBox
提问by Raushan
Currently I am working with PDFBox of Apache to generate pdf. It is working perfectly fine in portrait mode but then my requirement is that 1st two page should be in landscape mode and afterwards all other pages in portrait.
目前我正在使用 Apache 的 PDFBox 生成 pdf。它在纵向模式下运行良好,但我的要求是第一两页应处于横向模式,然后所有其他页面应处于纵向模式。
So can anyone please help me on how to create pdf in landscape and achieve this functionality??
那么任何人都可以帮助我如何在横向中创建 pdf 并实现此功能吗?
Note:I cannot switch from PDFBox to other libraries
注意:我无法从 PDFBox 切换到其他库
回答by wutzebaer
Another solution would be
另一种解决方案是
PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
回答by Tilman Hausherr
There are two strategies:
有两种策略:
1) assign a landscape mediabox (this is for A4):
1)分配一个横向媒体框(这是A4):
float POINTS_PER_INCH = 72;
float POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH;
new PDPage(new PDRectangle(297 * POINTS_PER_MM, 210 * POINTS_PER_MM));
2) assign a portrait mediabox, rotate the page and rotate the CTM, as shown in the official example:
2) 指定一个纵向mediabox,旋转页面并旋转CTM,如官方示例所示:
PDPage page = new PDPage(PDRectangle.A4);
page.setRotation(90);
doc.addPage(page);
PDRectangle pageSize = page.getMediaBox();
float pageWidth = pageSize.getWidth();
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
// add the rotation using the current transformation matrix
// including a translation of pageWidth to use the lower left corner as 0,0 reference
contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
(...)
回答by gkbstar
Below worked for me.
下面为我工作。
float POINTS_PER_INCH = 72;
float POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH;
PDPage page = new PDPage(new PDRectangle(400 * POINTS_PER_MM, 210 * POINTS_PER_MM));