title: Export Swing Component to PDF using iText
body: <p>I have a frame and there are several components on it. How do I paint this JFrame using iText?</p>
  
  <p>I write some code as follow(but does not work):</p>
  
  <pre><code>public void PrintFrameToPDF(Component c, File file) {
      try {
          Document d = new Document();
          PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(file));
          d.open();
  
          PdfContentByte cb = writer.getDirectContent();
          PdfTemplate template = cb.createTemplate(c.getWidth(), c.getHeight());
          Graphics2D g2d = template.createGraphics(c.getWidth(), c.getHeight());
          c.paintAll(g2d);
          c.addNotify();
          c.validate();
          g2d.dispose();
  
          d.close();
      } catch (Exception e) {
          //
      }
  }
  </code></pre>