如何在打印机作业java完成后停止打印

时间:2016-07-29 04:19:22

标签: java printing

我的打印机工作有问题.. 我设置了我的脚本并设置了纸张尺寸,但打印机在完成打印后总是滚动。 我使用的是爱普生LX-300 + II 在我的爱普生LX-300,LX-310和LX-800中,打印正常,打印机在完成打印时自动停止,但在我的爱普生LX-300 + II中,打印机在完成打印后总是滚动。 我不知道为什么,我使用相同的脚本进行打印.. 这是我设置自定义纸张尺寸的脚本:

public void setPrinter(String textParam, String spesialWords, int gotoX2) {

    try {
        String destination = "";
        boolean printToDoc = true;
        PrinterJob pj = PrinterJob.getPrinterJob();
        //boolean printDialog = pj.printDialog();
        System.out.println("--------------------");

        //PageFormat pf = pj.getPageFormat(null);
        PageFormat pf = pj.defaultPage();
        Paper paper = pf.getPaper();

        double cm = 72.0 / 2.54;
        if (!destination.equalsIgnoreCase("document.pdf")) {
            if (destination.equalsIgnoreCase("printer")) {
                paper.setSize(26 * cm, 7 * cm);
            }
        }
                paper.setSize(23 * cm, 7 * cm);
        double getHeight = 7 * cm;
        double getWidth = 23 * cm;
        double margin = 0.0; // half inch

        paper.setImageableArea(margin, margin, paper.getWidth(), paper.getHeight());
        pf.setPaper(paper);
        System.out.println("==========================================");
        System.out.println("paper height : "+getHeight);
        System.out.println("paper width : "+getWidth);

        PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
        if (destination.equalsIgnoreCase("document.pdf")) {
            PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
            DocPrintJob printerJob = defaultPrintService.createPrintJob();
            File pdfFile = new File(destination);
            SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null);
            try {
                printerJob.print(simpleDoc, null);
            } catch (PrintException ex) {
                Logger.getLogger(Printing2.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else if (destination.equalsIgnoreCase("document.ps")) {
            try {
                if (textParam.toLowerCase().contains("post")) {
                    destination = "post_"+destination;
                } else if (textParam.toLowerCase().contains("non")) {
                    destination = "non_"+destination;
                } else if (textParam.toLowerCase().contains("token")) {
                    destination = "pre_"+destination;
                }

                attr_set.add(new Destination(new java.net.URI("file:" + destination)));
            } catch (URISyntaxException ex) {
                Logger.getLogger(Printing2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        PrinterResolution pr = new PrinterResolution(120, 144, PrinterResolution.DPI);
        attr_set.add(pr);

        pj.setPrintable(new MyPrintable(textParam, spesialWords, gotoX2), pf);
        if (!destination.equalsIgnoreCase("document.pdf")) {

            try {
                //System.out.println("ini print");
                if (destination.equalsIgnoreCase("xps")) {
                    pj.print();
                } else {
                    pj.print(attr_set);
                }
            } catch (PrinterException e) {
                System.out.println(e);
                javax.swing.JOptionPane.showMessageDialog(rootPane, "Gagal mencetak struk, Silahkan lihat pengaturan printer anda");
            }
        }

    } catch (IOException ex) {
        //System.out.println("testing cuuy");
        Logger.getLogger(Printing2.class.getName()).log(Level.SEVERE, null, ex);
    }

}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题: 将lx-300 + II的驱动程序打印机更改为lx-800 我不知道为什么。 但它工作得很好