我如何在java中打印到2个字符串

时间:2014-05-20 17:22:05

标签: java printers

生病尽量让这个尽可能简单

我试图打印包含此内容的帐单:

String text="Home\n";
       text+="Sweet\n"+"Home";
String cut_page="((char)12)m"; //this string is just a reference

我想在打印机“cut_page”之后立即向打印机打印字符串“text”。

我想按照这个顺序这样做,因为连接对我来说没有用,因为由于某种原因,它会缩短在短语的中间位置。


如果使用真正的方法,如果上面的内容对你来说足够了,那么请忽略这部分

String param = request.getParameter("empresa");
    System.out.println("empresa");
    System.out.println(param);
    Gson gson = new Gson();
    String corte="m";
    DetalleImpresion detalle = gson.fromJson(param, DetalleImpresion.class);
    System.out.println(detalle);
    ArrayList<HashMap<String, Object>> detalles_factura = detalle.getDetalle_factura();

    String total = detalle.total;
    String direccion = detalle.direccion;
    String fecha = detalle.fecha;
    String facturacli = detalle.factura;
    String cliente = detalle.cliente;
    String empresa = detalle.empresa;
    String clienteNombre = detalle.cliente_nombre;
    String guia = detalle.guia_remision;

    String factura = empresa+"\t"+facturacli+"\n"+"\t"+fecha+"\n";
            factura+="\t"+clienteNombre+"\tRUC\n"+"\t"+direccion+"\n"+guia+"\n";//+"=======================================\n";

    for(int i=0; i< detalles_factura.size();i++){
        HashMap<String, Object> row = detalles_factura.get(i);      
        factura+=row.get("cantidad").toString() +"\t" + row.get("producto_nombre").toString()+
                "\t"+row.get("precio_unitario").toString()+"\t"+row.get("a_pagar").toString()+"\n";                 
    }   

    StringBuilder facfinal = new StringBuilder();
    facfinal.append(factura);
    facfinal.append(corte);
    String ff=facfinal.toString();
    //factura+=corte;

    String printerName = request.getParameter("dispositivo");
    System.out.println(printerName);
    AttributeSet attributeSet = new HashAttributeSet();
    attributeSet.add(new PrinterName(printerName, null));
    attributeSet = new HashAttributeSet();
    attributeSet.add(ColorSupported.NOT_SUPPORTED);
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, attributeSet);
    int indexOfEmpsonPrinter = -1;

    for( int i = 0 , count = services.length ;  i < count ; ++i ){
        System.out.println(services[i].getName());
        if( services[i].getName().equals(printerName) ){
            indexOfEmpsonPrinter = i;

        }

    }

    if(indexOfEmpsonPrinter!=-1){

        InputStream is = new ByteArrayInputStream(ff.getBytes("UTF8"));
        InputStream is2 = new ByteArrayInputStream(factura.getBytes("UTF8"));
        PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(2));


        Doc doc = new SimpleDoc(is, flavor, null);
        Doc doc2 = new SimpleDoc(is2, flavor, null);
        DocPrintJob job = services[indexOfEmpsonPrinter].createPrintJob();

        try {
            job.print(doc, pras);
        } catch (PrintException e) {
            e.printStackTrace();
        }

        is.close();
        is2.close();
    }
    else{
        System.out.println("Impresora no encontrada");
    }

1 个答案:

答案 0 :(得分:0)

来自DocFlavor API

  

不需要Java Print Service实例来支持以下内容   打印数据格式和打印数据表示类。事实上,一个   使用此类的开发人员不应该假设特定的打印   service支持与这些预定义对应的文档类型   doc flavors。

Java可能会自动传真&#34; text / plain&#34;来自您的打印作业的doc flavor。我遇到的大多数打印机都只有这方面的基本支持。

在您的情况下,我怀疑问题是打印机需要&#34; \ r \ n&#34;结束而不是Java&#34; \ n&#34;。但更一般地说,如果您实际实现自己的Printable对象,则可以更好地控制结果。

相关问题