如何在爱普生打印机上设置打印机可打印区域?

How to set printer printable area on Epson printer?

我在为我的 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();
        }
    }
}

问题是您正在创建新页面但没有将其添加到 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 函数中不需要它。