Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing Word Docs file in Java

 
vasu devan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vasu devan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic