File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes RandomAccessFile + Java printing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "RandomAccessFile + Java printing " Watch "RandomAccessFile + Java printing " New topic
Author

RandomAccessFile + Java printing

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi
I am trying to utilize Java's printing utility from java.awt.print . On java's web page, I found a code that allows to print a file from an applet. The problem is that it uses RandomAccessFile and I am trying to run this applet on the server, but I cannot load a file straight from the server to an applet. As a result I must use HTTP connection and read from a stream using URL connection, which does not allow me to use RAF since I can't use it with HTTP connection. Do you know of any way that this problem can be solved, or at least point me in the right direction.
Below is the print code from SUN:
import java.awt.*;
import java.awt.print.*;
import java.io.*;
public class PrintListing {
public static void main(String[] args) {
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Ask user for page format (e.g., portrait/landscape)
PageFormat pf = job.pageDialog(job.defaultPage());
// Specify the Printable is an instance of
// PrintListingPainter; also provide given PageFormat
job.setPrintable(new PrintListingPainter(args[0]), pf);
// Print 1 copy
job.setCopies(1);
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception e) { /* handle exception */ }
}
System.exit(0);
}
}
class PrintListingPainter implements Printable {
//private RandomAccessFile raf;
private BufferedReader raf;
private String fileName;
private Font fnt = new Font("Courier New", Font.PLAIN, 12);
private int rememberedPageIndex = -1;
private long rememberedFilePointer = -1;
private boolean rememberedEOF = false;
public PrintListingPainter(String file) {
fileName = file;
try {
// Open file
//raf = new RandomAccessFile(file, "r");
raf = new BufferedReader(new FileReader(fileName));
} catch (Exception e) { rememberedEOF = true; }
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
try {
// For catching IOException
if (pageIndex != rememberedPageIndex) {
// First time we've visited this page
rememberedPageIndex = pageIndex;
// If encountered EOF on previous page, done
if (rememberedEOF) return Printable.NO_SUCH_PAGE;
// Save current position in input file
rememberedFilePointer = raf.getFilePointer();
} else raf.seek(rememberedFilePointer);
g.setColor(Color.black);
g.setFont(fnt);
int x = (int) pf.getImageableX() + 10;
int y = (int) pf.getImageableY() + 12;
// Title line
g.drawString("File: " + fileName + ", page: " +
(pageIndex+1), x, y);
// Generate as many lines as will fit in imageable area
y += 36;
while (y + 12 < pf.getImageableY()+pf.getImageableHeight()) {
String line = raf.readLine();
if (line == null) {
rememberedEOF = true;
break;
}
g.drawString(line, x, y);
y += 12;
}
return Printable.PAGE_EXISTS;
} catch (Exception e) { return Printable.NO_SUCH_PAGE;}
}
}
Thank you in advance,
Alex

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RandomAccessFile + Java printing