我将如何从 Java 程序打印文本?(爪哇)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7198855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:02:19  来源:igfitidea点击:

How would I go about printing text from a java program? (Java)

javatextprinting

提问by Confiqure

I don't even know if this is possible and I highly doubt it is, but if you can, can you please tell me how? I just want to know how to print some text from a printer.

我什至不知道这是否可行,我非常怀疑它是否可行,但如果可以,请告诉我如何做?我只想知道如何从打印机打印一些文本。

Any thoughts?

有什么想法吗?

回答by Timothy Radonich

Here is something that is even easier.

这是更容易的事情。

import javax.swing.JTextPane;
import java.awt.print.PrinterException;

public class TestPrint {


    public static void main(String[] args) throws PrinterException {

        JTextPane textPane = new JTextPane();

        textPane.setText("test text string - Hello World! Are you there?");

        textPane.print();

    }
}

Output: popup

输出:弹出

enter image description here

enter image description here

回答by RealHowTo

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class PrintText {

  public static void main(String[] args) throws PrintException, IOException {

    String defaultPrinter = 
      PrintServiceLookup.lookupDefaultPrintService().getName();
    System.out.println("Default printer: " + defaultPrinter);
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    // prints the famous hello world! plus a form feed
    InputStream is = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));

    PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));

    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc doc = new SimpleDoc(is, flavor, null);
    DocPrintJob job = service.createPrintJob();

    PrintJobWatcher pjw = new PrintJobWatcher(job);
    job.print(doc, pras);
    pjw.waitForDone();
    is.close();
  }
}

class PrintJobWatcher {
  boolean done = false;

  PrintJobWatcher(DocPrintJob job) {
    job.addPrintJobListener(new PrintJobAdapter() {
      public void printJobCanceled(PrintJobEvent pje) {
        allDone();
      }
      public void printJobCompleted(PrintJobEvent pje) {
        allDone();
      }
      public void printJobFailed(PrintJobEvent pje) {
        allDone();
      }
      public void printJobNoMoreEvents(PrintJobEvent pje) {
        allDone();
      }
      void allDone() {
        synchronized (PrintJobWatcher.this) {
          done = true;
          System.out.println("Printing done ...");
          PrintJobWatcher.this.notify();
        }
      }
    });
  }
  public synchronized void waitForDone() {
    try {
      while (!done) {
        wait();
      }
    } catch (InterruptedException e) {
    }
  }
}

回答by Tushar Chutani

What you can do is write to a file and then you can use Desktopclass to print it. for more infomation on desktop class go here

您可以做的是写入文件,然后您可以使用Desktop类来打印它。有关桌面课程的更多信息,请访问此处

here is the program


import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class abc {


    public static void main(String[] args) throws IOException{

        BufferedWriter out = new BufferedWriter(new FileWriter("1.txt"));
        out.write("Hello this is a test");
        out.flush();
        out.close();

        File ff = new File("1.txt");

        Desktop desktop = Desktop.getDesktop();
      desktop.print(ff);

}

}

}

回答by ziesemer

Following Man o War's and bmargulies' comments, take a look at the Java Tutorial Printing Lessonand the Java Print API.

按照 Man o War 和 bmargulies 的评论,查看Java 教程打印课程Java Print API

(Primarily posting this answer in an attempt to either get this question some additional attention / competitive answers, or at the least, to simply remove this from the growing list of unanswered questions.)

(主要是发布这个答案,试图让这个问题得到一些额外的关注/竞争性答案,或者至少,简单地从不断增长的未回答问题列表中删除它。)