打印预览功能
发票打印
@RequestMapping(value = “getXps1”)
public void xpsGet1(HttpServletResponse response){
File file = new File(“C:/Users/Administrator/Desktop/1.xps”); // 获取选择的文件
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService service1 = printService[0];
PrintService service = ServiceUI.printDialog(null, 200, 200, printService,service1, flavor, pras);
DocPrintJob job = service.createPrintJob();
try {
FileInputStream fis = new FileInputStream(file);
DocAttributeSet das = new HashDocAttributeSet();
pras.add(Chromaticity.COLOR);
pras.add(OrientationRequested.LANDSCAPE);
pras.add(MediaSizeName.NA_LETTER);
// European A4 paper
pras.add(MediaSizeName.ISO_A4);
//请求装订
pras.add(Finishings.STAPLE);
//整理多个副本
pras.add(SheetCollate.COLLATED);
//请求双面
pras.add(Sides.DUPLEX);
// 2页到一个工作表
pras.add(new NumberUp(2));
//多少个副本
pras.add(new Copies(2));
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "getXps")
public void xpsGet(HttpServletResponse response){
try {
File file = new File("C:/Users/Administrator/Desktop/1.xps");
byte[] bs = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bs);
fis.close();
File xpsFile = File.createTempFile("tempx", ".xps");
FileOutputStream outputStream = new FileOutputStream(xpsFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write(bs);
bufferedOutputStream.flush();
bufferedOutputStream.close();
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
desktop.open(xpsFile);
xpsFile.deleteOnExit();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
参考:00https000://www.000cnblogs.000com/ClareZjy/p/6929040.ht000ml+
+http000s://blog.000csdn.net/XIA_1997/article/details/81837891