Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java打印文本_Java_Printing_Network Printers - Fatal编程技术网

使用Java打印文本

使用Java打印文本,java,printing,network-printers,Java,Printing,Network Printers,我们正在尝试使用Java将一些文本从web应用程序发送到网络打印机。为了做一个小测试,我们创建了一个独立的java程序并执行了它。最初,它都是用重叠的字符打印出来的。阅读Orcale bug报告后,我们将JRE从1.7更改为1.6。然后一切都很好。但如果我使用Web应用程序打印,同样的程序会再次打印重叠字符。我们已将web应用程序中的设置更改为使用JRE 1.6和Tomcat 6。程序如下所示 public static void main(String[] args) { Te

我们正在尝试使用Java将一些文本从web应用程序发送到网络打印机。为了做一个小测试,我们创建了一个独立的java程序并执行了它。最初,它都是用重叠的字符打印出来的。阅读Orcale bug报告后,我们将JRE从1.7更改为1.6。然后一切都很好。但如果我使用Web应用程序打印,同样的程序会再次打印重叠字符。我们已将web应用程序中的设置更改为使用JRE 1.6和Tomcat 6。程序如下所示

public static void main(String[] args) {
        TextPrint tp = new TextPrint();
        try {
            tp.textprint("test");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void textprint(String nmae) throws IOException {

        PrintService printService = getPrintService("\\\\ADMIN-PC\\TVS MSP 250 Star");
        PrinterJob printJob = PrinterJob.getPrinterJob();
        try {
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(new Copies(1));
            attributes.add(OrientationRequested.PORTRAIT);
            printJob.setPrintService(printService);
            attributes
                    .add(new PrinterResolution(120, 72, ResolutionSyntax.DPI));

            PageFormat pPageFormat = new PageFormat();
            Paper pPaper = pPageFormat.getPaper();
            pPaper.setSize(432, 432);
            pPaper.setImageableArea(0, 0, pPaper.getWidth(), pPaper.getHeight());
            pPageFormat.setPaper(pPaper);
            printJob.setPrintable(this, pPageFormat);
            printJob.print();
        } catch (PrinterException e1) {
            e1.printStackTrace();
        }
    }

    public void drawString(Graphics g, String line, int wc, int lineW, int y) {
        g.drawString(line, (300 - lineW) / 2, y);// center

    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException {
        if (pageIndex > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D) graphics;

        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        final String[][] priceTable = {
                { "1", "Paracetamol", "BAT123456", "03/20", "999", "8934.78",
                        "38865.22", "|" },
                { "2", "Distillation Water - Oral suspension (Ped)",
                        "BAT123457", "10/20", "99", "45.00", "4455.00", "|" },
                { "3", "Vicks", "BAT123458", "M05-21", "34", "453.00",
                        "15402.00", "|" },
                { "4", "Test Name ", "BAT123459", "12/21", "56", "53.00",
                        "2968.00", "|" },
                { "15", "No Name Tablets", "BAT123460", "06-22", "23",
                        "456.00", "10488.00", "|" },
                { "10", "Others", "BAT123461", "02/23", "7", "76.00", "532.00",
                        "|" } };

        String[] columnW = { "10", "27", "200", "262", "295", "312", "340",
                "410" };
        String[] columnCharLen = { "2", "30", "10", "5", "2", "7", "8", "1" };
        String[] align = { "R", "L", "L", "L", "R", "R", "R", "R" };
        for (int i = 0; i < priceTable.length; i++) {
            int y = 84 + (i * 12);
            for (int j = 0; j < priceTable[i].length; j++) {
                int xAxis = Integer.valueOf(columnW[j]);
                int xChar = Integer.valueOf(columnCharLen[j]);
                int diff = xChar - priceTable[i][j].length();
                if (diff > 0 && align[j].equalsIgnoreCase("R")) {
                    xAxis = (int) (xAxis + (diff * 5.6));
                }
                System.out.println(" X for - " + priceTable[i][j] + " - "
                        + xAxis);
                graphics.drawString(priceTable[i][j], xAxis, y);//
            }
        }

        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }

运行代码的计算机是否安装了JRE 1.7?指定1.6法规遵从性并不一定意味着代码由1.6 JRE运行。它同时拥有JRE 1.6和JRE 1.7。但是我们使用eclipse运行该程序,所有设置都指向JRE 1.6。但是,我们将尝试完全删除JRE 1.7。请检查项目的运行/调试配置。确保他们指向JRE 1.6。谢谢。这很有帮助。虽然我们的所有设置都指向JRE 1.6,但只有在从服务器系统中完全删除JRE 1.7之后,我们才能从点阵打印机获得正确的打印。不确定Orcale什么时候能解决这个问题试试看