【问题标题】:How to wrap text while generating tables in apache PDFbox如何在apache PDFbox中生成表格时换行
【发布时间】:2015-01-15 08:09:29
【问题描述】:

我正在尝试使用以下代码使用 pdfbox 库生成表格。

//Draw lines 
for (int i = 0; i <= cols; i++) {
            contentStream.drawLine(nextx,y,nextx,y-tableHeight);
            nextx += colWidth;
        }

 //fill with data
  float textx = margin+cellMargin;
    float texty = y-15;
    for(int i = 0; i < content.length; i++){
        for(int j = 0 ; j < content[i].length; j++){
            String text = content[i][j];
            contentStream.beginText();
            contentStream.moveTextPositionByAmount(textx,texty);
            contentStream.drawString(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty-=rowHeight;
        textx = margin+cellMargin;
    }

这样做时,创建的单元格中的数据不会被包装 。我在 pdfbox 库中找不到这样做的方法。请提出解决方案。

【问题讨论】:

  • 如果您需要自己编程(例如,因为在大提琴的回答中不允许您使用其他第三方类,例如 PdfLayoutManager),来自this answer 的关于中断文本行的信息可能会有所帮助。

标签: java pdf-generation pdfbox


【解决方案1】:

PdfBox 是一个用于 PDF 文件的低级 API,因此不提供开箱即用的表格设计帮助。 如果你不想自己重新发明轮子并想出这样的功能,你可能想看看这里的 PdfLayoutManager:https://github.com/GlenKPeterson/PdfLayoutManager

它为 PdfBox 提供更高级别的 API,并提供带有自动换行符的文本框,以及带有换行符的表格。

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 2011-10-25
    • 2016-09-19
    • 2015-03-19
    • 2015-03-23
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多