package javaappimprimir;
@author
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.EOFException;
import java.io.IOException;
public class Main implements Printable, ActionListener
{
static String nombreFichero = null;
static DataInputStream dis = null;
static int siguientePag = PAGE_EXISTS;
static boolean pagImpresa = false;
static Graphics2D g2d = null;
public void actionPerformed(ActionEvent e)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog())
{
try
{
job.print();
}
catch (PrinterException ex)
{
System.out.println(ex.getMessage());
}
}
}
public int print(Graphics g, PageFormat formatoPag, int numPag)
throws PrinterException
{
System.out.println("núm. pag.: " + numPag);
if (!pagImpresa)
{
pagImpresa = true;
return siguientePag;
}
Font font = new Font("Arial", Font.PLAIN, 10);
FontMetrics metrics = g.getFontMetrics(font);
int altoLinea = metrics.getHeight();
double margenIzDe = 0, margenSupInf = 0, anchoPag = 0, altoPag = 0;
if (numPag == 0)
{
margenIzDe = Math.ceil(altoLinea * 3);
margenSupInf = Math.ceil(altoLinea * 4);
anchoPag = Math.ceil(formatoPag.getImageableWidth());
altoPag = Math.ceil(formatoPag.getImageableHeight());
Paper papel = formatoPag.getPaper();
papel.setImageableArea(margenIzDe, margenSupInf,
anchoPag - 2*margenIzDe, altoPag - 3*margenSupInf);
formatoPag.setPaper(papel);
}
g2d = (Graphics2D) g;
g2d.translate(formatoPag.getImageableX(), formatoPag.getImageableY());
String linea = "";
int y = altoLinea;
altoPag = Math.ceil(formatoPag.getImageableHeight());
while (siguientePag == PAGE_EXISTS && y < altoPag+margenIzDe)
{
linea = leerUnRegistro();
if (linea == null)
{
siguientePag = NO_SUCH_PAGE;
break;
}
g.drawString(linea, 0, y);
y += altoLinea;
System.out.println("linea: " + linea + " - y: " + y + ", alto: " + altoPag);
}
pagImpresa = false;
return siguientePag;
}
public String leerUnRegistro()
{
String nombre, direccion;
long telefono;
String cadena = null;
try
{
nombre = dis.readUTF();
direccion = dis.readUTF();
telefono = dis.readLong();
cadena = nombre + ", " + direccion + ", tfno.: " + telefono;
}
catch (EOFException e)
{
System.out.println("Fin del fichero.");
cadena = null;
}
catch (IOException e)
{
System.out.println("Error de lectura.");
System.exit(0);
}
return cadena;
}
public static void main(String args[])
{
File fichero = null;
nombreFichero = "agenda.dat";
fichero = new File(nombreFichero);
if (!fichero.exists())
{
System.out.print("El archivo no existe...");
return;
}
try
{
dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(nombreFichero)));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
JFrame f = new JFrame("Imprimir desde Java (http://www.fjceballos.es)");
f.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
if (dis != null)
{
try { dis.close(); } catch (IOException ex) {}
}
System.exit(0);
}
});
JButton printButton = new JButton("Imprimir");
printButton.addActionListener(new Main());
f.add("Center", printButton);
f.setSize(400, 300);
f.setVisible(true);
}
}