我正在尝试使用java中的itext pdf库将记录从数据库导出到pdf中。但是我在pdf文件内的pdf表对齐方面遇到以下问题。
1.表格未显示在完整的 pdf 页面中。它在 pdf 页面的左侧和右侧留有空格。
2.每个页面只显示一半页面的值。意味着pdf表格显示在一半pdf页面中。
这是我的代码..
Document document = new Document();
PdfWriter.getInstance(document, fos);
PdfPTable table = new PdfPTable(10);
table.setWidthPercentage(100);
table.setSpacingBefore(0f);
table.setSpacingAfter(0f);
PdfPCell cell = new PdfPCell(new Paragraph("DateRange"));
cell.setColspan(10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(5.0f);
cell.setBackgroundColor(new BaseColor(140, 221, 8));
table.addCell(cell);
table.addCell("Calldate");
table.addCell("Calltime");
table.addCell("Source");
table.addCell("DialedNo");
table.addCell("Extension");
table.addCell("Trunk");
table.addCell("Duration");
table.addCell("Calltype");
table.addCell("Callcost");
table.addCell("Site");
while (rs.next()) {
table.addCell(rs.getString("date"));
table.addCell(rs.getString("time"));
table.addCell(rs.getString("source"));
table.addCell(rs.getString("destination"));
table.addCell(rs.getString("extension"));
table.addCell(rs.getString("trunk"));
table.addCell(rs.getString("dur"));
table.addCell(rs.getString("toc"));
table.addCell(rs.getString("callcost"));
table.addCell(rs.getString("Site"));
}
table.setSpacingBefore(5.0f); // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(5.0f); // Space After table starts, like margin-Bottom in CSS
document.open();//PDF document opened........
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.add(new Paragraph("TechsoftTechnologies.com"));
document.add(new Paragraph("Document Generated On - " + new Date().toString()));
document.add(table);
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.newPage(); //Opened new page
//In the new page we are going to add list
document.close();
fos.close();
最佳答案
在我明白你想抑制边距之前,我不得不多次阅读你的问题。我复制/粘贴(并改编)了您的示例,并且我认为我无法重现您的问题,因为我(和许多其他开发人员)已经习惯了页面有边距的事实。
无论如何,这是我的示例版本:FullPageTable它会创建以下 PDF:full_page_table.pdf
我做了一些小的更改,例如:将“Paragraph”作为 PdfPCell
的参数传递是没有意义的,因为 Paragraph
会被视为一个短语
,但我认为您正在寻找的行是:
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
零定义了边距的宽度,如果我正确理解你的问题,你想抑制这些边距。
至于您的指控每个页面仅在一半页面中显示值。意味着pdf表格在一半的pdf页面中显示,我认为您是通过引入自己造成的>document.newPage();
否则你的指控就没有意义;-)
关于java - 如何增加itext Pdf中PdfPTable的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19873263/