java iText - 避免最后一行不将页面上的表格拆分到下一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16619914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iText - avoid last row not to cut tables on page split to next page
提问by Chandra Shekar
I am working on itext 5 using java. I have pages with mutiple tables with dynamic rows. In some instances, the table last row is splitted into next page with the folowing header. I am using setHeaderRows()
and setSkipFirstHeader()
to manage continuation of next page. The last row has enough space to fit on earlier page. I would like to fit that last row in same page instead of next page.
我正在使用 java 处理 itext 5。我有多个带有动态行的表的页面。在某些情况下,表格的最后一行被拆分为具有以下标题的下一页。我正在使用setHeaderRows()
和setSkipFirstHeader()
管理下一页的延续。最后一行有足够的空间来容纳前面的页面。我想将最后一行放在同一页而不是下一页。
For example, on page 1, the last row is splitted into first row of next page. Instead I would like to fit that row in page 1 so save one extra page with all blanks.
例如,在第 1 页上,最后一行被拆分为下一页的第一行。相反,我想将该行放在第 1 页中,以便保存一个额外的页面,其中包含所有空白。
I tried using setExtendLastRow()
, but its not working. Does anyone know how to fix this problem. I am attaching a working sample code.
我尝试使用setExtendLastRow()
,但它不起作用。有谁知道如何解决这个问题。我附上了一个工作示例代码。
public class ProposalItextSplitLastRow {
public static void main(String[] args) {
try {
Document document = new Document();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 14, 14);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/SplitLastRow.pdf"));
document.open();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 42, 38);
for (int m = 1; m < 20; m++) {
int row = 0;
PdfPTable table = new PdfPTable(1);
table.setSpacingAfter(0);
table.setSpacingBefore(0);
table.setWidthPercentage(100);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
add(table, "Header Row continued " + m, BaseColor.LIGHT_GRAY, row++);
add(table, "Header Row normal " + m, BaseColor.LIGHT_GRAY, row++);
add(table, "Text Row 1 ", BaseColor.WHITE, row++);
add(table, "Text Row 2 ", BaseColor.WHITE, row++);
add(table, "Text Row 3 ", BaseColor.WHITE, row++);
addPadding(table);
document.add(table);
}
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
private static void add(PdfPTable table, String text, BaseColor color, int row) {
PdfPCell pdfCellHeader = new PdfPCell();
pdfCellHeader.setBackgroundColor(color);
pdfCellHeader.addElement(new Paragraph(new Phrase(text)));
table.addCell(pdfCellHeader);
}
private static void addPadding(PdfPTable table) {
PdfPCell cell = new PdfPCell();
cell.setFixedHeight(2f);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(table.getNumberOfColumns());
table.addCell(cell);
}
}
采纳答案by Bruno Lowagie
I had to execute the example to understand your question. You confused me by talking about a header that isn't a header (the rows with "Header Row normal" aren't header rows!) and your reference to setExtendLastRow()
didn't help either (mentioning that method doesn't make sense to me; it's very confusing).
我必须执行该示例才能理解您的问题。您通过谈论不是标题的标题(带有“标题行正常”的行不是标题行!)而使我感到困惑,并且您对它的引用setExtendLastRow()
也没有帮助(提及该方法对我来说没有意义; 这很令人困惑)。
This being said, the solution to your problem is a no-brainer. I've rewritten the main class:
话虽如此,您的问题的解决方案是轻而易举的。我重写了主类:
public static void main(String[] args) {
try {
Document document = new Document();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 14, 14);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("SplitLastRow.pdf"));
document.open();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 42, 38);
for (int m = 1; m < 20; m++) {
int row = 0;
PdfPTable table = new PdfPTable(1);
table.setSpacingAfter(0);
table.setSpacingBefore(0);
table.setTotalWidth(document.right() - document.left());
table.setLockedWidth(true);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
add(table, "Header Row continued " + m, BaseColor.LIGHT_GRAY, row++);
add(table, "Header Row normal " + m, BaseColor.LIGHT_GRAY, row++);
add(table, "Text Row 1 ", BaseColor.WHITE, row++);
add(table, "Text Row 2 ", BaseColor.WHITE, row++);
add(table, "Text Row 3 ", BaseColor.WHITE, row++);
addPadding(table);
if (writer.getVerticalPosition(true) - table.getRowHeight(0) - table.getRowHeight(1) < document.bottom()) {
document.newPage();
}
document.add(table);
}
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
Make sure you define a total widthinstead of a width percentage, and lockthe width. As documented (and as common sensetells you), a PdfPTable
object doesn't know its actual width if you define a width percentage. It goes without saying that you can't calculate the height of a table that doesn't know it's actual width.
确保您定义的是总宽度而不是宽度百分比,并锁定宽度。正如所记录的(以及常识告诉您的),PdfPTable
如果您定义宽度百分比,则对象不知道其实际宽度。不用说,您无法计算不知道实际宽度的表格的高度。
Then use getVerticalPosition()
method to get the current position of the cursor, and check if the first two rows fit on the page. If they don't go to a new page before adding the table. If you want to check if the complete table fits, use the getTotalHeight()
method instead of the getRowHeight()
method.
然后使用getVerticalPosition()
方法获取光标的当前位置,并检查前两行是否适合页面。如果他们在添加表格之前没有转到新页面。如果要检查整个表格是否适合,请使用getTotalHeight()
方法而不是getRowHeight()
方法。
回答by Subhadeep Ray
you can table.setKeepRowsTogather(true);
你可以 table.setKeepRowsTogather(true);
table.setHeaderRows(1)
as well alongwith it
table.setHeaderRows(1)
也随之而来
setKeepRowsTogather()
checks if it can keep all the rows in page but splits the rows in case the table spans multiple pages. In that case setHeaderRows(1)
will put the header rows again in the next page.
setKeepRowsTogather()
检查它是否可以将所有行保留在页面中,但在表跨越多个页面的情况下拆分行。在这种情况下,setHeaderRows(1)
将在下一页再次放置标题行。
回答by MorningDew
You can do
你可以做
table.setSplitRows(false);
But I believe that when there is a row that wont fit it just wont be shown. It's worth a shot though
但我相信,当有一行不适合时,它就不会显示出来。不过值得一试