title: How to add tables using IText without casting exception?
body: <p>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?</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>But when I run the file I get this error:
  <a href="https://i.stack.imgur.com/iJBFo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iJBFo.png" alt="enter image description here"></a></p>
  
  <p>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...)</p>
  
  <p>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</p>
  
  <pre><code> &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;kernel&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;io&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;layout&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;forms&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;    
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;pdfa&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;sign&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;barcodes&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;font-asian&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;     
          &lt;/dependency&gt;  
  
          &lt;dependency&gt;         
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;         
              &lt;artifactId&gt;hyph&lt;/artifactId&gt;         
              &lt;version&gt;7.0.2&lt;/version&gt;    
          &lt;/dependency&gt; 
          &lt;!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.itextpdf&lt;/groupId&gt;
              &lt;artifactId&gt;itextpdf&lt;/artifactId&gt;
              &lt;version&gt;5.5.13&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>