public class JavaWorldPrintExample3 {
public static void main(String[] args) {
JavaWorldPrintExample3 example3 = new JavaWorldPrintExample3();
System.exit(0);
}
//--- Private instances declarations
private final static int POINTS_PER_INCH = 10;
public JavaWorldPrintExample3() {
//--- Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob();
//--- Create a new book to add pages to
Book book = new Book();
//--- Add the cover page using the default page format for this print
// job
book.append(new IntroPage(250), printJob.defaultPage());
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat();
documentPageFormat.setOrientation(PageFormat.PORTRAIT);
//--- Tell the printJob to use the book as the pageable object
//printJob.setPageable(book);
printJob.setPrintable(new IntroPage(250), documentPageFormat);
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
// if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
//}
}
private class IntroPage implements Printable {
int y;
public IntroPage(int x){
y=x;
}
public int print(Graphics g, PageFormat pageFormat, int page) {
if(page == 0){
// --- Create the Graphics2D object
Graphics2D g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX(), pageFormat
.getImageableY());
//--- Set the default drawing color to black
g2d.setPaint(Color.black);
//--- Print the title
String titleText = ".....";
Font titleFont = new Font("Dialog", Font.PLAIN, 10);
g2d.setFont(titleFont);
//--- Compute the horizontal center of the page
g2d.drawString("John Brown", 37, 20);
g2d.drawString("NIT.40.440.805-6", 50, 30);
g2d.drawString("Date 2007/06/15", 50, 60);
g2d.drawString("Number from 00001 to 10000", 30, 70);
g2d.drawString("Quantity.", 10, 100);
g2d.drawString("Vlr/Unit", 40, 100);
g2d.drawString("-------", 10, 110);
g2d.drawString("--------", 40, 110);
return (PAGE_EXISTS);
}
else
return NO_SUCH_PAGE;
}
}
} |