File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Get file details and print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get file details and print" Watch "Get file details and print" New topic

Get file details and print

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
Dear all,

I am trying to do a program that get the data from a file and send it straight to printing. My code is as below

import java.awt.*;
import java.awt.print.*;
import java.awt.geom.*;
//import java.sql.*;
import java.text.*;
import java.util.*;

class PrintObject implements Printable
public int print (Graphics g, PageFormat f, int pageIndex)

String str="NONE",str1="NONE1";
BufferedReader in = new BufferedReader(new FileReader("text.txt"));

while ((str = in.readLine()) != null) {


} catch (IOException e) {


switch (pageIndex)
case 0 : g.drawString(str1, 100,100);

default: return NO_SUCH_PAGE; // No other pages

public class printest
public static void main (String[] args)
// Create an object that will hold all print parameters, such as
// page size, printer resolution. In addition, it manages the print
// process (job).
PrinterJob job = PrinterJob.getPrinterJob();

// It is first called to tell it what object will print each page.
job.setPrintable(new PrintObject());

// Then it is called to display the standard print options dialog.
if (job.printDialog())
// If the user has pressed OK (printDialog returns true), then go
// ahead with the printing. This is started by the simple call to
// the job print() method. When it runs, it calls the page print
// object for page index 0. Then page index 1, 2, and so on
// until NO_SUCH_PAGE is returned.
try { job.print(); }
catch (PrinterException e) { System.out.println(e); }

Problem is when run the program the print dialog box display there is 9999 pages to print. So i change the dialog box to print 1 page and the printing only came out 1 line. It also didnt leave a line meaning /n didnt perform. Can anyone correct me?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You have to set the y-coordinate yourself.

Just to give a starting point:
I agree. Here's the link:
subject: Get file details and print
It's not a secret anymore!