Hi everybody,
I need help please, to manage to print something directly in the
application.
I have an application in Java, with frames (JFrame exactly), and a
button "print" is dedicated to the print of the contents of the window.
I use FOP to format the content. Currently, i manage to create a PDF
file as output, but i'd like to set the output to the printer, but i
don't know how to do this, since it's impossible to use
"Driver.RENDER_PRINT" when I set the renderer of the driver (see the
following method "printMyJPanelC", the second...). Here is the code I
use to "print" a window.
/**
* Impression du contenu de la fenetre
*/
public void imprimeToi(){
try {
//System.out.println("FOP MyJPanel to printer\n");
//System.out.println("Preparing...");
//Reglages des dossiers
File baseDir = new File(".");
File tempDir = new File(baseDir, MyJPanel.TEMP_REP);
tempDir.mkdirs();
tempDir.deleteOnExit();
//Reglages des fichiers xslt(depend du type de la fenetre)
et de sortie
String t = getType();
System.out.println("\nType de test a imprimer : "+t+"\n");
File xsltfile;
if(t.equals("gz"))
xsltfile = new File(baseDir, XSLT_GZ);
else if(t.equals("ivp"))
xsltfile = new File(baseDir, XSLT_IVP);
else if(t.equals("kuder"))
xsltfile = new File(baseDir, XSLT_KUD);
else if(t.equals("mbti"))
xsltfile = new File(baseDir, XSLT_MBTI);
else if(t.equals("papi"))
xsltfile = new File(baseDir, XSLT_PAPI);
else if(t.equals("dat"))
xsltfile = new File(baseDir, XSLT_DAT);
else
throw new MyException("Mauvais type de test : \nImpression
impossible!");
File pdffile = new File(tempDir, "Result.pdf");
System.out.println("\nInput: Test object");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...\n");
printMyJPanelC(this, xsltfile, pdffile);
System.out.println("\nSuccess!");
} catch (Exception e) {
//System.err.println(ExceptionUtil.printStackTrace(e));
e.printStackTrace();
System.exit(-1);
}
}
private void printMyJPanelC(MyJPanelCollection c, File xslt, File
pdf) throws IOException, FOPException, TransformerException {
//Construct driver
Driver driver = new Driver();
//Setup logger
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
//Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);
//Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
try {
driver.setOutputStream(out);
//Setup input for XSLT transformation
Source src = c.getSourceForMyJPanelC();
//Resulting SAX events (the generated FO) must be piped
through to FOP
Result res = new SAXResult(driver.getContentHandler());
//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new
StreamSource(xslt));
//Start XSLT transformation and FOP processing
transformer.transform(src, res);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]