我正在创建具有许多表的PDF文档,并且当表只有一行时,不会将其添加到文档中。我在另一篇文章中看到了该行是否已完成,并添加了table.completeRow()
以防万一。但是,唯一可行的方法是添加另一个空行:
table.addCell(new Paragraph("", paragraphFontNormal));
table.completeRow()
我认为这不是一个很好的解决方案,我想知道这里出了什么问题。有人可以帮忙吗?
这是方法:
public void createPdf(DateRange dates, HttpServletResponse response) throws DocumentException, IOException {
boolean first = true;
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
List<Rejects> report = findAll(dates);
for(Rejects rejects : report){
if (first) {
first = false;
addGroupAdmin(rejects, document, dates);
}else{
if (rejects.getAdminGroup().equals(adminGroup)) {
table.addCell(new Paragraph(rejects.getZone(), paragraphFontNormal));
table.addCell(new Paragraph(dateFormat.format(rejects.getRejDate()), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getProcName(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getKindId(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getVarietyName(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getSlrn(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getRejReason(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getAdminGroup(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getDescription(), paragraphFontNormal));
}else{
/*** It doesn't work like this for one row***/
document.add(table);
document.newPage();
addGroupAdmin(rejects, document, dates);
}
}
}
/*** It works like this ***/
table.addCell(new Paragraph("", paragraphFontNormal));
table.completeRow();
document.add(table);
document.newPage();
response.flushBuffer();
document.close();
}
这是addGroupAdmin方法:
private void addGroupAdmin(Rejects rejects, Document document, DateRange dates) throws DocumentException {
adminGroup = rejects.getAdminGroup();
Paragraph headLine = new Paragraph("REJECTED APPLICATION LIST " + dateFormat.format(dates.getStartDate())
+ "-" + dateFormat.format(dates.getEndDate()) + " - " + rejects.getAdminGroupDesc() , paragraphFontBold);
headLine.setAlignment(Element.ALIGN_CENTER);
document.add(headLine);
document.add( Chunk.NEWLINE );
table = new PdfPTable(9);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setMinimumHeight(20);
table.getDefaultCell().setPaddingBottom(10);
table.setWidthPercentage(900);
table.setHeaderRows(1);
table.setTotalWidth(new float[]{ 40, 60, 140, 30, 60, 90, 30, 90, 100 });
table.setLockedWidth(true);
table.addCell(new Paragraph(rejects.getZone(), paragraphFontNormal));
table.addCell(new Paragraph(dateFormat.format(rejects.getRejDate()), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getProcName(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getKindId(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getVarietyName(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getSlrn(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getRejReason(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getAdminGroup(), paragraphFontNormal));
table.addCell(new Paragraph(rejects.getDescription(), paragraphFontNormal));
}
答案 0 :(得分:2)
您设置
table.setHeaderRows(1);
这意味着您添加到表中的第一行将用作标题行。
因此,如果
该表只有一行
然后该表具有标题行,但没有内容行。 iText会忽略没有内容行的表。因此,
它不会将其添加到文档中。
因此解决方案是不要在您的HeaderRows
方法中设置addGroupAdmin
!