Question

I have two peices of code for printing using java as seen below:

The First Code

for(int i = 0; i < files.length; i++) {
    String file = "C:\\images\\colour\\"+files[i].getName();
    String filename = file;
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras);

    if (service != null) {
        DocPrintJob job = service.createPrintJob();

        PrintJobListener listener = new PrintJobAdapter() {
            public void printDataTransferCompleted(PrintJobEvent e) {
                System.exit(0);
            }
        };

        job.addPrintJobListener(listener);
        FileInputStream fis = new FileInputStream(filename);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);

    }
}

This code has a printDialog and prints as intended on the printer

the second code:

try {
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));

    PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

    if (pss.length == 0) 
        throw new RuntimeException("No printer services available.");

    PrintService ps = pss[3];
    System.out.println("Printing to " + ps);
    DocPrintJob job = ps.createPrintJob();

    FileInputStream fin = new FileInputStream(files[i]);
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
    job.print(doc, pras);

    fin.close();
}
catch (IOException ie) {
    ie.printStackTrace();
}
catch (PrintException pe) {
    pe.printStackTrace();
}

}

prints without a print dialog which is what i am after but this will print a blank page to the printer.

Now may aim is to use only one of these codes but i have supplied what i have tried. I need to have code 1 working but with no printerDialog.

if i do remove the printerDialog from code 1 then basically it does the same the same as code 2 (prints blank on this printer).

I beleive the issue is with the parameters passed in code one from PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras); not been passed anymore

Is there anyway of passing the parameters from PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, DocFlavor.INPUT_STREAM.GIF, pras); into the printer without using a dialog or is there a way of skipping the dialog as though the user has clicked yes?

Apologise firstly for the VERY long post. Hope anyone can help, or give me some advise. Thank you in advance

Was it helpful?

Solution

If your goal is just to print a file, there are much easier methods of doing so, such as using java.awt.Desktop.print

heres the code to execute the batch file i mentioned below for better format

Process p;
    String execBatchPath = "your file path";

    try {
        p = Runtime.getRuntime().exec("cmd /c start " + execBatchPath);
        p.waitFor();
    } catch (IOException e) {
        FileIO.writeLog("IO exception while trying to run the batch");
        ErrorUtils.manageCatch(e);
    } catch (InterruptedException e) {
        FileIO.writeLog("Batch process was interrupted");
        ErrorUtils.manageCatch(e);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top