Impresión de un txt

vamogni
09 de Febrero del 2004
Hola a todos! Necesitaría imprimir directamente un archivo .txt sin mostrarlo en pantalla. Es posible redireccionar la salida a la impresora para no tener que preocuparme por margenes, cantidad de líneas por página, etc.? Muchas gracias. Si alguien tiene un código que haga esto se lo agradecería mucho.

maraneda
09 de Febrero del 2004
package test;

/**Imprime pagina Text
*/



import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;

import java.net.URL;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.xml.transform.stream.StreamResult;

import org.apache.xalan.serialize.CharInfo;


public class PrintText implements Printable {
static String filetypestr = "html";
private JComponent mComponent;
private double mScaleX;
private double mScaleY;
private URL path;
private JPanel mainPane;
//Private Boolean useServiceFormat = false;
public static void main(String args[])
{
new PrintText().Flavor();
//PrintText sp = new PrintText();
//System.exit(0);
}


public void Flavor(){
/* Use the pre-defined flavor for a Printable from an InputStream */
//DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII;
//DocFlavor flavor = new DocFlavor("text/plain;charset=ascii","java.net.URL");
//DocFlavor flavor = new DocFlavor("text/html; charset=utf-16", "java.io.InputStream");
/* Specify the type of the output stream */
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
// Input the file

FileInputStream textStream = null;
try {
textStream = new FileInputStream("C://documentos.txt");
} catch (FileNotFoundException ffne) {
}
if (textStream == null) {
return;
}


mainPane = new JPanel();
mComponent = mainPane;
Rectangle componentBounds = mainPane.getBounds(null);
SimpleDoc myDoc = new SimpleDoc(textStream, flavor, null);

myDoc.getAttributes();
try {
myDoc.getReaderForText();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
//aset.add(new Copies(2));
aset.add(MediaSizeName.NA_LETTER);
//aset.add(Sides.DUPLEX);

PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor,aset);
if (services.length > 0) {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
DocPrintJob job = services[0].createPrintJob();
try {
try {
pj.setPrintService(services[0]);
} catch (PrinterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pj.pageDialog(aset);
if(pj.printDialog(aset)) {
try {
pj.print(aset);
} catch (PrinterException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
job.print(myDoc, aset);
}
} catch (PrintException pe) {}
}
}




public int print(Graphics graphics,PageFormat pageFormat,int pageIndex)throws PrinterException {
if (pageIndex == 0) {
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(
pageFormat.getImageableX(),
pageFormat.getImageableY());
PageFormat format = pageFormat;
//setPrintable(this);
Rectangle componentBounds = mComponent.getBounds(null);
g2.translate(-componentBounds.x, -componentBounds.y);
g2.scale(mScaleX, mScaleY);
boolean wasBuffered = mComponent.isDoubleBuffered();
mComponent.paint(g2);
mComponent.setDoubleBuffered(wasBuffered);
return Printable.PAGE_EXISTS;
}else {
return Printable.NO_SUCH_PAGE;
}
}
// public int print(Graphics g,PageFormat pf,int pageIndex) {
//if (pageIndex == 0) {
// Graphics2D g2d= (Graphics2D)g;
// g2d.translate(pf.getImageableX(), pf.getImageableY());
// g2d.setColor(Color.BLUE);
// g2d.drawString("example string", 250, 250);

// g2d.fillRect(0, 0, 200, 200);
// return Printable.PAGE_EXISTS;
//} else {
// return Printable.NO_SUCH_PAGE;
//}
}