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;
}
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);
}