wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Printing Word Docs file in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Printing Word Docs file in Java" Watch "Printing Word Docs file in Java" New topic
Author

Printing Word Docs file in Java

vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
Hi friends,

Im using Javax.print package to Print txt/html files and working fine
but when i am trying to print microsoft word document files.its not working and it prints some ascii values in the printouts.

following are the java program that i used

import java.io.*;
import java.util.*;
import java.util.Properties;
import java.net.URI;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;

public class sample
{public static void main(String args[])
{
try {
InputStream is =new BufferedInputStream(new FileInputStream("test.doc"));
//Find the default service



DocFlavor flavor =DocFlavor.INPUT_STREAM.AUTOSENSE;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService services[] = PrintServiceLookup.lookupPrintServices(flavor,aset);

//Create the print job


DocPrintJob job = services[0].createPrintJob();

Doc doc = new SimpleDoc(is, flavor, null);
//Print it
job.print(doc, null);
//It is now safe to close the input stream
is.close();
} catch (PrintException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}



}
}

any help will be appreciated

cheers,
vasu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Are your sure that MS Word files are supported by javax.print on your platform? It would seem that the auto-sense feature is not picking up the format.


Ping & DNS - my free Android networking tools app
vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
Hi ,

Currently im using windows platform,
please help me how can i get the solution
or tell me any other way to Print DOC files through Java

cheers
vasu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
It should be possible (via java.lang.Runtime.exec) to run a VBS or JS script which scripts Word itself into printing the document. Search MSDN for WSH (Windows Scripting Host) and you will find examples of how to proceed.
vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
hi

through java.lang.Runtime.exec it will work fine
but i want solution through javax.print package.
if im able to get printout for txt/html for above program
why dont im not able to get for DOC and RTF files
please give me solution for javax.print package to print DOC and RTF files

thanks in advance
help from anyone will be appreciated

cheers
vasu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
I have never used javax.print, so I can't give you an answer to that. Like I said before, it would seem that it can't handle doc or rtf files. The javadocs for the relevant classes seem to indicate as much. The article at http://www-106.ibm.com/developerworks/java/library/j-mer0322/ might give you starting points for further research.
vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
Hi,

Is anyother way to Print DOC and RTF files using java?
if yes provide the necessary steps and sample programs,this would help me to speedup my project.
and that should work platform independent.

thanks in advance

cheers
vasu
Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
Check out POI from the Apache Software Foundation Jakarta project.

POI is a set of APIs for reading word and excel documents. You could then reformat them into something that javax.print would handle. I do NOT have any sample code for this but I believe the API comes with same.


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing Word Docs file in Java