Sonia Mathews
Sonia Mathews

Reputation: 89

How to add tables using IText without casting exception?

I am trying to add a table to a pdf document generated by IText, however I get a cast exception error( com.itextpdf.text.pdf.PdfPTable cannot be cast to com.itextpdf.layout.element.IBlockElement) when I run the file. How would I solve this?

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import  com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IBlockElement;

import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
...


PdfWriter writer = new PdfWriter(dest);
            // Creating a PdfDocument       
            PdfDocument pdf = new PdfDocument(writer);
        ...
            Document document = new Document(pdf);

PdfPTable p = new PdfPTable(5);
            p.setWidthPercentage(100);
            Font neg = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK);
            PdfPCell cell1 = new PdfPCell(new Phrase("Item", neg));
            PdfPCell cell2 = new PdfPCell(new Phrase("Part No", neg));
            PdfPCell cell3 = new PdfPCell(new Phrase("Unit Cost", neg));
            PdfPCell cell4 = new PdfPCell(new Phrase("Qunatity", neg));
            PdfPCell cell5 = new PdfPCell(new Phrase("Cost", neg));

            p.addCell(cell1);
            p.addCell(cell2);
            p.addCell(cell3);
            p.addCell(cell4);
            p.addCell(cell5);

            for(SparePart s :sp ){
                p.addCell(s.getPartName());
                p.addCell(s.getCode());
                p.addCell(s.getPrice().toString());
                p.addCell(Integer.toString(s.getQnty_Used()));
                p.addCell(s.getTotalPrice().toString());
            }
            document.add((IBlockElement) p);

            document.close();

But when I run the file I get this error: enter image description here

Note when I remove the 'IBlockElement' I get a compile time error.(no suitable method found for add(PdfPTable) method RootElement.add(IBlockElement) is not applicable (argument mismatch; PdfPTable cannot be converted to IBlockElement) method RootElement.add(Image) is not applicable...)

Also for reference these are the corresponding dependencies stored in my .pom file, not sure if there something missing/conflicting that is causing the issue

 <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>kernel</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>io</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>layout</artifactId>         
            <version>7.0.2</version>
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>forms</artifactId>         
            <version>7.0.2</version>    
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>pdfa</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>sign</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>barcodes</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>font-asian</artifactId>         
            <version>7.0.2</version>     
        </dependency>  

        <dependency>         
            <groupId>com.itextpdf</groupId>         
            <artifactId>hyph</artifactId>         
            <version>7.0.2</version>    
        </dependency> 
        <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

Upvotes: 1

Views: 2428

Answers (1)

Uladzimir Asipchuk
Uladzimir Asipchuk

Reputation: 2458

You're using both iText5 and iText7 classes, avoid that.

that's how one can add a table to a document in iText7:

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
    Document doc = new Document(pdfDoc);

    Table table = new Table(new float[]{50, 50})
            .addCell(new Cell().add(new Paragraph("cell 1, 1")))
            .addCell(new Cell().add(new Paragraph("cell 1, 2")));
    doc.add(table);
    doc.close();

These are the right iText7 imports:

    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.itextpdf.layout.element.Cell;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.element.Table;

As a rule, in iText7 one can avoid using classes which start with com.itextpdf.text., so the last of your dependencies should be removed.

Upvotes: 2

Related Questions