views:

77

answers:

1

Currently, I am using iText to convert my jTable data to pdf.

private void print() {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));

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

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

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

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

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

The problem i got was that there is no table header, and let say if the data display in table cell is not complete due to the space is not enough, in pdf the data isn't showing completely as well. is there any other API can convert the jTable model data to pdf?

A: 

Yes, another API would be Docmosis. You would put your model data into a DataProvider instance and use that to populate a template. The template would control how your table looks and Docmosis would populate it to produce the PDF. You could do it as an image, but using the template to design the table look and feel is better.

jowierun
Is there any example? I tried downlaod the Docmosis but not sure how to put my model data into DataProvider.
I look through those example, do I need to create a template first?
Yes, Docmosis merges data and a template always. Look at example1 in the download - it has source code. You would create a table in your template with placeholders and in Java code read the data out of your model into a DataProvider using the addString() methods.
jowierun