File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with printing multiple files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with printing multiple files" Watch "Problem with printing multiple files" New topic
Author

Problem with printing multiple files

Sashanak Mohanty
Greenhorn

Joined: Mar 11, 2004
Posts: 2
Hello ,
I am working on a project where the printer will be given multiple files to print in a sequence .But the problem is the printer is taking the first file to print. By the time the second file is sent to print while the first print operation is not complete. I want to send the next print sequentially once the first print job completes or canceled i.e the printJob is idle to accept the new print call.
The exception throws is ...
javax.print.PrintException: already printing
The following code is for referal.
Please Help me ..I am in deadline.
Thank you.
===============
printmulti.java
===============
import javax.print.*;
import javax.print.event.*;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import javax.print.attribute.EnumSyntax.*;
import javax.print.attribute.standard.*;
import javax.print.DocPrintJob;
import javax.print.attribute.*;
import javax.print.DocFlavor;
import java.lang.*;
import java.io.*;
public class printmulti
{
static DocFlavor flavor;
static PrintService pr_service;
static DocPrintJob job;
public static void main(String []arg)
{
printmulti prn=new printmulti();
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
pr_service= PrintServiceLookup.lookupDefaultPrintService();
job = pr_service.createPrintJob();
for(int i=0;i<arg.length;i++)
{
prn.print(arg[i]);
}
}
void print(String filename)
{
try
{
// Open the file
InputStream is = new BufferedInputStream(new FileInputStream(filename));
// Create the print job
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
Doc doc = new SimpleDoc(is,flavor ,null);
// Monitor print job events
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, aset);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the input stream
is.close();
}
catch (PrintException e)
{
System.out.println("Print Exception =========\n : "+e);
}
catch (IOException e)
{
System.out.println("IO Exception =========\n : "+e);
}
}
}
class PrintJobWatcher
{
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone()
{
try
{
while (!done)
{
System.out.println("Waiting");
wait();
}
} catch (InterruptedException e)
{
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with printing multiple files