Printing problem, please help :~~~
Hi, i am new to java =[
I am trying to print a text using awt.print in a applet, but I have a problem
the printer doesn't have pages, but one big paper, it is a fiscal printer...
when I send the text to the printer, the printer not only print the text in the paper, but grabs more paper, the size of an A4 paper
I don't want it, i just need that the printer only prints the necessary and stops
because if not, it will loose the position to print the fiscal note again...
Please, help me with this...
the code:
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.awt.image.*;
import java.text.*;
import java.net.*;
publicclass Example6{
// Private instances declarations
privatefinalstaticint POINTS_PER_INCH = 72;
/**
* Constructor: Example6
*
*/
public Example6 (){
// Create a new PrinterJob object
PrinterJob printJob = PrinterJob.getPrinterJob ();
// Create a new book to add pages to
Book book =new Book ();
// Add the document page using a landscape page format
PageFormat documentPageFormat =new PageFormat ();
documentPageFormat.setOrientation (PageFormat.LANDSCAPE);
//Paper paper = documentPageFormat.getPaper();
Paper paper =new Paper();
// Limit paper width and height
paper.setSize(120, 120);
paper.setImageableArea(10, 10, 80, 80);
// set the paper to this, edited one
documentPageFormat.setPaper(paper);
book.append (new Document (), documentPageFormat);
// Tell the printJob to use the book as the pageable object
printJob.setPageable (book);
// 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();
}
}
}
/**
* Class: Document
*
* This class is the painter for the document content. In this example,
*/
privateclass Documentextends Componentimplements Printable{
/**
* Method: print
*
* @param g a value of type Graphics
* @param pageFormat a value of type PageFormat
* @param page a value of type int
* @return a value of type int
*/
publicint print (Graphics g, PageFormat pageFormat,int page){
// 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 drawing color to black
g2d.setPaint (Color.black);
// Draw a border arround the page using a 12 point border
//g2d.setStroke (new BasicStroke (4));
Rectangle2D.Double border =new Rectangle2D.Double (0,
0,
pageFormat.getImageableWidth (),
pageFormat.getImageableHeight ());
g2d.draw (border);
Font titleFont =new Font ("Courier New", Font.BOLD, 10);
g2d.setFont (titleFont);
g2d.drawString("w=" + pageFormat.getWidth(), 20, 20);
// Validate the page
return (PAGE_EXISTS);
}
}
}// Example6

