在iText上添加段落元素pdf转到下一页

时间:2014-11-13 16:04:28

标签: java pdf-generation itext

以下是用于创建段落的代码。 dpa.getName()将从另一段代码中获取HTML内容,该代码由HTML标记组成,然后将其删除并传递给单元格。

PdfPTable table = new PdfPTable(new float[] {100});
table.setWidthPercentage(100);

//This code takes the HTML and converts it to a list of iText objects
//which can then be rendered on PDF
StringReader stream = new StringReader(Utilities.parseForHTMLWorker(dpa.getName()));
PdfPCell dpaCell = makeBodyCell("");
try {
        ArrayList list = HTMLWorker.parseToList( stream, null);
        for( int i = 0; i < list.size(); i++ ) {
            Paragraph p = (Paragraph)list.get(i);
            dpaCell.addElement(p);
        }            
    } catch (IOException e) {
        Logger.Log("InitialInterviewDetailsPrint", "render()", e.toString(), 1);
        e.printStackTrace();
    }
table.addCell(dpaCell);

list变量获取Paragraph的列表。这些段落足够大,至少有10个句子。即使页面底部有5行空间,所有内容也会移动到新页面。我不希望页面底部留下这个空白区域,并希望段落在两个页面中分割其内容。

是否需要在段落上设置任何特定属性才能将它们放在同一页面上?

1 个答案:

答案 0 :(得分:0)

您必须使用setKeepTogether方法强制iText

Paragraph paragraph = new Paragraph("");
paragraph.setKeepTogether(false);

PdfPTable table = new PdfPTable(numberOfColumns);
table.setKeepTogether(false);

正如您在API中看到的那样:

protected boolean keeptogether
//Does the paragraph has to be kept together on 1 page.
相关问题