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

[5554 byte] By [Jodia] at [2007-11-26 17:50:13]
# 1
I think I solved it, but need more tests...thanks for anyone who read this post
Jodia at 2007-7-9 5:02:43 > top of Java-index,Java Essentials,New To Java...
# 2

This will print a text file

PrintDoc.java

===========

import java.io.File;

import java.io.FileInputStream;

import javax.print.Doc;

import javax.print.DocFlavor;

import javax.print.DocPrintJob;

import javax.print.PrintService;

import javax.print.PrintServiceLookup;

import javax.print.SimpleDoc;

import javax.print.attribute.HashPrintRequestAttributeSet;

import javax.print.attribute.PrintRequestAttributeSet;

import javax.print.attribute.standard.Finishings;

import javax.print.attribute.standard.MediaSizeName;

import javax.print.attribute.standard.Sides;

import javax.print.event.PrintJobAdapter;

import javax.print.event.PrintJobEvent;

import javax.print.event.PrintJobListener;

import javax.swing.JFileChooser;

public class PrintDoc {

public static void printDoc(File file) {

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

aset.add(MediaSizeName.ISO_A4);

/*

aset.add(new Copies(1));

aset.add(Sides.TWO_SIDED_LONG_EDGE);

*/

aset.add(Sides.ONE_SIDED);

aset.add(Finishings.EDGE_STITCH_LEFT);

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

DocPrintJob job = printService.createPrintJob();

try{

FileInputStream input = new FileInputStream(file);

PrintJobListener pjlistener = new PrintJobAdapter() {

public void printDataTransferCompleted(PrintJobEvent e) {

System.out.println("finish printing one document");

}

};

job.addPrintJobListener(pjlistener);

Doc doc = new SimpleDoc(input, flavor, null);

job.print(doc, aset);

//inStream.close();

input.close();

}

catch(Exception t){t.printStackTrace();}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

JFileChooser chooser = new JFileChooser();

int approve = chooser.showOpenDialog(null);

if (approve == JFileChooser.APPROVE_OPTION) {

printDoc(chooser.getSelectedFile());

}

}

}

tjacobs01a at 2007-7-9 5:02:43 > top of Java-index,Java Essentials,New To Java...