gpt4 book ai didi

java - 如何使用 jtable 标题将 jtable 保存在 pdf 上

转载 作者:行者123 更新时间:2023-12-02 02:59:40 24 4
gpt4 key购买 nike

我可以在 pdf 上保存 jtable,但表没有标题,请帮助我:

  Document document = new Document(PageSize.A4, 50.0f, 50.0f, 10.0f, 50.0f);
PdfWriter writer = PdfWriter.getInstance((Document)document, (OutputStream)new FileOutputStream(this.file + ".pdf"));
document.open();
PdfContentByte cb1 = writer.getDirectContent();
// modules table
PdfTemplate template = cb1.createTemplate(2480.0f, 3500.0f);
Graphics2D g1 = template.createGraphics((float)this.jTable_Modules2.getWidth(), (float)this.jTable_Modules2.getHeight());
// this.jTable_Modules2.getViewport().setBackground(Color.WHITE);
this.jTable_Modules2.print(g1);
g1.dispose();
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance((PdfTemplate)template);
image.scalePercent(60.0f);
image.setAbsolutePosition(80.0f, 458.0f);

document.add((Element)image);

document.close();
JOptionPane.showMessageDialog(null, "Transcript successfully generated");

最佳答案

this answer所示您可以创建一个新的 JTable 并将其作为组件传递给 getImageFromComponent(...) 及其 header 应用程序:

enter image description here

java.awt.Image img = getImageFromComponent(table.getTableHeader());
addImageToDocument(img);
img = getImageFromComponent(table);
addImageToDocument(img);

如果您不将 JTable 添加到 JScrollPane,如果您这样做,则发送 JScrollPane,例如:

enter image description here

java.awt.Image img = getImageFromComponent(scrollPane);
addImageToDocument(img);

如何添加表格取决于您...我更喜欢 scrollPane 版本:)

<小时/>

另一种方法是迭代 JTable 的数据并使用 PdfPTable

基于this answer你可以得到这样的东西:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class JTableToPdf {

private JFrame frame;
private JTable table;
private JButton button;
private Document document;
private PdfWriter writer;
private JScrollPane scrollPane;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTableToPdf().createAndShowGui();
}
});
}

public void openPdf() throws FileNotFoundException, DocumentException {
document = new Document(PageSize.A4, 30, 30, 30, 30);
writer = PdfWriter.getInstance(document, new FileOutputStream("myFile2.pdf"));
document.open();
}

public void closePdf() {
document.close();
}

public void addData(PdfPTable pdfTable) throws DocumentException {
pdfTable.setHeaderRows(1);

PdfPCell cell = new PdfPCell(new Paragraph(table.getColumnName(0)));
cell.setBackgroundColor(new GrayColor(0.7f));
pdfTable.addCell(cell);

cell = new PdfPCell(new Paragraph(table.getColumnName(1)));
cell.setBackgroundColor(new GrayColor(0.7f));
pdfTable.addCell(cell);

cell = new PdfPCell(new Paragraph(table.getColumnName(2)));
cell.setBackgroundColor(new GrayColor(0.7f));
pdfTable.addCell(cell);

for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
pdfTable.addCell(table.getModel().getValueAt(i, j).toString());
}
}

document.add(pdfTable);
}

public void createAndShowGui() {
frame = new JFrame("PDF creator");

int rows = 100;
int cols = 3;

String data [][] = new String[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = i + "-" + j;
}
}

table = new JTable(data, new String []{"A", "B", "C"});
table.setBorder(BorderFactory.createLineBorder(Color.RED));
button = new JButton("Create PDF");

scrollPane = new JScrollPane(table);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
PdfPTable pdfTable = new PdfPTable(table.getColumnCount());
openPdf();
addData(pdfTable);
closePdf();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (DocumentException e1) {
e1.printStackTrace();
}
}
});

// frame.add(table.getTableHeader(), BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

enter image description here enter image description here

如果您不希望标题在每个页面上重复,只需删除 addData(...) 方法上 for 循环之前的行即可。

<小时/>

第三种方法可以使用 JTableprintable 方法,但我这里没有打印机,稍后我将处理这个示例,因为我现在没有太多时间,我不知道如何将其与 iText 集成,所以我必须搜索一下......

<小时/>

唷!我花了一段时间才创建这个结果,但我认为它效果很好(尽管您需要在创建 PDF 后恢复 JTable)。

查看代码注释并告诉我这是否有帮助。我结合使用 PDFPTable 和图像方法来实现这种新方法:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class JTable2Pdf {

private JFrame frame;
private JTable table;
private JButton button;
private JScrollPane scrollPane;
private DefaultTableModel model;
private DefaultTableModel restoreModel;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTable2Pdf().createAndShowGui();
}
});
}

public void restoreTableView() {
table.setModel(restoreModel);
frame.revalidate();
frame.repaint();
}

public void createAndShowGui() {
frame = new JFrame("PDF creator");
int rows = 130;
int cols = 3;

String data[][] = new String[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = i + "-" + j;
}
}

model = new DefaultTableModel(data, new String[] { "A", "B", "C" });

restoreModel = new DefaultTableModel(data, new String[] { "A", "B", "C" });

table = new JTable(model);

//Just some borders for the table and its header
table.setBorder(BorderFactory.createLineBorder(Color.RED));
table.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.RED));

scrollPane = new JScrollPane(table);

button = new JButton("Create PDF");

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
//We create an instance of pdfCreator and pass our JTable as instance
PdfCreator pdfCreator = new PdfCreator(table);

//We open the pdf and say we want to repeat the header for each page
pdfCreator.openPdf(true);
//Then we add the header (if we don't add it first, then the 1st row of the table will be the one repeating over the pages
pdfCreator.addImageToTable(pdfCreator.getImageFromComponent(table.getTableHeader()));
//Then we add the rows to the PDF
pdfCreator.getRowsImage();
//Finally we close the PDF
pdfCreator.closePdf();
//And last we restore the table
restoreTableView();

} catch (DocumentException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

frame.add(scrollPane, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class PdfCreator {

private JTable table;
private Document document;
private PdfWriter writer;
private boolean isNewPage;
private PdfPTable pdfTable;

public PdfCreator(JTable table) {
this.table = table;
}

//This method opens the PDF, repeatHeader if true, adds the header to each page, otherwise it only adds it to the 1st one
public void openPdf(boolean repeatHeader) throws FileNotFoundException, DocumentException {
document = new Document(PageSize.A4, 30, 30, 30, 30);
writer = PdfWriter.getInstance(document, new FileOutputStream("myFile.pdf"));
pdfTable = new PdfPTable(1);
if (repeatHeader) {
pdfTable.setHeaderRows(1);
}
pdfTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
pdfTable.getDefaultCell().setPadding(0);
document.open();
}

//This method closes the document
public void closePdf() {
document.close();
}

//This method gets the image for each row recursively
//As this method gets the graphics for the table from 0, 0 to the width and length determined, we need to remove the 1st row of the table
//each time we go through it and that's why we need to restore the table model later in the code
public void getRowsImage() throws DocumentException, IOException {
if (table.getRowCount() > 0) {
int width = table.getCellRect(0, 0, true).width + table.getCellRect(0, 1, true).width + table.getCellRect(0, 2, true).width;
int height = table.getCellRect(0, 0, true).height;

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
table.paint(img.getGraphics());
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
System.out.println(table.getRowCount() + " - " + isNewPage);

dtm.removeRow(0);
addImageToTable(img);
getRowsImage();
} else {
document.add(pdfTable);
}
}

//This gets the image for the table header and / or any other components you need to add to the PDF
public java.awt.Image getImageFromComponent(JComponent component) throws DocumentException {
int width = component.getWidth();
int height = component.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
component.paint(image.getGraphics());
return image;
}

//This method add the image to a PDFPTable which will be added later to the document
public void addImageToTable(java.awt.Image img) throws IOException, DocumentException {
Image image = Image.getInstance(writer, img, 1);
image.scalePercent(100);
pdfTable.addCell(image);
System.out.println("printed!");
}
}

这些是输出:

第一页有边框和重复标题 enter image description here带边框和重复页眉的第二页 enter image description here

第二页有边框且不重复页眉 enter image description here

第一页无边框且不重复页眉 enter image description here第二页无边框且不重复页眉 enter image description here

<小时/>

我能够通过移动而不是删除行来更改上述代码,这会产生相同的结果,并且仍然可以正常工作,但您不再需要 restoreTableView() 方法...只需更改 getRowsImage() 方法,如下所示:

public void getRowsImage() throws DocumentException, IOException {
if (movedRows < table.getRowCount()) {
int width = table.getCellRect(0, 0, true).width + table.getCellRect(0, 1, true).width + table.getCellRect(0, 2, true).width;
int height = table.getCellRect(0, 0, true).height;

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
table.paint(img.getGraphics());
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
System.out.println(table.getRowCount() + " - " + isNewPage);

dtm.moveRow(0, 0, table.getRowCount() - 1);
movedRows++;
addImageToTable(img);
getRowsImage();
} else {
document.add(pdfTable);
}
}

并将变量 movedRows = 0 声明为 PdfCreator 类的类成员:)

关于java - 如何使用 jtable 标题将 jtable 保存在 pdf 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42534763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com