Java 如何在itext Pdf中增加PdfPTable的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19873263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to increase the width of PdfPTable in itext Pdf
提问by Adi
I am trying to export records from database into pdf using itext pdf library in java..But i am getting following problems in alignment of pdf table inside pdf file..
我正在尝试使用 java 中的 itext pdf 库将数据库中的记录导出到 pdf 中。
1.Table is not showing in the full pdf page .It is leaving spaces from left and right of the pdf page.
1.表格未显示在完整的pdf页面中。它在pdf页面的左侧和右侧留有空格。
2.Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages..
2.每页仅在页面的一半中显示值。意味着pdf表在pdf页面的一半中显示..
Here is my code..
这是我的代码..
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();
采纳答案by Bruno Lowagie
I had to read your question multiple times before I understood that you wanted to suppress the margins. I copy/pasted (and adapted) your example, and I thought I couldn't reproduce your problem because I (and many other developers) am used to the fact that pages have margins.
我不得不多次阅读您的问题,然后我才明白您想抑制边距。我复制/粘贴(并改编)了您的示例,我认为我无法重现您的问题,因为我(和许多其他开发人员)已经习惯了页面有边距的事实。
Anyway, this is my version of your example: FullPageTableand it creates the following PDF: full_page_table.pdf
无论如何,这是我的示例版本:FullPageTable并创建以下 PDF:full_page_table.pdf
I've made some minor changes, for instance: it doesn't make sense to pass a ′Paragraph′ as a parameter for a PdfPCell
because that Paragraph
will be treated as a Phrase
, but I think the line you're looking for is:
我做了一些小改动,例如:将 'Paragraph' 作为参数传递给 a 是没有意义的,PdfPCell
因为这Paragraph
将被视为 a Phrase
,但我认为您要查找的行是:
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
The zeros define the width of the margin, and if I understand your question correctly, you want to suppress those margins.
零定义边距的宽度,如果我正确理解你的问题,你想抑制这些边距。
As for your allegation Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages, I think you're causing that yourself by introducing document.newPage();
otherwise your allegation doesn't make sense ;-)
至于你的指控,每一页只显示一半页面的值。意思是 pdf 表显示在一半的 pdf 页面中,我认为你是通过引入自己造成的,document.newPage();
否则你的指控没有意义;-)