5

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

Liam Sorsby
  • 2,912
  • 3
  • 28
  • 51

1 Answers1

0

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);
    }
  • is it possible to specify printers with this method? – Liam Sorsby Jul 18 '13 at 14:00
  • no, but this was my solution to the same problem. You can write a batch file through java with the command `rundll32 printui.dll,PrintUIEntry /y /q /n "printer name" `, and then have it execute. this will change the default printer on the machine, and the Desktop.print command uses your computer's settings, so it will always print to the printer that is currently set as the default. execute with something like this – user2464620 Jul 18 '13 at 14:11
  • can i ask what is getGoodBatchExecString? – Liam Sorsby Jul 18 '13 at 14:19
  • oh sorry, that doesnt really pertain to the answer. i copy pasted it from a program i had written before. basically my method got a string that was the filePath, but it was always really wonky and didnt work so i wrote another method to fix it. ill fix my example to make it more accurate – user2464620 Jul 18 '13 at 14:38
  • glad to help. good luck. I think you will find this to be a better method – user2464620 Jul 18 '13 at 15:07
  • very good command in the batch file however to be aquard this only works on vista upwards and i am using xp! – Liam Sorsby Jul 18 '13 at 15:58
  • Ah. There might be a command that works in xp, going to have to bust out some of your google fu to find it – user2464620 Jul 18 '13 at 21:26
  • the batch file will take away platform independency, by the way – tbodt Jul 22 '13 at 18:47
  • 1
    tbodt is correct, however you can check to see if the os is windows or unix based, and if its unix then you can write a bash script to do a similar thing, but the process will be different. – user2464620 Jul 23 '13 at 20:47
  • I was looking for an internal program on windows xp anyways – Liam Sorsby Aug 01 '13 at 06:29