收据只打印一张纸A4和剪裁java打印

时间:2017-02-17 12:08:37

标签: java printing receipt

我使用上面的代码打印收据

问题是,当我有超过10个项目时,它只打印10个项目并减少收据

我注意到这一点,当我用A4纸打印它只打印一页并停止

有没有办法解决这个问题

SimpleDateFormat df = new SimpleDateFormat();
DecimalFormat decf = new DecimalFormat("#,##0.00"); 
String SPACES20 = "          ";//20 spaces
String SPACES15 = "               ";//15 spaces    
String SPACES10 = "          ";//10 spaces
String SPACES7 = "     ";//7 spaces
String SPACES5 = "     ";//5 spaces
String uline   = "___________________________________________";
String dline      = "-------------------------------------------";
String etoileline = "*******************************************";
int x = 0;
int y = 15;
int lineheight = 13;
int rightEdge = 182;

Ticket ticket;
String type = "";

public TicketImpression(Ticket ticket_, String type_){
    this.ticket = ticket_;
    this.type= type_;
}

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

    if (page > 0) {
        return NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());              

    try {
        g2d.drawImage(ImageIO.read(getClass().getResource("/resources/logo_bricosmart_52_50.png")), 67, 5, null);
    } catch (IOException e) {           
        e.printStackTrace();
    }

    Font font = new Font("Monospaced", Font.BOLD, 10);
    g2d.setFont(font);      

    g2d.drawString("Company Name", 16, 75);

    font = new Font("Monospaced", Font.BOLD, 8);
    g2d.setFont(font);

    int i =1;

    g2d.drawString("       Tél : 05-22-44-57-68", x, 93);// Tél     

    g2d.drawString("    Site web : www.company.ma", x, 101);// site web             
    y = 101;

    // some rows to print ...........

    g2d.drawString(" Merci de votre visite", x, y+lineheight*i++);      
    i++;                        

    g2d.drawString("                                           ", x, y+lineheight*i++);

    return PAGE_EXISTS;
}

public static void ImprimerTicket(Ticket ticket, String type) {
    PageFormat format = new PageFormat();
    Paper paper = new Paper();

    double paperWidth = 3;// 3.25
    double paperHeight = 3.69;// 11.69
    double leftMargin = 0.05;
    double rightMargin = 0.10;
    double topMargin = 0;
    double bottomMargin = 0.01;

    String printer =Utils.printerName;

    paper.setSize(paperWidth * 200, paperHeight * 200);
    paper.setImageableArea(leftMargin*200, topMargin * 200, (paperWidth - leftMargin - rightMargin) * 200,
        (paperHeight - topMargin - bottomMargin) * 200);

    format.setPaper(paper);

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(OrientationRequested.PORTRAIT);

    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();         
        printServiceAttributeSet.add(new PrinterName(printer, null));
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
        job.setPrintService(printServices[0]);// set printer
        format = job.validatePage(format);
        job.setPrintable(new TicketImpression(ticket, type), format);
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));
        job.print(pras);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我称这个类为

  

TicketImpression.ImprimerTicket(ticketObject,“”);

0 个答案:

没有答案