title: How2: Add a JPanel to a Document then export to PDF
body: <p>first post ever in any forum with regard to programming... i usually just search until i find the answer... but this time im really stuck...</p>
  
  <p>here's the problem...
  i have a JPanel, and recently discovered that itext provides you with a way to export Java GUI to PDF...</p>
  
  <p>i cant seem to understand itext's language nor how to add a simple JPanel to a document then export that document to a PDF... this is all i have at the moment...</p>
  
  <pre><code>import java.io.FileOutputStream;
  import com.itextpdf.text.*;
  import com.itextpdf.text.pdf.*;
  
  import java.awt.Color;
  import javax.swing.*;
  public class HelloWorld {
  
  public static void main(String[] args) {
      try
      {
          //Panel creation and setup
          JPanel panel    = new JPanel();
  
          //just to ensure that the panel has content...
          JLabel label    = new JLabel("i am a label");
          panel.add(label);
          panel.setSize(100,100);
          //so that even if the label doesnt get added... 
          //i can see that the panel does
          panel.setBackground(Color.red);
  
  
          //my understanding of the code below: the virtual document
          Document document   = new Document();
  
          //my interpretation just writes the virtual pdf document to the hdd
          PdfWriter writer    = PdfWriter.getInstance
                  (document, new FileOutputStream("C:/test.pdf"));
  
          //begin editing the vpdf
          document.open();
  
  
          //i wanna do something like this
          //document.add(panel);
  
          //end editing the vpdf
          document.close();
  
      } catch (Exception e)
      {
          System.out.println(e);
      }
  }
  </code></pre>
  
  <p>please please help... i tried to make the code as short as possible to avoid useless stuff...</p>
  
  <p>thanks in advance...
  Craig</p>