如何在Epson打印机上设置打印机可打印区域?

时间:2018-03-08 20:26:38

标签: java printing epson

我的Epson打印机设置边距或可打印区域宽度有问题,我不确定。我尝试了一切,设置了驱动程序,以及一切。

我正在使用两台打印机EPSON TM-T20II Receipt和EPSON TM-H6000II Receipt并且两者都有问题。测试页面打印正确,但我的数据不会在页面中传播。

这是我正在使用的代码。

public class Printing implements Printable {
    private String stringToPrint;

    public Printing(String stringToPrint) {
        this.stringToPrint = stringToPrint;
    }
    @Override
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        if (pageIndex >= 1) {
            return Printable.NO_SUCH_PAGE;
        }
        Paper paper = new Paper();
        double margin = 0;
        paper.setImageableArea(margin, margin, 612.0, 792.0);
        pf.setPaper(paper);
        g.setColor(Color.black);
        g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 13));
        g.translate(0, 0);
        String printers = paper.getImageableHeight() + " " + paper.getImageableWidth() + "---" + pf.getHeight() + " "
            + pf.getWidth();
        Writer fw = null;
        try {
            fw = new OutputStreamWriter(new FileOutputStream("printingData.txt"), StandardCharsets.UTF_8);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter writer = new PrintWriter(bw);
        writer.print(printers);
        writer.close();
        int x = 0;
        int y = 100;
        for (String line : stringToPrint.split("\n")) {
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
        }
        return Printable.PAGE_EXISTS;
    }
    public static void printer(String printerData, Boolean bill, String designatedPrinter)
        throws IOException, PrinterException {
        try {

            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            PrintService designatedService = null;
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
            AttributeSet aset = new HashAttributeSet();
            aset = new HashAttributeSet();
            aset.add(ColorSupported.NOT_SUPPORTED);
            String printers = "";
            for (int i = 0; i < printServices.length; i++) {
                printers += " service found " + printServices[i].getName() + "\n";
            }
            for (int i = 0; i < printServices.length; i++) {
                System.out.println(" service found " + printServices[i].getName());
                if (printServices[i].getName().equalsIgnoreCase(designatedPrinter)) {
                System.out.println("I want this one: " + printServices[i].getName());
                    designatedService = printServices[i];
                    break;
                }
            }
            Writer fw = new OutputStreamWriter(new FileOutputStream("printing.txt"), StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter writer = new PrintWriter(bw);
            writer.print(printers);
            writer.close();
            PrinterJob pj = PrinterJob.getPrinterJob();
            pj.setPrintService(designatedService);
            Printable painter;

            // Specify the painter
            painter = new Printing(printerData);
            pj.setPrintable(painter);
            pj.print();

        } catch (PrinterException e) {
            Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter writer = new PrintWriter(bw);
            e.printStackTrace(writer);
            writer.close();
        }
    }
}

What i got from printer.

1 个答案:

答案 0 :(得分:1)

问题是您正在创建新页面,但未将其添加到PrinterJob

您可以添加以下评论。

// Specify the painter
painter = new Printing(printerData);
pj.setPrintable(painter);
PageFormat pf0 = pj.defaultPage();
Paper p = pf0.getPaper();
p.setImageableArea(0, 0, pf0.getWidth(), pf0.getHeight());
pf0.setPaper(p);
pj.validatePage(pf0);
Book book = new Book();
book.append(painter, pj.validatePage(pf0));
pj.setPageable(book);
pj.print();

print函数中不需要它。