How to print saved txt file in java using printer

I created a text file using java and saved it using this code:

BufferedWriter bfw;
bfw = new BufferedWriter(new FileWriter("D:\\abc.txt"));

Now I want to call the printer from my java code to print the file, how to do it?

+4
source share
2 answers

An easy way to print to a specific printer that you select in the printdialog dialog box:

    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    text.print(null, null, true, null, null, false);

Printing on a default printer without printdialog:

    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    text.print(null, null, false, service, null, false);
0
source

Perhaps look at an API that supports printing.

If you are using java 1.7 above, you can use this one. You can find an example inside the documentation.

http://docs.oracle.com/javase/7/docs/api/javax/print/package-summary.html

0
source

Source: https://habr.com/ru/post/1569545/


All Articles