简体   繁体   English

使用pdfBox在Landscape中使用Pdf

[英]Pdf in Landscape using pdfBox

Currently I am working with PDFBox of Apache to generate pdf. 目前我正在使用Apache的PDFBox生成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. 它在肖像模式下工作得非常好,但后来我的要求是前两页应该是横向模式,然后是所有其他页面的纵向。

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切换到其他库

另一个解决方案是

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));

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)分配纵向媒体框,旋转页面并旋转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));
(...)

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM