Java 使用 iText 在 PDF 表格中添加一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24359321/
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
Adding a row to a table in PDF using iText
提问by Srinivasan
What I'm getting in this code is a table with 8 columns and 3 rows.
我在这段代码中得到的是一个有 8 列和 3 行的表格。
What should I do to get just 2 rows? And the 1st column in the 3 rows are empty, but the remaining cells are filled with "hi".
我该怎么做才能得到 2 行?并且 3 行中的第一列是空的,但剩余的单元格填充了“hi”。
Code:
代码:
PdfPTable table = new PdfPTable(8);
PdfPCell cell;
cell = new PdfPCell();
cell.setRowspan(2);
table.addCell(cell);
for(int aw=0;aw<8;aw++){
table.addCell("hi");
}
回答by Nishanthi Grashia
Try this:
尝试这个:
PdfPTable table = new PdfPTable(8);
PdfPCell cell;
for(int aw=0;aw<8;aw++)
{
cell = new PdfPCell(new Paragraph("hi"));
table.addCell(cell );
}
EDIT:
编辑:
// Step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfPTable table = new PdfPTable(8);
for(int aw=0;aw<16 ; aw++){
table.addCell("hi");
}
// Step 5
document.add(table);
// step 6
document.close();
See SimpleTablefor the full sample code and the resulting PDF:
有关完整示例代码和生成的 PDF,请参阅SimpleTable:
As you can see in the screen shot, the table has 8 columns and 2 rows (as expected).
正如您在屏幕截图中看到的,该表有 8 列和 2 行(如预期)。
Reading the original question, I see that the first column has a cell with colspan 2. It's only a small change to take this into account:
阅读原始问题,我看到第一列有一个 colspan 2 的单元格。考虑到这一点,这只是一个很小的变化:
PdfPTable table = new PdfPTable(8);
PdfPCell cell = new PdfPCell(new Phrase("hi"));
cell.setRowspan(2);
table.addCell(cell);
for(int aw = 0; aw < 14; aw++){
table.addCell("hi");
}
Now the result looks like this:
现在的结果是这样的:
Again 8 columns and two rows, but now the cell in the first column spans two rows.
同样是 8 列和两行,但现在第一列中的单元格跨越了两行。
回答by Robby Cornelissen
Your creating a table with 8 columns and adding 17 cells in total. This obviously results in three rows being created. Perhaps you meant to do this:
您创建了一个包含 8 列并总共添加 17 个单元格的表格。这显然会导致创建三行。也许你打算这样做:
PdfPTable table = new PdfPTable(8);
for(int aw=0;aw<16;aw++){
table.addCell("hi");
}
回答by anji.k.mca
dear srinivasan the code you added will print only one row with 7 columns and first column will have rowspan2. To print 8 rows ,you took table of 8 columns and in for loop for every increment of variable 'aw' one cell will be added to the table and for every multiple of 8 of 'aw' new row will be created.so to get 8 rows try folowwing for loop in code:
亲爱的 srinivasan,您添加的代码将仅打印一行 7 列,第一列将具有 rowspan2。要打印 8 行,您使用 8 列的表格,并在 for 循环中为变量 'aw' 的每个增量添加一个单元格,并且对于 'aw' 的每 8 的倍数将创建新行。所以得到8 行在代码中尝试 flowwing for 循环:
PdfPTable table = new PdfPTable(8);
for(int aw=0;aw<64;aw++)
{
table.addCell("hi");
}
回答by G_V
iText 7 in 2019.
2019 年的 iText 7。
When making a Table object, you specify the width as the number of columns you want to add.
创建 Table 对象时,您将宽度指定为要添加的列数。
int numberOfColumns = 10;
Table table = new Table(numberOfColumns);
Then you can set how wide you want the whole table to be etc.
然后你可以设置你想要整个桌子的宽度等。
table.setWidth(new UnitValue(UnitValue.PERCENT, 50));
table.setMaxWidth(new UnitValue(UnitValue.PERCENT, 100));
table.setFontSize(6f);
After which you can add cells with content and manipulate their look:
之后,您可以添加带有内容的单元格并操纵它们的外观:
Cell cell = new Cell();
cell.setBorder(Border.NO_BORDER);
Paragraph paragraph = new Paragraph("hi");
paragraph.setTextAlignment(TextAlignment.LEFT);
paragraph.setBold();
//Instead of text you can add images and such in the same way
cell.add(paragraph);
//Insert cell into table
table.addCell(cell);
iText will simply keep adding your cells until it runs out of slots for cells on that row and then creates a new row by itself and starts injecting cells there.
iText 将简单地继续添加您的单元格,直到它用完该行上的单元格的插槽,然后自己创建一个新行并开始在那里注入单元格。