Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get file details and print

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.io.*;
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";
try{
BufferedReader in = new BufferedReader(new FileReader("text.txt"));

while ((str = in.readLine()) != null) {
str1=str1+str+"\n";


}

in.close();
} catch (IOException e) {
System.out.println(e);
}

System.out.println(str1);


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

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?
Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to set the y-coordinate yourself.

Just to give a starting point:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic