title: How to print bufferedImage on pdf
body: <p>I am working on pdf report application. Using</p>
  
  <p>itext 5.3.3.jar
  jdk 1.6</p>
  
  <p>I am trying to print JTable on the pdf. I got the JTable image correctly, but unable to get the image on pdf. Is their anyway I can print the BufferedImage on the pdf. I used the below code for it:-</p>
  
  <p>Trying to write a bufferedImage on pdf if possible. </p>
  
  <pre><code>private static void print(BufferedImage bi,JTable table) { 
      Document document = new Document(PageSize.A4.rotate()); 
      try { 
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/jTable.pdf")); 
  
        document.open(); 
        PdfContentByte cb = writer.getDirectContent(); 
        PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight()); 
        Image image = Image.getInstance(bi , null); 
        image.scaleToFit(table.getWidth(), table.getHeight()); 
        table.addNotify();
        table.validate();
        Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight()); 
        table.print(g2);
        pdfTemplate.addImage(image); 
        /*g2.setColor(Color.BLACK); 
        g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/ 
        //table.print(g2); 
  
        cb.addTemplate(pdfTemplate, 0,20); 
  
  
  
      } catch (Exception e) { 
        System.err.println(e.getMessage()); 
      } 
      document.close(); 
    } 
  </code></pre>
  
  <p>But it shows me something like below:-</p>
  
  <p>The image must have absolute positioning.
  Exception in thread "main" com.itextpdf.text.exceptions.IllegalPdfSyntaxException: Unbalanced save/restore state operators.
      at com.itextpdf.text.pdf.PdfContentByte.sanityCheck(PdfContentByte.java:3083)
      at com.itextpdf.text.pdf.PdfContentByte.toPdf(PdfContentByte.java:248)
      at com.itextpdf.text.pdf.PdfFormXObject.(PdfFormXObject.java:88)
      at com.itextpdf.text.pdf.PdfTemplate.getFormXObject(PdfTemplate.java:247)
      at com.itextpdf.text.pdf.PdfWriter.addXFormsToBody(PdfWriter.java:1293)
      at com.itextpdf.text.pdf.PdfWriter.addSharedObjectsToBody(PdfWriter.java:1304)
      at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1209)
      at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:768)
      at com.itextpdf.text.Document.close(Document.java:398)
      at com.swing.data.TableImage.print(TableImage.java:102)
      at com.swing.data.TableImage.main(TableImage.java:52)</p>
  
  <p>Following is the code for testing:-</p>
  
  <pre><code>package com.swing.data;
  
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.image.BufferedImage;
  import java.io.FileOutputStream;
  
  import javax.swing.ImageIcon;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.table.JTableHeader;
  
  import com.itextpdf.text.Document;
  import com.itextpdf.text.Image;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfTemplate;
  import com.itextpdf.text.pdf.PdfWriter;
  
  
  
  class TableImage {
      BufferedImage bi = null;
      public static void main(String[] args) throws Exception {
          Object[][] data = {
              {"Hari", new Integer(23), new Double(78.23), (true)},
              {"James", new Integer(23), new Double(47.64), (false)},
              {"Sally", new Integer(22), new Double(84.81), (true)}
          };
          String[] columns = {"Name", "Age", "GPA", "Pass"};
          JTable table = new JTable(data, columns);
          JScrollPane scroll = new JScrollPane(table);
          JPanel p = new JPanel(new BorderLayout());
          p.add(scroll, BorderLayout.CENTER);
          JOptionPane.showMessageDialog(null, p);
          JTableHeader h = table.getTableHeader();
          int x = table.getWidth();
          int y = table.getHeight();
          table.setIntercellSpacing(new Dimension(0,0));
          table.setDefaultRenderer(Object.class, new ColumnAlignmentRenderer(table.getDefaultRenderer(Object.class)));
          BufferedImage bi = new BufferedImage(
                  x, y, BufferedImage.TYPE_INT_RGB);
          Graphics g = bi.createGraphics();
          Graphics2D g2 = (Graphics2D) g;
          table.paint(g2);
          JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
          print(bi,table);
          //createPdf(true,table);
      }
     private static void print(BufferedImage bi,JTable table) { 
          Document document = new Document(PageSize.A4.rotate()); 
          try { 
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/jTable.pdf")); 
  
            document.open(); 
            PdfContentByte cb = writer.getDirectContent(); 
            PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight()); 
            Image image = Image.getInstance(bi , null); 
            image.scaleToFit(table.getWidth(), table.getHeight()); 
            table.addNotify();
            table.validate();
            Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight()); 
            table.print(g2);
            pdfTemplate.addImage(image); 
            /*g2.setColor(Color.BLACK); 
            g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/ 
            //table.print(g2); 
  
            cb.addTemplate(pdfTemplate, 0,20); 
  
  
  
          } catch (Exception e) { 
            System.err.println(e.getMessage()); 
          } 
          document.close(); 
        } 
       private TableImage() {
      }
  }
  </code></pre>
  
  <p>Thanks!</p>