简体   繁体   English

如何通过Java或javascript在客户端打印出任何文本内容?

[英]How to print out any text content at client side by java or javascript?

i have a web application which is coded in Java. 我有一个用Java编码的Web应用程序。 I need to print out a text content(not a web page) at client side by choosing the printer. 我需要通过选择打印机在客户端打印出文本内容(而不是网页)。 I can do this at server side with Java but how should i overcome at client side? 我可以在服务器端使用Java来做到这一点,但是在客户端应该如何克服呢?

Should i prefer javascript or applet? 我应该使用javascript还是applet? And can i have the solution choose a printer among all printers? 我可以让解决方案在所有打印机中选择一个打印机吗?

Thanks in advance... 提前致谢...

Printer selection is in the OS. 打印机选择在OS中。 I don't know if you can do it with Java, but I know for fact you can't do it in JavaScript. 我不知道您是否可以使用Java来做到这一点,但是我知道您实际上无法在JavaScript中做到这一点。

You can use applet for client side printing.` 您可以使用小程序进行客户端打印。

public void paint(Graphics g) {
    TextArea display = new TextArea(1, 80);
    try {
        PrintService printService = getPrintService("printerName");
        if(printService == null)
            printService = PrintServiceLookup
                .lookupDefaultPrintService();

        printData(printService , "Printing text");
        g.drawString(
                " \n The Print was Successfull..  ",
                10, 10);
    } catch (Exception e) {
        System.out.println("Exception was thrown. Exception is \t : " + e);
    }
}

Print text to selected print device 将文本打印到选定的打印设备

private boolean printData(PrintService printService , String printText) {
    try {
        SimpleDoc doc;
        doc = new SimpleDoc(printText.getBytes(),
                javax.print.DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        DocPrintJob job = printService.createPrintJob();
        job.print(doc, new HashPrintRequestAttributeSet());
        System.out.println("Job sent to printer succesfully");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

This code for choose a printer among all printers 该代码用于在所有打印机中选择一个打印机

private PrintService getPrintService(String name) {
    PrintService pService = null;
    if (name == null || name.trim().length() == 0)
        return null;
    PrintService pServices[] = PrintServiceLookup.lookupPrintServices(null, null);;
    int i = 0;
    do {
        if (i >= pServices.length)
            break;
        String pServiceName = pServices[i].getName();
        if (name.equalsIgnoreCase(pServiceName)) {
            pService = pServices[i];
            break;
        }
        i++;
    } while (true);
    return pService;
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在客户端使用JavaScript提取文本 - How to extract text using javascript on client side 客户端javascript与服务器端Java的速度有多快? - How fast is client side javascript versus server side Java? 如何在Java中将文本打印为HTML - How to print out text as HTML in Java 在java web应用程序中如何从客户端打印服务器文件 - In java web application how to print server file from client side 如何找出GWT(客户端)中缺少哪些Java类? - How to find out which Java classes are missing in GWT (client side)? 在服务器端加密(使用Java)和在客户端解密(使用任何javascript密码库) - encryption on the server side (using java) and decryption on the client side(using any javascript crypto library) 如何使用“text / rtf”内容正确打印出JTextPane的硬拷贝? - How to correctly print out a hard copy of a JTextPane with “text/rtf” content? 如何在客户端从JavaScript执行Java文件 - How to execute Java file from JavaScript on client's side 如何在Java中读取文本文件的第一行并打印出来? - How to read the first line of a text file in java and print it out? 如何复制文本文件,并用Java打印输出原始和复制的文件 - How to copy a text file, and print out the original and copied file in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM