Cha*_*kar 9 java itext itextpdf
我正在使用java在itext 5上工作.我的页面包含多个动态行表.在某些情况下,表格的最后一行将使用下面的标题拆分为下一页.我正在使用setHeaderRows()并setSkipFirstHeader()管理下一页的继续.最后一行有足够的空间放在前面的页面上.我想将最后一行放在同一页面而不是下一页.
例如,在第1页上,最后一行被拆分为下一页的第一行.相反,我想在第1页中安装该行,因此请保留一个包含所有空白的额外页面.
我尝试过使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
您可以 table.setKeepRowsTogather(true);
table.setHeaderRows(1) 以及它
setKeepRowsTogather()检查它是否可以保留页面中的所有行,但是如果表跨越多个页面则拆分行.在这种情况下,setHeaderRows(1)将在下一页中再次放置标题行.
我不得不执行这个例子来理解你的问题.你通过谈论一个不是标题的标题让你感到困惑("Header Row normal"的行不是标题行!)你的引用setExtendLastRow()也没有帮助(提到这个方法对我来说没有意义;这非常令人困惑).
话虽这么说,解决你的问题是一个明智的选择.我重写了主要课程:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
确保定义总宽度而不是宽度百分比,并锁定宽度.正如文档所述(并且常识告诉您),PdfPTable如果您定义宽度百分比,则对象不知道其实际宽度.不言而喻,你无法计算不知道它的实际宽度的表的高度.
然后使用getVerticalPosition()方法获取光标的当前位置,并检查前两行是否适合页面.如果他们在添加表之前没有转到新页面.如果要检查整个表是否适合,请使用getTotalHeight()方法而不是getRowHeight()方法.
| 归档时间: |
|
| 查看次数: |
18620 次 |
| 最近记录: |