0

I have a jframe with jtable, Button, and Jlabel. I have a problem When I click the button it appears that saved to pdf just fill in the table, but no column name. And what if I also want to add jlabel into pdf file??

It's the script :

Private void print(){ 

Document document = new Document(PageSize.A4.rotate());

try {
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/jTable.pdf"));

  document.open();
  PdfContentByte cb = writer.getDirectContent();

  cb.saveState();
  Graphics2D g2 = cb.createGraphicsShapes(500, 500);

  Shape oldClip = g2.getClip();
  g2.clipRect(0, 0, 500, 500);

  table_pdf.print(g2);
  g2.setClip(oldClip);

  g2.dispose();
  cb.restoreState();
} catch (Exception e) {
  System.err.println(e.getMessage());
}
document.close();}

it's screenshoot enter image description here

and it's file pdf enter image description here

dtnder
  • 381
  • 2
  • 10
  • 25
  • `Private void print () {` That would not compile and therefore cannot be the code used. But for better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 16 '12 at 03:49
  • 2
    If you are drawing a table you really should use PdfPTable. http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html – scrappedcola Nov 16 '12 at 03:51
  • @scrappedcola Good call. With the difficulties of [getting a graphic from a table](http://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image) correctly, it is nice to know there is a specific class in the API that handles the details of creating a PDF conversion. – Andrew Thompson Nov 16 '12 at 03:53

1 Answers1

3

Table's columns headers are shown as column header of the JScrollPane where the table is added. Try to use the method of JScrollPane

public JViewport getColumnHeader()

and print it above the table

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • @user1815938 or get JTableHeader from JTable (required BorderLayout for Container) put to the NORTH area – mKorbel Nov 16 '12 at 06:52