Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 273823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:30:57+00:00 2026-05-12T00:30:57+00:00

I have a text file, and I need to print it to a specific

  • 0

I have a text file, and I need to print it to a specific network printer. I know the name of the printer.

Until now I have made a Printable class to print my file (ticket).

public class TicketPrintPage implements Printable {

    private File ticket;

    public TicketPrintPage(File f) {
        ticket = f;
    }

    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        int interline = 12;
        Graphics2D g2 = (Graphics2D) g;
        g2.setFont(new Font("CourierThai", Font.PLAIN, 10));
        int x =  (int) pf.getImageableX();
        int y = (int) pf.getImageableY();

        try {
            FileReader fr = new FileReader(ticket);
            BufferedReader br = new BufferedReader(fr);

            String s;
            while ((s = br.readLine()) != null) {
                y += interline;
                g2.drawString(s, x, y);
            }
        } catch (IOException e) {
            throw new PrinterException("File to print does not exist (" + ticket.getAbsolutePath() +") !");
        }
        return Printable.PAGE_EXISTS;
    }
}

I call this TicketPrintPage this way :

public void printTicketFile(File ticket, int orientation) throws PrinterException {
    if (!ticket.exists()) {
        throw new PrinterException("Ticket to print does not exist (" + ticket.getAbsolutePath() + ") !");
    }
    PrinterJob pjob = PrinterJob.getPrinterJob();
    // get printer using PrintServiceLookup.lookupPrintServices(null, null) and looking at the name
    pjob.setPrintService(getPrintService());
    // job title
    pjob.setJobName(ticket.getName());

    // page fomat
    PageFormat pf = pjob.defaultPage();
    // landscape or portrait
    pf.setOrientation(orientation);
    // Paper properties
    Paper a4Paper = new Paper();
    double paperWidth  =  8.26;
    double paperHeight = 11.69;
    double margin = 16;
    a4Paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
    a4Paper.setImageableArea(
                margin,
                //0,
                margin,
                //0,
                a4Paper.getWidth()- 2 * margin,
                //a4Paper.getWidth(),
                a4Paper.getHeight()- 2 * margin
                //a4Paper.getHeight()
                ); // no margin = no scaling
    pf.setPaper(a4Paper);
    // Custom class that defines how to layout file text
    TicketPrintPage pages = new TicketPrintPage(ticket);
    // adding the page to a book
    Book book = new Book();
    book.append(pages, pf);
    // Adding the book to a printjob
    pjob.setPageable(book);
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        // No jobsheet (banner page, the page with user name, job name, date and whatnot)
    pras.add(JobSheets.NONE);
    // Printing
    pjob.print(pras);
}

It works not so bad but :
– I doesn’t work for more than one page of text (found some algorithms for that but well)
– I can’t get to know when the printer is done printing, and if I try printing two or more tickets in a row the printer will return a Printer not ready message.

So the question again is : Isn’t there a simple way to print a text file to a printer ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:30:58+00:00Added an answer on May 12, 2026 at 12:30 am

    I’m not sure if this solves your problem but I use the following to print a text file

    FileInputStream textStream;
    textStream = new FileInputStream(FILE_NAME);
    
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc mydoc = new SimpleDoc(textStream, flavor, null);
    
       PrintService[] services = PrintServiceLookup.lookupPrintServices(
                    flavor, aset);
       PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    
       if(services.length == 0) {
           if(defaultService == null) {
                 //no printer found
    
           } else {
                //print using default
                DocPrintJob job = defaultService.createPrintJob();
                job.print(mydoc, aset);
    
           }
    
        } else {
    
           //built in UI for printing you may not use this
           PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
    
    
            if (service != null)
            {
               DocPrintJob job = service.createPrintJob();
               job.print(mydoc, aset);
            }
    
        }
    

    You may not need the ServiceUI, but I think you could use PrintService[] services to get a list of printers available for printing. And using an input stream and the Doc class you can print a file to a printer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1
I have script code for zebra TLP2844 printer in a text file. I need
I have text file with some text information and i need to split this
I have a text file with dos elements - hex(00) for example. I need
I have a text file which contains data seperated by '|'. I need to
I have a large text file I am reading from and I need to
I have a a large text file (over 70mb) and need to count the
I have crystal report and I need to convert it to text file. Currently
I have a directory path stored in a text file, and I need to
I have a tab delimited text file that I need to upload to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.