user16514999
user16514999

Reputation:

how to get print status in java?

I am using the below java code to print the pdf file with print status, but the print status(printDataTransferCompleted,printJobCompleted,printJobFailed,printJobCanceled,etc,.) is not returning.

Below is the Java code,

    PDDocument document = PDDocument.load(new File("D:\\Rose\\Work\\17.pdf"));

        PrintService myPrintService = findPrintService("Champ RP Series");

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PDFPageable(document));
        job.setPrintService(myPrintService);
        job.print();
        if(job.isCancelled() == true) {
            System.out.println("cancelled"+ job.isCancelled());
            
        }else {
            System.out.println("progress"+ job.isCancelled());
        }
       
        DocPrintJob docPrint = myPrintService.createPrintJob();
        System.out.println(docPrint.getPrintService());
        docPrint.addPrintJobListener(new PrintJobListenerSample());

How do I return the print status(printDataTransferCompleted,printJobCompleted,printJobFailed,printJobCanceled,etc,.)?

Thank you

Upvotes: 0

Views: 597

Answers (1)

queeg
queeg

Reputation: 9394

Very likely, after your code has run as expected the correct result has been delivered. On the contrary your code would not execute correctly but abort when - for whatever reason - an exception is thrown. So to report this status you could use a try-catch block:

try {
    // place here your logic from your question

    // check if the job was cancelled by user
    if (job.isCancelled()) {
        log.info("job cancelled by user");
        return printJobCancelled;
    }

    return printDataTransferCompleted; // mark success
} catch (Exception e} {        // bad pattern to catch Exception. Get more concise if you can
    log.error("Could not print", e); // at least log what happened. If necessary, throw some exception
    return printJobFailed;    // mark failure
}

Upvotes: 1

Related Questions