Hi all,
I trying to print a JTable to a PDF using iText. So far, the resulting PDF
only shows the table outlines, but not the table's contents. I don't know
which part of the code went wrong. Could someone please help me out. The
following is a sample application demonstrating the problems.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
public class PrintJTable extends JFrame {
private JTable table;
/**
* Constructor for PrintJTable.
*/
public PrintJTable() {
getContentPane().setLayout(new BorderLayout());
setTitle("JTable test");
createToolbar();
createTable();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
/**
* Create a table with some dummy data
*/
private void createTable() {
Object[][] data ={
{"Mary", "Campione", "Snowboarding", new Integer(5),
new
Boolean(false)},
{"Alison", "Huml", "Rowing", new Integer(3), new
Boolean(true)},
{"Kathy", "Walrath", "Chasing toddlers", new
Integer(2), new
Boolean(false)},
{"Mark", "Andrews", "Speed reading", new Integer(20),
new
Boolean(true)},
{"Angela", "Lih", "Teaching high school", new
Integer(4), new
Boolean(false)}
};
String[] columnNames =
{"First Name", "Last Name", "Sport", "# of Years",
"Vegetarian"};
table = new JTable(data, columnNames);
// Use a panel to contains the table and add it the frame
JPanel tPanel = new JPanel(new BorderLayout());
tPanel.add(table.getTableHeader(), BorderLayout.NORTH);
tPanel.add(table, BorderLayout.CENTER);
getContentPane().add(tPanel, BorderLayout.CENTER);
}
/**
* Toolbar for print and exit
*/
private void createToolbar() {
JToolBar tb = new JToolBar();
JButton printBtn = new JButton("Print");
printBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print();
}
});
JButton exitBtn = new JButton("Exit");
exitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit();
}
});
tb.add(printBtn);
tb.add(exitBtn);
getContentPane().add(tb, BorderLayout.NORTH);
}
/**
* Print the table into a PDF file
*/
private void print() {
Document document = new Document(PageSize.A4.rotate());
try {
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
// Create the graphics
Graphics2D g2 = cb.createGraphics(500, 500);
cb.saveState();
// Print the table to the graphics
Shape oldClip = g2.getClip();
g2.clipRect(10, 10, 500, 500);
table.print(g2);
g2.setClip(oldClip);
cb.restoreState();
g2.dispose();
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
/**
* Exit app
*/
private void exit() {
System.exit(0);
}
/**
* Main program
*/
public static void main(String[] args) {
PrintJTable frame = new PrintJTable();
frame.pack();
frame.setVisible(true);
}
}
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
-------------------------------------------------------
This sf.net email is sponsored by: Dice - The leading online job board
for high-tech professionals. Search and apply for tech jobs today!
http://seeker.dice.com/seeker.epl?rel_code=31
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions