Java 为什么下一页的空白表格行-看起来像iText 5.5.10中的一个bug
根据要求,我必须显示一个部分、一些内容,然后是一个只有两列的表。奇怪的是,在生成表时,下一页会出现一个完整的空行。目前我使用的是iText 5.5.10版本。这似乎是itext 5.5.10版本中的一个bug。我认为,由于表中的单元格具有固定的高度,因此在生成时,最后一个单元格无法容纳在页面中,这就是它试图创建空白单元格的原因 见下图 我在下面提供了可以在eclipse中运行以测试生成的pdf的完整代码Java 为什么下一页的空白表格行-看起来像iText 5.5.10中的一个bug,java,itext,Java,Itext,根据要求,我必须显示一个部分、一些内容,然后是一个只有两列的表。奇怪的是,在生成表时,下一页会出现一个完整的空行。目前我使用的是iText 5.5.10版本。这似乎是itext 5.5.10版本中的一个bug。我认为,由于表中的单元格具有固定的高度,因此在生成时,最后一个单元格无法容纳在页面中,这就是它试图创建空白单元格的原因 见下图 我在下面提供了可以在eclipse中运行以测试生成的pdf的完整代码 package com.itc.apollo.tables; import java.i
package com.itc.apollo.tables;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPHeaderCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class BlankCell {
public static final String DEST = "results/tables/blankcell.pdf";
private Paragraph createCellParagraph(String text, Font font, int align, boolean isLeftIndent) {
Paragraph cellPara = new Paragraph(text, font);
if (isLeftIndent)
cellPara.setIndentationLeft(10);
else
cellPara.setIndentationRight(10);
cellPara.setAlignment(align);
return cellPara;
}
private PdfPCell createTableCell(int colspan) {
PdfPCell tableCell = new PdfPCell();
tableCell.setFixedHeight(22.5F);
tableCell.setUseAscender(true);
tableCell.setColspan(colspan);
tableCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
return tableCell;
}
private void insertCell(PdfPTable table, String text, int align, int colspan, Font font, boolean isLeftIndent, BaseColor color) {
Paragraph para = createCellParagraph(text, font, align, isLeftIndent);
PdfPCell tabCell = createTableCell(colspan);
tabCell.addElement(para);
if (null != color) {
tabCell.setBackgroundColor(color);
}
table.addCell(tabCell);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static PdfPTable getBodyTable() {
PdfPTable bodyTable = new PdfPTable(2);
bodyTable.setWidthPercentage(100);
bodyTable.setSplitLate(false);
bodyTable.setSpacingAfter(0);
bodyTable.setSpacingBefore(0);
return bodyTable;
}
public static PdfPCell getHeaderCell(String headerName) {
PdfPHeaderCell headCell = new PdfPHeaderCell();
headCell.setColspan(2);
headCell.setBorder(Rectangle.NO_BORDER);
headCell.setPaddingLeft(22.5f);
Font SECTION_HEADING_FONT = FontFactory.getFont("Open Sans", 14f, Font.NORMAL, new BaseColor(77, 55, 51));
Paragraph headerParagraph = new Paragraph(headerName, SECTION_HEADING_FONT);
headCell.addElement(headerParagraph);
PdfPTable tab = createBlankTable();
headCell.addElement(tab);
return headCell;
}
private static PdfPTable createBlankTable() {
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell tableCell = new PdfPCell();
tableCell.setBorder(0);
tableCell.setFixedHeight(70.9f);
tableCell.setUseAscender(true);
tableCell.setUseDescender(true);
tableCell.setColspan(1);
table.addCell(tableCell);
return table;
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable _tableLast = getBodyTable();
PdfPCell headerCell = getHeaderCell("Objectives");
_tableLast.addCell(headerCell);
PdfPCell courseCell = new PdfPCell();
courseCell.setBorder(Rectangle.NO_BORDER);
courseCell.setColspan(2);
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
Font Core_Course_FONT = FontFactory.getFont("Open Sans", 12f, Font.NORMAL, new BaseColor(000, 000, 000));
for (int i = 0; i < 80; i++) {
if (i % 2 == 0) {
insertCell(table, "Left Side - "+i, Element.ALIGN_LEFT, 2, Core_Course_FONT, true, new BaseColor(221, 211, 199));
insertCell(table, "Right Side - "+i, Element.ALIGN_RIGHT, 1, Core_Course_FONT, false,new BaseColor(221, 211, 199));
}
else {
insertCell(table, "Left Side - "+i, Element.ALIGN_LEFT, 2, Core_Course_FONT, true, null);
insertCell(table, "Right Side - "+i, Element.ALIGN_RIGHT, 1, Core_Course_FONT, false, null);
}
}
courseCell.addElement(table);
_tableLast.addCell(courseCell);
// document.add(table);
document.add(_tableLast);
document.close();
}
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new BlankCell().createPdf(DEST);
System.out.println("******************** PDF Created *********************");
}
}
package com.itc.apollo.tables;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入com.itextpdf.text.BaseColor;
导入com.itextpdf.text.Document;
导入com.itextpdf.text.DocumentException;
导入com.itextpdf.text.Element;
导入com.itextpdf.text.Font;
导入com.itextpdf.text.FontFactory;
导入com.itextpdf.text.PageSize;
导入com.itextpdf.text.paragration;
导入com.itextpdf.text.Rectangle;
导入com.itextpdf.text.pdf.PdfPCell;
导入com.itextpdf.text.pdf.PdfPHeaderCell;
导入com.itextpdf.text.pdf.PdfPTable;
导入com.itextpdf.text.pdf.PdfWriter;
公共类空白单元格{
公共静态最终字符串DEST=“results/tables/blankcell.pdf”;
私有段落CreateCellParagation(字符串文本、字体字体、整型对齐、布尔isLeftIndent){
段落cellPara=新段落(文本、字体);
如果(isLeftIndent)
cellPara.设置缩进左(10);
其他的
cellPara.setIndentationRight(10);
cellPara.setAlignment(对齐);
返回cellPara;
}
专用PdfPCell createTableCell(int colspan){
PdfPCell tableCell=新的PdfPCell();
表单元设置固定高度(22.5F);
tableCell.setUseAcender(true);
tableCell.setColspan(colspan);
tableCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
tableCell.setHorizontalAlignment(PdfPCell.Alignment_CENTER);
返回表单元;
}
私有void insertCell(PdfPTable表格、字符串文本、int-align、int-colspan、字体、布尔isLeftIndent、基色){
段落段落=创建单元格段落(文本、字体、对齐、isLeftIndent);
PdfPCell tabCell=createTableCell(colspan);
tabCell.附录(第6段);
如果(空!=颜色){
tabCell.setBackgroundColor(颜色);
}
表2.addCell(tabCell);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
公共静态PdfPTable getBodyTable(){
PdfPTable bodyTable=新PdfPTable(2);
车身。设置宽度百分比(100);
bodyTable.setSplitLate(假);
bodyTable.setSpacingAfter(0)之后的设置间隙;
bodyTable.setSpacingBefore(0);
返回式车身工作台;
}
公共静态PdfPCell getHeaderCell(字符串headerName){
PdfPHeaderCell headCell=新PdfPHeaderCell();
头细胞。setColspan(2);
头部单元格。设置顺序(矩形。无边框);
头部细胞。设置填充左侧(22.5f);
Font SECTION_HEADING_Font=FontFactory.getFont(“开放式SAN”,14f,Font.NORMAL,新基色(77,55,51));
段落标题Paragraph=新段落(标题名称,章节标题字体);
头细胞。附加元素(头细胞参数图);
PdfPTable tab=createBlankTable();
头部细胞。附加元素(选项卡);
返回头细胞;
}
私有静态PdfPTable createBlankTable(){
PdfPTable table=新的PdfPTable(1);
表1.设定宽度百分比(100);
PdfPCell tableCell=新的PdfPCell();
表单元格的顺序(0);
表单元设置固定高度(70.9f);
tableCell.setUseAcender(true);
tableCell.setusedowner(true);
tableCell.setColspan(1);
table.addCell(tableCell);
返回表;
}
public void createPdf(字符串dest)引发IOException、DocumentException{
文件=新文件(页面大小为A4);
getInstance(文档,新文件输出流(dest));
document.open();
PdfPTable_tableLast=getBodyTable();
PdfPCell-headerCell=getHeaderCell(“目标”);
_tableLast.addCell(headerCell);
PdfPCell courseCell=新的PdfPCell();
courseCell.SetBeOrder(矩形,无边框);
courseCell.setColspan(2);
PdfPTable=新的PdfPTable(3);
表1.设定宽度百分比(100);
Font Core_Course_Font=FontFactory.getFont(“开放式SAN”,12f,Font.NORMAL,新基色(000000000));
对于(int i=0;i<80;i++){
如果(i%2==0){
insertCell(表,“左侧-”+i,Element.ALIGN_Left,2,Core_Course_FONT,true,新基色(221211199));
insertCell(表,“右侧-”+i,Element.ALIGN_Right,1,Core_Course_FONT,false,新基色(221211199));
}
否则{
insertCell(表,“左侧-”+i,Element.ALIGN\u Left,2,核心\u Course\u字体,true,null);
insertCell(表格,“右侧-”+i,Element.ALIGN\u Right,1,核心\u Course\u字体,false,null);
}
}
courseCell.附录(表);
_tableLast.addCell(courseCell);
//文件。添加(表);
文件。添加(_tableLast);
document.close();
}
公共静态void main(字符串[]args)引发IOException、DocumentException{
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新建BlankCell().createPdf(DEST);
System.out.println(“*********************************************创建的PDF*************************”);
}
}
请帮助我解决问题,并让我们知道哪个版本的iText可以解决此类问题。我想这是因为最后一行与页面不符,是的。如果将
目标
字体大小更改为16f
,则非常适合。我不认为这是臭虫