0

在这里我必须在 pageIndex 上工作,但不知道如何设置它,这样我就可以在 POS 打印机(纸卷)中打印尺寸大于 A4 纸张高度的长收据

    public int print(Graphics g, PageFormat pf, int pageIndex) {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pf1 = pj.getPageFormat(attr_set);
    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    attr_set.add(res);
    g2 = (Graphics2D) g;
    //initialising X & Y coordinates
    int x_header = 20;
    int y_header = 0;

    Easy.log("In Print method--------------------------------------");

    //call the method which will print header
    if (printData == 1) {  //this will print KOT
        pageWidth = (float) pf1.getImageableWidth();
        y_header=printKOTDetails(x_header,y_header);
    }
     return PAGE_EXISTS;
    }

配置方法是:

    private void printerConfiguration() throws PrinterException {

    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
    Media res = (Media) defaultPrintService.getDefaultAttributeValue(Media.class);
    PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
    attr_set.add(res);

    PrinterJob pj = PrinterJob.getPrinterJob();
    pf1 = pj.getPageFormat(attr_set);
    pj.setPrintService(printService);

    Paper paper = new Paper();
    double margin = 2;

    paper.setImageableArea(margin, 0, paper.getImageableWidth(), paper.getImageableHeight());
    pf1.setPaper(paper);

    pj.setPrintable(this, pf1);
    pj.print();
}
4

0 回答 0