Estou tentando imprimir uma imagem, depois vou ajutar margens e etc, mas, a principio meu problema é apenas a impressão.
Meu problema no momento é que quando imprimo uma pagina, ela se repete na proxima impressão, ou seja, a segunda vez que chamo o comando de imprimir ele imprimi a uma pagina identica a anterior e só depois, na segunda tentativa, ela imprime a pagina correta. Alguem tem alguma sugestão?
Muito obrigado.
[code]
import java.awt.;
import java.awt.print.;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
class PrintObject implements Printable
{
public int print (Graphics g, PageFormat f, int pageIndex)
{
Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graphics on
// the print pages :
if(pageIndex == 0){
Paper p = new Paper();
p.setSize(5.48, 8.6);
f.setPaper(p);
try {
g2.drawImage(ImageIO.read(new File("C:\\Desenvolvimento\\testeJ.jpg")), null, pageIndex, pageIndex);
} catch (IOException ex) {
Logger.getLogger(PrintObject.class.getName()).log(Level.SEVERE, null, ex);
}
return PAGE_EXISTS;
}
else
return NO_SUCH_PAGE;
}
}
public class Teste
{
public static void main (String[] args)
{
// Create an object that will hold all print parameters, such as
// page size, printer resolution. In addition, it manages the print
// process (job).
PrinterJob job = PrinterJob.getPrinterJob();
// It is first called to tell it what object will print each page.
job.setPrintable(new PrintObject());
// Then it is called to display the standard print options dialog.
if (job.printDialog())
{
// If the user has pressed OK (printDialog returns true), then go
// ahead with the printing. This is started by the simple call to
// the job print() method. When it runs, it calls the page print
// object for page index 0. Then page index 1, 2, and so on
// until NO_SUCH_PAGE is returned.
try { job.print(); }
catch (PrinterException e) { System.out.println(e); }
}
}
}[/code]