Java JTable 导出为 PDF

发布于 2024-09-07 19:53:24 字数 755 浏览 1 评论 0原文

目前,我正在使用 iText 将 jTable 数据转换为 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();
  }

我遇到的问题是没有表格标题,假设表格单元格中的数据显示由于空间不够而不完整,那么在pdf中数据也没有完全显示。有没有其他API可以将jTable模型数据转换为pdf?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2024-09-14 19:53:24

是的,另一个 API 是 Docmosis。您可以将模型数据放入 DataProvider 实例中,并使用它来填充模板。该模板将控制表格的外观,Docmosis 将填充它以生成 PDF。您可以将其作为图像来完成,但使用模板来设计表格的外观和感觉会更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文