java - 针对特定表格布局裁剪单元格数据。iText 2.1.7
问题描述
对于具有较大数据的特定单元格的特定表格布局,它无法正确导出为 PDF,该特定单元格中的数据被裁剪。
private static void tableLayout1() {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream(
".\\TableLayout_1.pdf"));
doc.open();
// Table
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100f);
table.setSplitLate(false);
//row 1
PdfPCell cell00 = new PdfPCell(new Paragraph("Cell00"));
cell00.setBackgroundColor(new Color(200, 0, 0));
cell00.setRowspan(3);
PdfPCell cell01 = new PdfPCell(new Paragraph("Cell01"));
cell01.setBackgroundColor(new Color(0, 200, 0));
cell01.setColspan(2);
//row 2
PdfPCell cell11 = new PdfPCell(new Paragraph("Cell11"));
cell11.setBackgroundColor(new Color(100, 100, 0));
PdfPCell cell12 = new PdfPCell( getLongCellData() );
cell12.setBackgroundColor(new Color(0, 200, 200));
cell12.setRowspan(2);
//row 3
PdfPCell cell21 = new PdfPCell(new Paragraph("Cell21"));
cell21.setBackgroundColor(new Color(200, 0, 200));
table.addCell(cell00);
table.addCell(cell01);
table.addCell(cell11);
table.addCell(cell12);
table.addCell(cell21);
doc.add(table);
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
}
doc.close();
}
private static Paragraph getLongCellData() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("Cell12_" + i);
sb.append("\n");
}
return new Paragraph(sb.toString());
}
此处来自 cell12 的数据(其行跨度为 2 并且具有更大的数据)在页面末尾被裁剪,并且不会在下一页继续。尝试将此表分为两个不同的表,如下所示,但问题仍然存在。
private static void tableLayout2() {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream(
".\\TableLayout_2.pdf"));
doc.open();
// Table
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100f);
table.setSplitLate(false);
table.setWidths(new int[]{1, 2});
//Inner table for column 1
PdfPTable column1Table = new PdfPTable(1);
PdfPCell cell00 = new PdfPCell(new Paragraph("Cell00"));
cell00.setBackgroundColor(new Color(200, 0, 0));
//cell00.setRowspan(3);
column1Table.addCell(cell00);
//Inner table for column 2
PdfPTable column2Table = new PdfPTable(2);
PdfPCell cell01 = new PdfPCell(new Paragraph("Cell01"));
cell01.setBackgroundColor(new Color(0, 200, 0));
cell01.setColspan(2);
PdfPCell cell11 = new PdfPCell(new Paragraph("Cell11"));
cell11.setBackgroundColor(new Color(100, 100, 0));
PdfPCell cell12 = new PdfPCell( getLongCellData() );
cell12.setBackgroundColor(new Color(0, 200, 200));
cell12.setRowspan(2);
PdfPCell cell21 = new PdfPCell(new Paragraph("Cell21"));
cell21.setBackgroundColor(new Color(200, 0, 200));
column2Table.addCell(cell01);
column2Table.addCell(cell11);
column2Table.addCell(cell12);
column2Table.addCell(cell21);
table.addCell(column1Table);
table.addCell(column2Table);
doc.add(table);
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
}
doc.close();
}
这个问题在 iText5 上得到了部分解决,但是,由于某些问题,我们不太可能升级库,所以有什么解决方案吗?
使用 iText5,数据不会被裁剪,但单元格的高度不太合适。对于 TableLayout_2_withIText5.pdf,cell21 应该在 page3 而不是 page4 上呈现。对于 TableLayout_1_withIText5.pdf,cell21 在 Page1 的边距区域中得到扩展。
解决方案
推荐阅读
- r - 足够大文件的 IO 写入错误
- java - 如果用户在没有 Firebase 的情况下有 Internet 连接,请在线检查用户
- ios - CIRadialGradient 减小图像大小
- android - 自定义库中包含的库在示例项目 Android 中不可用/不可见
- reactjs - React + d3v4:无法使用正确的动作在蜂群图中移动节点位置
- java - Google/Tink:如何使用公钥验证签名
- amazon-web-services - 已注册的目标实例在目标组中显示不正常
- reactjs - 使用后退按钮在 Safari 中反应生命周期
- html - Bulma 色谱柱不工作,如何解决?
- angular - 在角度打字稿中重新排列我的数组索引