how to print a saved txt file in java using a printer

how to print a saved txt file in java using a printer

By : user2957393
Date : November 24 2020, 01:01 AM
will help you A simple way to print to a specific printer which you select in a printdialog:
code :
    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    text.print(null, null, true, null, null, false);
    JEditorPane text = new JEditorPane("file:///D:/abc.txt");
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    text.print(null, null, false, service, null, false);

Share : facebook icon twitter icon
Print text File to specific printer in java

Print text File to specific printer in java

By : Marius Ionescu
Date : March 29 2020, 07:55 AM
Automatically Print the Back side of All print jobs on Duplexed printer with same file

Automatically Print the Back side of All print jobs on Duplexed printer with same file

By : Nurudin
Date : March 29 2020, 07:55 AM
Hope that helps The answer provided by Douglas Anderson is the easiest option. However if you also want this to get enabled in a client-server environment, then it would be difficult. One option is to go for a print processor which would insert this template job after every page. However this may affect drivers that use their own print processors. Another easy solution - if you need this backside template for all jobs that are fired to this printer, why not use a preprinted paper with the template already printed? So you dont need to write any software at all!!
How to print using a line printer dirictly from java using the fonts of the printer?

How to print using a line printer dirictly from java using the fonts of the printer?

By : user3447325
Date : March 29 2020, 07:55 AM
hope this fix your issue This code doesn't require any Swing related component but still it needs Graphics class of awt, but you can print a text from console there is no UI component being displayed, just tested it:
code :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;

public class DirectPrint implements Printable {

private PrintService[] printService;
private String text;

public DirectPrint() {
    this.printService = PrinterJob.lookupPrintServices();

public static void main(String[] args) {
    DirectPrint lt = new DirectPrint();
    lt.printString("If this text gets printed, it will have worked! ;D");

public void printString(String input) {

    this.text = input;

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new PageRanges(1, 1));
    aset.add(new Copies(1));

    PrinterJob printJob = PrinterJob.getPrinterJob();

    try {
        //index of installed printers on you system
        //not sure if default-printer is always '0'
    } catch (PrinterException err) {

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    g.drawString(String.valueOf(this.text), 14, 14);
    return PAGE_EXISTS;
  InputStream in = null;
try {
log.debug("preparing input stream");
in = getFileTobePrinted();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

// find the printing service
log.debug("fetching print service");
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("lq2170", null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null,  attributeSet);

// create the print job
log.debug("creating print job");
DocPrintJob job = services[0].createPrintJob();
Doc doc = new SimpleDoc(in, flavor, null);

// monitor print job events
log.debug("preparing print job monitor");
PrintJobWatcher watcher = new PrintJobWatcher(job);

// print it
log.debug("start printing");
job.print(doc, null);

// wait for the print job is done
log.debug("waiting for the printing to finish");

log.debug("done !");
  } finally {
if (in != null) try { in.close(); } catch(Exception e) {}
Java - Print numbers saved to text file in past 24 hours

Java - Print numbers saved to text file in past 24 hours

By : KikeSIlva
Date : March 29 2020, 07:55 AM
may help you . You are right that you need to change the format of your file. I would want to use either Instant.toString() or LocalDateTime.toString() for generating the time stamps. So each line would become like
code :
5 2017-11-06T14:52:31.434Z
How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer

How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer

By : Halil Matıracı
Date : March 29 2020, 07:55 AM
it fixes the issue To print a PrintDocument object using the Microsoft Print to PDF printer without prompting for a filename, here is the pure code way to do this:
Related Posts Related Posts :
  • Why is it mandatory for ternary operations to return a value?
  • How can I make my listview rows different heights
  • Array declaration trick : Is it bad to use it?
  • Calling different classes with if else statements
  • Free API for speech recognition system
  • Java: Objects in JFrame are messed up
  • Is there a similar of MessageBox of ExtJS in ADF Faces
  • Passing HttpServletResponse in Spring Boot controller method
  • Failed to load libraries: [netty_tcnative_linux_arm_32, netty_tcnative_linux_arm_32_fedora, netty_tcnative_arm_32, netty
  • How to build a small documents storage?
  • Getting pixel position of a text in an image
  • How do I get a list of Methods called from a Class in Eclipse IDE?
  • Convert string to day of week (not exact date)
  • Unable to understand how rounding is working for decimal digits (Java)
  • How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
  • JTable Renderer not working with RowSorter?
  • From java.util.concurrent.Future<T> to play.libs.F.Promise<T>: How to do that in Java?
  • jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub
  • How do i add a tooltip to existing gwt Textbox?
  • actor replying to non-actor
  • Google Cloud Messaging - server connection
  • Which classes are unmodifiable and cannot be intrumented and is there a way to bypass it?
  • Java & Jersey 1.17: Custom default response headers
  • Java rounding error
  • Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?
  • Arquillian test class variables lose value
  • java.util.ConcurrentModificationException while inserting in ArrayList
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org