java 如何在特定坐标处以pdf格式导出完整的JTable组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12785264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:12:54  来源:igfitidea点击:

How to export a complete JTable component in pdf at a specific coordinate

javaswingpdfjtableitext

提问by ajay partoti

In my project I have to print JTable components in a pdf with all the customizations(like borders,colors etc). For this requirement I searched a little bit and found one code to print JTable in pdfs. But after printing I found that table's border for 1st row and 1st columns are missing plus I didn't find a way to print at some specific coordinate. Please look at the specific code below:-

在我的项目中,我必须使用所有自定义(如边框、颜色等)以 pdf 格式打印 JTable 组件。对于这个要求,我搜索了一点,找到了一个代码来在 pdf 中打印 JTable。但是打印后我发现第一行和第一列的表格边框丢失了,而且我没有找到在某个特定坐标处打印的方法。请看下面的具体代码:-

package com.jpmorgan.dqreport;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.FileOutputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class JTable2Pdf extends JFrame {
  private JTable table;

  public JTable2Pdf() {
    getContentPane().setLayout(new BorderLayout());
    createTable();
  }
  private void createTable() {
      Object[][] data = {
                {"Kathy", "Smith",
                 "SnowboardingXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
                };
      String[] columnNames = {"First Name",
              "Last Name",
              "Sport",
              "# of Years",
              "Vegetarian"};

    table = new JTable(data, columnNames);

    JPanel tPanel = new JPanel(new BorderLayout());
    tPanel.add(table.getTableHeader(), BorderLayout.NORTH);
    tPanel.add(table, BorderLayout.CENTER);

    getContentPane().add(tPanel, BorderLayout.CENTER);
  }
  private void print() {
    Document document = new Document(PageSize.A4.rotate());
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\jTable.pdf"));

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

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

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

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

      g2.dispose();
      cb.restoreState();
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
  public static void main(String[] args) {
    JTable2Pdf frame = new JTable2Pdf();
    frame.pack();
    frame.setVisible(true);
    frame.print();
  }
}

Please suggest someway to print the entire JTable at a specific coordinate.

请建议以某种方式在特定坐标处打印整个 JTable。

Thanks

谢谢

采纳答案by ajay partoti

Hi I resolved the code by adding like below:-

嗨,我通过添加如下解决了代码:-

cb.saveState();

PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
/*g2.setColor(Color.BLACK);
g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/
table.print(g2);
System.out.println("x="+x + "," + "y=" + y);
cb.addTemplate(pdfTemplate, x, y);
g2.dispose();
cb.restoreState();

回答by Andrew Thompson

A JTableis notoriously difficult to get rendered correctly for images (or I guess, printing). See this threadon which some of the gurus weigh in with tips.

一个JTable是出了名的难图像得到正确呈现(或者我猜,打印)。请参阅此线程,其中一些大师通过提示权衡。

Looking closely at the screenshots of that thread suggests that the default Metal table has no border on the far left.

仔细查看该线程的屏幕截图表明,默认的 Metal 表在最左侧没有边框。

The table in Nimbus PLAF does, though.

不过,Nimbus PLAF 中的表格确实如此。