使用pdfBox在Landscape中使用Pdf

Rau*_*han 5 java pdf pdf-generation pdfbox

目前我正在使用Apache的PDFBox生成pdf.它在肖像模式下工作得很好,但后来我的要求是前两页应该是横向模式,然后是所有其他页面的纵向.

所以任何人都可以帮助我如何在横向创建PDF并实现此功能?

注意:我无法从PDFBox切换到其他库

wut*_*aer 29

另一个解决方案是

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
Run Code Online (Sandbox Code Playgroud)

  • 恕我直言,这应该是公认的答案。更简单,更优雅。 (3认同)

Til*_*err 7

有两种策略:

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));
Run Code Online (Sandbox Code Playgroud)

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));
(...)
Run Code Online (Sandbox Code Playgroud)