在java中打印(在打印机上)时进行换行

bihw5rsg  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(217)

我要用java打印这个代码。我想知道是否有任何方法,当文本超出纸张的限制时,我会换行并在下一行打印。如果行超出了纸张的限制,就不会改变行。

int etiquetasPorPagina=7;
    int nEtiquetas=jtImprimir.getRowCount();
    int nPaginas=(nEtiquetas/etiquetasPorPagina)+1;    
    PrinterJob pj = PrinterJob.getPrinterJob();
    Book book = new Book();
    PagePrinter[] page = new PagePrinter[nPaginas+1];
    MediaSize isoA4Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4);
    float[] size = isoA4Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0]*72, size[1]*72);
    paper.setImageableArea(0.0, 0.0, size[0]*72, size[1]*72);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper); 
    Font font = new Font("Helvetica", Font.BOLD, 18);
    Font font2 = new Font("Helvetica", Font.BOLD, 30);   
    int label=0;
    int pagina=0;
    int n;
    while(pagina<(nPaginas))
    {       
    page[pagina]=new PagePrinter();
    if (label<(nEtiquetas)-(nEtiquetas%etiquetasPorPagina))
    {
        n=etiquetasPorPagina;
    }
    else
    {
        n=nEtiquetas%etiquetasPorPagina;
    }
    for (int i=0;i<n;i++)
    {
        page[pagina].addPrintElement(new MyItem(jtImprimir.getValueAt(label, 1).toString(),jtImprimir.getValueAt(label, 2).toString(), font, 50, 50+100*i));

    label++;        
    }
    book.append(page[pagina], pf);
    pagina++;        
    }     
    pj.setPageable(book);
    try {
        pj.print();
    } catch (PrinterException ex) {
        Logger.getLogger(ferreteria.class.getName()).log(Level.SEVERE, null, ex);
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题