javapdfitextpdfptable

Create a iTextPdf PdfPtable in Java with one row without a border


I am trying to generate a table in my pdf document that should have two rows. The first row should not be split in columns but the second row should be split in two columns. Something that looks like below:

enter image description here

So far I have been able to create a table inside a PdfPCell but that is not how I want my table to look. Is there a way I can do this?

Edited: I am using itextpdf-5.5.13.3 with pdfbox-3.0.0 and commons-lang3-3.14.0.


Solution

  • You don't specify which version you use, nor an example but with itext8 you can use row and col span overload in Cell.

    Something like this should do the trick:

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
    
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        Cell row = new Cell(1, 2).add(new Paragraph(" Row 1 full "));
        table.addCell(row);
    
        Cell row2Col1 = new Cell().add(new Paragraph(" row 2 col 1 "));
        table.addCell(row2Col1);
        Cell row2Col2 = new Cell().add(new Paragraph("row 2 col 2"));
        table.addCell(row2Col2);`
        pdfDoc.close();
    

    More advanced examples can be found here itext examples