有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用pdfBox在景观中使用java Pdf

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

那么,有谁能帮助我在横向中创建pdf并实现此功能

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


共 (3) 个答案

  1. # 1 楼答案

    另一个解决方案是

    PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
    
  2. # 2 楼答案

    有两种策略:

    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));
    (...)
    
  3. # 3 楼答案

    下面是我的工作

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