dtnder
dtnder

Reputation: 389

create pdf file from JTable in Java

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

Upvotes: 0

Views: 5461

Answers (1)

StanislavL
StanislavL

Reputation: 57421

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

Upvotes: 3

Related Questions