Question

So im writing a java program for my dad to print out receipts and stuff. My original intention was to print out to his Receipt Printer some info regarding each transaction he made. However, the printer has some trouble printing what i send without clipping it to an extreme point.

My next idea, which worked out quite well, was to save the "receipt" into an XPS file and then print the XPS, which would not clip it and would make everything nice. Now, i can print into an XPS file using Microsoft's XPS Document Writer PrintService. The problem is, when i do it, it always pop's up a box asking for the file name and location to save it into.

Is there anyway to set it so as to not show that pop-up at all?

Current code:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
try {
    job.print();
} catch (PrinterException ex) {
    // The job did not successfully complete 
}

-

@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
     String temp;

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    int lineSize=20;

    Font testFont=new Font("Lucida Console", 0, 20);
    g.setFont(testFont);

    g.drawString("      Fatura/Recibo nº"+nmrRec+"      ", 5, 20);
    return PAGE_EXISTS;
}
Was it helpful?

Solution

You should be able to do it by setting the Destination attribute:

static void print(Printable printable, PrintService service)
throws PrintException,
       IOException {

    Path outputFile = Files.createTempFile(
        Paths.get(System.getProperty("user.home")), null, ".xps");

    Doc doc = new SimpleDoc(printable,
        DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

    PrintRequestAttributeSet attributes =
        new HashPrintRequestAttributeSet();
    attributes.add(new Destination(outputFile.toUri()));

    DocPrintJob job = service.createPrintJob();
    job.print(doc, attributes);
}

OTHER TIPS

So i followed VGR's advice and i got it working. This was my final code, in case anyone runs into the same problem:

Date data = new Date();                                         //Data
DateFormat dataform = new SimpleDateFormat("dd-MM-yyyy");       //Data

PrintService service=getPrinterService("Microsoft XPS Document Writer");
if(service!=null){
    try{
        File outputFile = new File(dataform.format(data)+"-Recibo"+nmrRec+".xps");

        Doc doc = new SimpleDoc(new myReceipt(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(new Destination(outputFile.toURI()));

        DocPrintJob job = service.createPrintJob();
        job.print(doc, attributes);
    } catch(Exception e){
        System.out.println("kaboom"+e);
    }
}
else{
    System.out.println("XPS Printer not found");
}

And there's my receipt class:

class myReceipt implements Printable{

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
        String temp;

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        int lineSize=20;

        Font testFont=new Font("Lucida Console", Font.BOLD, 20);
        // font name, style (0 for Plain), font size
        g.setFont(testFont);
        int line=20;

        g.drawString("        Fatura/Recibo nº"+nmrRec+"      ", 5, line);
        return PAGE_EXISTS;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top