2012-07-07 225 views
0

感谢您花时间回答我的问题。我正在动态地填充PDF模板中的数据。除了一部分,一切都按预期工作。我在Java代码中生成的表不显示在PDF中。我的问题是如何使用iText的API在左上角放置一张桌子?如何在iText中定位表格

这是到目前为止我的代码:

PdfPTable table = new PdfPTable(3); 
PdfPCell cell = new PdfPCell(new Phrase("VAT Rate")); 
table.setWidthPercentage(500); 
table.addCell(cell); 
cell = new PdfPCell(new Phrase("Net Value")); 
table.addCell(cell); 
table.addCell("VAT Amount"); 
table.addCell(Double.toString(currentVatRate)); 
table.addCell(Double.toString(currentVatRateNetValue)); 
table.addCell(Double.toString(currentVatVatAmount)); 
table.addCell("Totals"); 
table.addCell(Double.toString(subTotalExcludingVat)); 
table.addCell(Double.toString(totalVatAmount)); 

基本上我需要一个3列的表,每个列33%。我如何设置表格的位置?

回答

2

试试这个:

// table.setWidthPercentage(500); 
table.setWidths(new int[]{ 1, 1, 1 }); 

你应该使用类似下面的代码:

com.itextpdf.text.Document document = 
    new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4); 
FileOutputStream fos = new FileOutputStream(outputFileFolder + "PdfTableExample.pdf"); 
com.itextpdf.text.pdf.PdfWriter pdfWriter = 
    com.itextpdf.text.pdf.PdfWriter.getInstance(document, fos); 

document.open(); 
document.add(table); 
document.close(); 
+0

是做什么都没有。我仍然没有看到表格 – Dragan 2012-07-07 14:36:36

+0

@Dragan它对我来说非常合适。你可以发布添加ptable到文档的java代码吗? – 2012-07-07 14:49:08