0

Possible Duplicate:
JTable to PDF converter

I'm trying to save the contents of my current table to a pdf. My problem is that when I save the output, only what is currently displayed on the screen is saved. If a row is not in the current view of the scrollpane, it will not appear in the pdf. I'm using itextpdf-5.2.1.jar. Here is my code.

private JPanel contentPane;
    private JTable table_1;
    private JTable table;
    private DefaultTableModel aModel;
    private JScrollPane pane;
    private String currentTable;
    private Edit edit;

public void createPdf() {
        Document document = new Document();
        try {
            PdfWriter writer;

            writer = PdfWriter.getInstance(document, new FileOutputStream(
                    "mytable.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(500, 500);
            Graphics2D g2;

            g2 = tp.createGraphicsShapes(500, 500);
            pane.printAll(g2);
            g2.dispose();
            cb.addTemplate(tp, 30, 300);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
}
Community
  • 1
  • 1
MCR
  • 1,633
  • 3
  • 21
  • 36
  • 1
    Check [this answer](http://stackoverflow.com/q/7369814/418556) for tips. If that fails to give you the push you need to get it working, try posting an [SSCCE](http://sscce.org/). That would mean factoring out the PDF making, but it seems this is all about the image of the table, anyway. – Andrew Thompson Apr 24 '12 at 18:26
  • Why do you call the `printAll` on the `JScrollPane` and not on the actual `JTable` ? – Robin Apr 24 '12 at 18:28
  • If I call printAll on the JScrollPane my column headers are displayed in the pdf. If I call printAll on the JTable they are not. In both cases the pdf is truncated to what the screen is currently displaying. – MCR Apr 25 '12 at 02:07
  • 1
    Take a look at the [Swing table tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) (section about adding it to a container). You should probably print the headers as well. Note that @AndrewThompson 's link included all this information – Robin Apr 25 '12 at 05:46

0 Answers0