import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileOutputStream;
{
public MultiplePagePdf()
{
{
public Object getValueAt
(int row,
int column
) {
return String.
valueOf(row +
", " + column
);
}
};
{
{
if (printJob.printDialog()) {
try {
printJob.setPrintable(MultiplePagePdf.this);
printJob.print();
ex.printStackTrace();
}
}
}
});
tb.add(printButton);
tb.addSeparator();
{
{
saveAsPdf();
}
});
tb.add(saveButton);
tb.addSeparator();
}
/**
* this method is copied from http://java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html
*/
{
g2.
setColor(Color.
black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
//leave room for page number
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth = (double) table.getColumnModel().getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage = table.getTableHeader().getHeight() * scale;
double tableWidthOnPage = tableWidth * scale;
double oneRowHeight = table.getRowHeight() * scale;
int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage) / oneRowHeight);
double pageHeightForTable = oneRowHeight * numRowsOnAPage;
int totalNumPages =
(int) Math.
ceil(((double) table.
getRowCount()) / numRowsOnAPage
);
if (pageIndex >= totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
//bottom center
String pageNumber =
"Page: " +
(pageIndex +
1) +
" of " + totalNumPages;
int width = g2.getFontMetrics().stringWidth(pageNumber);
g2.drawString(pageNumber, (int) (pageWidth - width) / 2, (int) (pageHeight + fontHeight - fontDesent));
//
// Paint the header at the top
g2.translate(0f, 0f);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.
ceil(tableWidthOnPage
),
(int) Math.
ceil(headerHeightOnPage
));
g2.scale(scale, scale);
table.getTableHeader().paint(g2);
g2.scale(1 / scale, 1 / scale);
g2.translate(0f, headerHeightOnPage);
g2.translate(0f, -pageIndex * pageHeightForTable);
//If this piece of the table is smaller
//than the size available,
//clip to the appropriate bounds.
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = table.getRowCount()
- lastRowPrinted;
g2.setClip(0,
(int) (pageHeightForTable * pageIndex),
(int) Math.
ceil(tableWidthOnPage
),
(int) Math.
ceil(oneRowHeight *
numRowsLeft));
}
//else clip to the entire area available.
else {
g2.setClip(0,
(int) (pageHeightForTable * pageIndex),
(int) Math.
ceil(tableWidthOnPage
),
(int) Math.
ceil(pageHeightForTable
));
}
g2.scale(scale, scale);
table.paint(g2);
}
/**
* This is where the table is saved as pdf
*/
private void saveAsPdf()
{
try {
PdfWriter writer = PdfWriter.
getInstance(document,
new FileOutputStream("/tmp/table.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
printJob.cancel();
float width = ((float) pageFormat.getWidth());
float height = ((float) pageFormat.getHeight());
PdfTemplate tp;
// Assuming that the number of pages would be 3, You can calculate this same way it is calculated
// in print (Graphics g, PageFormat pageFormat, int pageIndex) method
int numberOfPages = 3;
for (int i = 0; i < numberOfPages; i++) {
tp = cb.createTemplate(width, height);
g2 = tp.createGraphicsShapes(width, height);
this.print(g2, pageFormat, i);
g2.dispose();
cb.addTemplate(tp, 0, 0);
document.newPage();
}
System.
err.
println(e.
getMessage());
}
document.close();
}
public static void main
(String[] args
) {
frame.getContentPane().add(new MultiplePagePdf());
frame.pack();
frame.setVisible(true);
}
}