简体   繁体   English

如何在iText中定位表格

[英]How to position a table in iText

Thanks for taking the time to answer my question. 感谢您抽出宝贵时间回答我的问题。 I'm dynamically filling data in a PDF template. 我正在动态填充PDF模板中的数据。 Everything is working as expected except for one part. 除了一部分外,一切都按预期工作。 The table I am generating in Java code is not displayed in the PDF. 我在Java代码中生成的表格未显示在PDF中。 My question would be how do I position a table in the top left corner using iText's API? 我的问题是如何使用iText的API在左上角放置一张桌子?

This is my code so far: 到目前为止这是我的代码:

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));

Basically I need a 3 column table, each column 33%. 基本上我需要一个3列表,每列33%。 How do I set the tables position? 如何设置表格位置?

Try this: 尝试这个:

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

You should be using something like the following code: 你应该使用类似下面的代码:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM