• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Printing in java

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
im trying to print a file after i take the filename as an argument from the user.
I can successfullyprint txt files but im getting garbage when i try to print .doc files ......
any Clue as to what going on .
any help is appreciated.
Thanks
Pranav
here is the code im using .

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 String fileName;
private Font fnt = new Font("Helvetica", Font.PLAIN, 10);
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");

}
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;
}
}
}

 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably your ".doc" file has been saved from a Word Processor in Microsoft Word format. To print such a file you really need to make a call to some application which undestands the file format. Java's printing system can't really be expected to understand every file format in the world ...
 
Pranav Jaidka
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frank .......any source code available which may help
for this.....
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic