Srinivasan
Srinivasan

Reputation: 117

Adding a row to a table in PDF using iText

What I'm getting in this code is a table with 8 columns and 3 rows.

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".

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

Upvotes: 0

Views: 42040

Answers (5)

Wenderson J.
Wenderson J.

Reputation: 31

A simple way to use. Example using tile headers (name and lastName), that makes 2 columns, therefore:

Name | Last Name Alfa | Beta Gamma | Epsilon

'values' are my cells. I will just use

var table = PdfPTable(2)
values = ["Alfa", "Beta", "Gamma", "Epsilon"] 
for value in values{ 
  var cell = PdfPCell()
  cell.setPhrase(Phrase(reveza.cpfMotorista))
  table.addCell(cell)
  table.add(value) 
}

and the itextpdf will fill each cell (horizontally) from the table until it achieves 2 columns and will go to the next line.

[used kotlin language]

Upvotes: 0

G_V
G_V

Reputation: 2434

iText 7 in 2019.

When making a Table object, you specify the width as the number of columns you want to add.

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.

Upvotes: 0

anji.k.mca
anji.k.mca

Reputation: 1

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:

PdfPTable table = new PdfPTable(8);      
 for(int aw=0;aw<64;aw++)
 {
   table.addCell("hi");
 }

Upvotes: 0

ngrashia
ngrashia

Reputation: 9884

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 SimpleTable for the full sample code and the resulting PDF:

enter image description here

As you can see in the screen shot, the table has 8 columns and 2 rows (as expected).

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:

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:

enter image description here

Again 8 columns and two rows, but now the cell in the first column spans two rows.

Upvotes: 5

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

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:

PdfPTable table = new PdfPTable(8);

for(int aw=0;aw<16;aw++){
    table.addCell("hi");
}

Upvotes: 0

Related Questions