使用 pdfBox 的横向 PDF
Pdf in Landscape using pdfBox
目前正在使用Apache的PDFBox生成pdf。它在纵向模式下工作得很好,但我的要求是第一两页应该是横向模式,然后所有其他页面应该是纵向模式。
那么谁能帮我看看如何在横向创建 pdf 并实现此功能??
注意:我无法从 PDFBox 切换到其他库
有两种策略:
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)分配纵向媒体框,旋转页面和旋转CTM,as shown in the official example:
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));
(...)
另一个解决方案是
PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
下面对我有用。
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));
目前正在使用Apache的PDFBox生成pdf。它在纵向模式下工作得很好,但我的要求是第一两页应该是横向模式,然后所有其他页面应该是纵向模式。
那么谁能帮我看看如何在横向创建 pdf 并实现此功能??
注意:我无法从 PDFBox 切换到其他库
有两种策略:
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)分配纵向媒体框,旋转页面和旋转CTM,as shown in the official example:
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));
(...)
另一个解决方案是
PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
下面对我有用。
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));