title: Rendering a table and chart in a single PDF using spring Boot
body: <pre><code>    package com.example.Reporting_Tool.ToolContoller;
  
      import java.awt.Graphics2D;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.ArrayList;
      import java.util.List;
  
      import org.jfree.chart.ChartFactory;
      import org.jfree.chart.JFreeChart;
      import org.jfree.chart.plot.PlotOrientation;
      import org.jfree.data.category.DefaultCategoryDataset;
      import org.jfree.data.general.DefaultPieDataset;
  
      import com.itextpdf.awt.DefaultFontMapper;
      import com.itextpdf.text.BaseColor;
      import com.itextpdf.text.Document;
      import com.itextpdf.text.Element;
      import com.itextpdf.text.pdf.BaseFont;
      import com.itextpdf.text.pdf.PdfContentByte;
      import com.itextpdf.text.pdf.PdfImportedPage;
      import com.itextpdf.text.pdf.PdfPCell;
      import com.itextpdf.text.pdf.PdfPTable;
      import com.itextpdf.text.pdf.PdfReader;
      import com.itextpdf.text.pdf.PdfTemplate;
      import com.itextpdf.text.pdf.PdfWriter;
  
      public class PieChartDemo {
      public static void main(String[] args) throws Exception {
              writeChartToPDF(generateBarChart(), 500, 400, "G://RT_pdf/Sample.pdf");
              writeChartToPDF(generatePieChart(), 500, 400, "G://RT_pdf/piechart.pdf");
          }
          @SuppressWarnings("deprecation")
          public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) throws Exception{
              PdfWriter writer = null;
  
              Document document = new Document();
              com.itextpdf.text.Document document2=new com.itextpdf.text.Document();
                PdfPTable table = new PdfPTable(new float[] { 2, 1, 2 ,1});
                table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                table.addCell("Name");
                    table.addCell("Age");
                    table.addCell("Location");
                    table.addCell("any");
                table.setHeaderRows(1);
                PdfPCell[] cells = table.getRow(0).getCells(); 
                for (int j=0;j&lt;cells.length;j++){
                   cells[j].setBackgroundColor(BaseColor.GRAY);
                }
                    for (int i=1;i&lt;5;i++){
                       table.addCell("Name:"+i);
                       table.addCell("Age:"+i);
                       table.addCell("Location:"+i);
                       table.addCell("any"+i);
                    }
                com.itextpdf.text.pdf.PdfWriter.getInstance(document2, new FileOutputStream("G://RT_pdf/Sample.pdf"));
                document2.open();
                    document2.add(table);
                document2.close();
              try {
                  writer = PdfWriter.getInstance(document, new FileOutputStream(
                          fileName));
                  document.open();
                  PdfContentByte contentByte = writer.getDirectContent();
                  PdfTemplate template = contentByte.createTemplate(width, height);
                  Graphics2D graphics2d = template.createGraphics(width, height,
                          new DefaultFontMapper());
                  java.awt.geom.Rectangle2D rectangle2d = new java.awt.geom.Rectangle2D.Double(0, 0, width,
                          height);
  
                  chart.draw(graphics2d, rectangle2d);
  
                  graphics2d.dispose();
                  contentByte.addTemplate(template, 0, 0);
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
              document.close();
          }
  
  
  
      public static JFreeChart generatePieChart() {
          DefaultPieDataset dataSet = new DefaultPieDataset();
          dataSet.setValue("China", 19.64);
          dataSet.setValue("India", 17.3);
          dataSet.setValue("United States", 4.54);
          dataSet.setValue("Indonesia", 3.4);
          dataSet.setValue("Brazil", 2.83);
          dataSet.setValue("Pakistan", 2.48);
          dataSet.setValue("Bangladesh", 2.38);
  
          JFreeChart chart = ChartFactory.createPieChart(
                  "World Population by countries", dataSet, true, true, false);
  
          return chart;
      }
  
      public static JFreeChart generateBarChart() {
          DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
          dataSet.setValue(791, "Population", "1750 AD");
          dataSet.setValue(978, "Population", "1800 AD");
          dataSet.setValue(1262, "Population", "1850 AD");
          dataSet.setValue(1650, "Population", "1900 AD");
          dataSet.setValue(2519, "Population", "1950 AD");
          dataSet.setValue(6070, "Population", "2000 AD");
  
          JFreeChart chart = ChartFactory.createBarChart(
                  "World Population growth", "Year", "Population in millions",
                  dataSet, PlotOrientation.VERTICAL, false, true, false);
  
          return chart;
      }
  }
  </code></pre>
  
  <ul>
  <li>In this example I am rendering one table and one chart in the pdf by using iText in a spring boot application.</li>
  <li>I'm trying to generate it by just apply in the same file but it will only render one thing and that is table, chart is not rendering in the result.</li>
  <li>I have visited many web sites but I can't find any code to satisfy my needs.</li>
  </ul>