Ciao, sto provando ad utilizzare la seguente public class
[CODE]
/**
* This class provides some facilities to menage the print job
*
* @author Mario Fusco
*/
import java.awt.*;
import java.util.*;
import java.awt.print.*;
public class Printer {
PrinterJob pj;
PageFormat pf;
// The font used to print the current item
Font font;
FontMetrics fm;
// The Graphics where the items to print will be painted
Graphics graphics;
// Holds the sequence of items to print
Vector itemsToPrint = new Vector();
/**
* The constructor
*/
public Printer() {
// create a printjob
pj = PrinterJob.getPrinterJob();
// get the default page
pf = pj.defaultPage();
// Print an empty book
// this trick is used to communicate with the printer and obtain the Graphics object
// where the items to print will be painted
Book dummyBook = new Book();
dummyBook.append(new DummyPage(), new PageFormat());
pj.setPageable(dummyBook);
try {
pj.print();
} catch (PrinterException pe) {
pe.printStackTrace();
}
}
/**
* Send to the printer the current graphics
*/
public void print() {
try {
pj.setPageable(getPages());
if (pj.printDialog()) pj.print();
} catch (PrinterException pe) {
pe.printStackTrace();
}
}
/**
* Set the font in which the next text appended to the job will be printed
*
* @param font The new font
*/
public void setFont(Font font) {
this.font = font;
}
/**
* Clear the current graphics and add a new String of text
*
* @param text The text to be printed
*/
public void setText(String text) {
itemsToPrint = new Vector();
itemsToPrint.add(new TextToPrint(text, font));
}
/**
* Add a new String of text to the current job
*
* @param text The text to be printed
*/
public void appendText(String text) {
itemsToPrint.add(new TextToPrint(text, font));
}
/**
* Append a new table to the current job
*
* @param header The header for the column in the table
* @param data The data of the table: each item of the Vector represent a row in the table
* and must be a String[] of length equals to the number of columns.
*/
public void appendTable(String[] header, Vector data) {
itemsToPrint.add(new TableToPrint(font, header, data, null, null));
}
/**
* Append a new table to the current job
*
* @param header The header for the column in the table
* @param data The data of the table: each item of the Vector represent a row in the table
* and must be a String[] of length equals to the number of columns.
* @columnWidth Each item of this array represent the relavite width of the corresponding column
*/
public void appendTable(String[] header, Vector data, int[] columnWidth) {
itemsToPrint.add(new TableToPrint(font, header, data, columnWidth, null));
}
/**
* Append a new table to the current job
*
* @param header The header for the column in the table
* @param data The data of the table: each item of the Vector represent a row in the table
* and must be a String[] of length equals to the number of columns.
* @rightAlligned Each item of this array indicates if the cell of the corresponding
* column must be right alligned
*/
public void appendTable(String[] header, Vector data, boolean[] rightAlligned) {
itemsToPrint.add(new TableToPrint(font, header, data, null, rightAlligned));
}
/**
* Append a new table to the current job
*
* @param header The header for the column in the table
* @param data The data of the table: each item of the Vector represent a row in the table
* and must be a String[] of length equals to the number of columns.
* @columnWidth Each item of this array represent the relavite width of the corresponding column
* @rightAlligned Each item of this array indicates if the cell of the corresponding
* column must be right alligned
*/
public void appendTable(String[] header, Vector data, int[] columnWidth, boolean[] rightAlligned) {
itemsToPrint.add(new TableToPrint(font, header, data, columnWidth, rightAlligned));
}
/**
* Return the book to be printed
*/
private Book getPages() {
Book book = new Book();
Vector currentPage = new Vector();
// Get the bounds of a page
int yLimit = (int)pf.getImageableHeight();
int xLimit = (int)pf.getImageableWidth();
// This point holds the bottom left corner angle position of the row of text to print
int currX = 0;
int currY = 0;
// Put all the the items to print in the page
for (int i = 0; i < itemsToPrint.size(); i++) {
if (itemsToPrint.elementAt(i) instanceof TableToPrint) {
TableToPrint table = (TableToPrint)itemsToPrint.elementAt(i);
currY = printTable(table, book, currentPage, currY, xLimit, yLimit);
} else {
TextToPrint text = (TextToPrint)itemsToPrint.elementAt(i);
currY = printText(text, book, currentPage, currY, xLimit, yLimit);
}
}
if (currentPage.size() > 0)
book.append(new Page(currentPage), pf);
return book;
}
/**
* Put a block of text in the book to be printed
*
* @return the y position where the next block of text or table should begin
*/
private int printText(TextToPrint text, Book book, Vector currentPage, int currY, int xLimit, int yLimit) {
// set the font used to print this text
graphics.setFont(text.font);
// Get the FontMetrics for the current Font
fm = graphics.getFontMetrics();
int rowHeight = fm.getHeight();
// If a new page is beginning shift the cursor in the bottom position of the first row
if (currY == 0) currY = rowHeight;
// Tokenize the test to be printed in row
StringTokenizer st = new StringTokenizer(text.text, "\n", true);
// Remember is the last row printed is empty or not
boolean emptyRow = true;
while (st.hasMoreTokens()) {
String currentRow = st.nextToken();
// If the row is empty move down the point of a distance equal to the row height
if (currentRow.equals("\n")) {
if (emptyRow) {
currY += rowHeight;
// If the page is finished save it in the book and begin a new one
if (currY >= yLimit) {
book.append(new Page((Vector)currentPage.clone()), pf);
currentPage.removeAllElements();
currY = rowHeight;
}
} else emptyRow = true;
} else {
// the row isn't empty
emptyRow = false;
// split the current block of text in more row to fit it in the page
Enumeration rows = splitString(currentRow, xLimit).elements();
// Print the text a row at time
while (rows.hasMoreElements()) {
currentPage.add(new StringToPrint((String)rows.nextElement(), text.font, (int)pf.getImageableX(), (int)pf.getImageableY() + currY));
currY += rowHeight;
// If the page is finished save it in the book and begin a new one
if (currY >= yLimit) {
book.append(new Page((Vector)currentPage.clone()), pf);
currentPage.removeAllElements();
currY = rowHeight;
}
}
}
}
return currY;
}
/**
* Put a block of text in the book to be printed
*/
private int printTable(TableToPrint table, Book book, Vector currentPage, int currY, int xLimit, int yLimit) {
// print the header of the table in bold
Font boldFont = new Font(table.font.getName(), Font.BOLD, table.font.getSize());
graphics.setFont(boldFont);
// Get the FontMetrics for the current Font
fm = graphics.getFontMetrics();
int rowHeight = fm.getHeight();
// If a new page is beginning shift the cursor in the bottom position of the first row
if (currY == 0) currY = rowHeight;
// Get the width of each column
int[] columnWidth = table.columnWidth;
for (int i = 0; i < table.columnNr; i++)
columnWidth[i] = columnWidth[i] * xLimit / 1000;
// Get the beginning position of each column
int[] columnPos = new int[table.columnNr];
for (int i = 0; i < table.columnNr; i++) {
columnPos[i] = 1;
for (int j = 0; j < i; j++)
columnPos[i] += columnWidth[j];
}
// take out two pixel from each column to get the space for the lines
for (int i = 0; i < table.columnNr; i++)
columnWidth[i] -= 2;
// Each Vector in this array holds the sequence of row to be printed in a cell
Vector[] cell = new Vector[table.columnNr];
// hold the max cell height of the cells in this row
int maxCellHeight = 0;
for (int i = 0; i < table.columnNr; i++) {
// split the text to fit it in the cell
cell[i] = splitString(table.header[i], columnWidth[i]);
maxCellHeight = maxCellHeight > cell[i].size() ? maxCellHeight : cell[i].size();
}
// add the first horizontal line of the table
currentPage.add(new LineToPrint((int)pf.getImageableX(), (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3));
// Write the header
for (int i = 0; i < maxCellHeight; i++) {
for (int j = 0; j < table.columnNr; j++) {
if (cell[j].size() > i)
currentPage.add(new StringToPrint((String)cell[j].elementAt(i), boldFont, (int)pf.getImageableX() + columnPos[j], (int)pf.getImageableY() + currY));
// add the left margin of the cell
currentPage.add(new LineToPrint((int)pf.getImageableX() + columnPos[j] - 1, (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + columnPos[j] - 1, (int)pf.getImageableY() + currY + 3));
}