首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache表/行对齐

Apache表/行对齐
EN

Stack Overflow用户
提问于 2021-11-01 18:55:27
回答 2查看 745关注 0票数 0

我在我的程序中导出一个PDF,我想用ApachePDF盒创建一个表,它应该是页面宽度的50-60%。

然而,我没有找到任何关于对行/表本身对齐的方法。

我找到了如何对齐行/单元格本身中的文本,但是如果我创建的行不使用页面的全部宽度,它总是左对齐,而且我不知道如何对齐行,因为行或表没有setAlign方法。

我在上面使用Boxable (https://github.com/dhorions/boxable)

EN

回答 2

Stack Overflow用户

发布于 2021-12-11 20:28:01

代码语言:javascript
复制
  public void Test() throws IOException     {  

    //Set margins
   
    float margin = 10;

    //Initialize Document
    PDDocument doc = new PDDocument();
    PDPage page = addNewPage(doc);

    //Initialize table
    float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
    float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
    boolean drawContent = true;
    boolean drawLines = true;
    float yStart = yStartNewPage;
    float bottomMargin = 70;
    BaseTable table = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, drawLines,
            drawContent);

    // set default line spacing for entire table
    table.setLineSpacing(1.5f);

    Row<PDPage> row = table.createRow(10);

    // set single spacing for entire row
    row.setLineSpacing(1f);

    // my first 3x wider cell
    Cell<PDPage> cell = row.createCell((3*100/15f), "1",
            HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
    cell.setFontSize(6);

            // my other 12 equal cells
    for(int i=2; i<14; i++){
        Cell<PDPage> cell2 = row.createCell((100/15f), String.valueOf(i),
                HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
        cell2.setFontSize(6);
    }

    table.draw();

    //Save the document
    File file = new File("target/test.pdf");
    System.out.println("Sample file saved at : " + file.getAbsolutePath());
    Files.createParentDirs(file);
    doc.save(file);
    doc.close();
}

调整设置位置和表宽..。

票数 0
EN

Stack Overflow用户

发布于 2021-12-11 20:37:05

调整设置位置和表宽:

代码语言:javascript
复制
public void Test() throws IOException {
    //Set margins
    float margin = 10;

    //Initialize Document
    PDDocument doc = new PDDocument();
    PDPage page = addNewPage(doc);

    //Initialize table
    float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
    float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
    boolean drawContent = true;
    boolean drawLines = true;
    float yStart = yStartNewPage;
    float bottomMargin = 70;
    BaseTable table = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, drawLines, drawContent);

    // set default line spacing for entire table
    table.setLineSpacing(1.5f);

    Row<PDPage> row = table.createRow(10);

    // set single spacing for entire row
    row.setLineSpacing(1f);

    // my first 3x wider cell
    Cell<PDPage> cell = row.createCell((3*100/15f), "1",
    HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
    cell.setFontSize(6);

    // my other 12 equal cells
    for(int i=2; i<14; i++){
        Cell<PDPage> cell2 = row.createCell((100/15f), String.valueOf(i),                    
        HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
        cell2.setFontSize(6);
    }

    table.draw();

    //Save the document
    File file = new File("target/test.pdf");
    System.out.println("Sample file saved at : " + file.getAbsolutePath());
    Files.createParentDirs(file);
    doc.save(file);
    doc.close();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69801508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档