Link to home
Start Free TrialLog in
Avatar of Narendranath
Narendranath

asked on

How to print a word document in java?

I have a word document(Contains 5 pages of text) in a folder i have to print this word document from my java program. Is it possible for me to print it.

If is, can I print PDF file also?
Avatar of Narendranath
Narendranath

ASKER

Please send me the solution with example.
hi,

You can read this docment

http://java.sun.com/j2se/1.4/docs/guide/jps/spec/JPSTOC.fm.html


Best Regard
PhuongNguyen
hi,

You can read this docment

http://java.sun.com/j2se/1.4/docs/guide/jps/spec/JPSTOC.fm.html


Best Regard
PhuongNguyen
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20149703
contains javax.print.*;
API but i am using java.awt.print API
can you tell me how to print word document using
this API.
How to print the word document in java 1.2
i am not able to print word document and PDF files.
please tell me the solution for this.
its very urgent for my application.
thanks.
Hello expert
I am waiting for your reply please send me the solution for my problem.
Avatar of Jim Cakalic
You can exec Acrobat with appropriate command line parameters to print a PDF. The command line parameters for Acrobat and Reader are as shown below (from Adobe documentation). These are unsupported but have worked for many developers.

AcroRd32.exe /p filename - executes the Reader and prints a file

AcroRd32.exe /t path printername drivername portname - Initiates Acrobat Reader, prints a file while suppressing the Acrobat print dialog box, then terminates Reader. The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).
printername - The name of your printer.
drivername - Your printer driver's name. Whatever appears in the Driver Used box when you view your printer's properties.
portname - The printer's port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.

Note: If using Acrobat, substitute Acrobat.exe in place of AcroRd32.exe in the command lines.

Actually exec'ing the AcroRd32.exe from Java can be somewhat troublesome as its location is not typically in the PATH environment variable. Like good win32 applications, it registers itself in the Windows registry. You can access the Windows registry from a Java program using a free package called JObjects from JST. The URL to the download page for JObjects is:
   http://www.jobjects.com/downloads/index.html

As I've needed to do this before, I have written the small utility class below that uses JObjects to find the registered path to an executable. The main shows how the class can be used. It uses the class to try to find the path to whatever is supplied as the first command line argument and then calls exec() using that path to start the program. To start the Acrobat Reader, you'd run it like:
   java WinRegPath acrord32.exe

---------- WinRegPath.java ----------
import com.jobjects.jst.*;
import java.util.*;

public class WinRegPath {
   private static final String subkey = "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";

   private static final int topkey = RegistryKey.KEY_LOCAL_MACHINE;

   public WinRegPath() {
       // check registry support
       if (!SystemToolkit.doesSupportRegistry()) {
           System.out.println("No registry support.");
       }
   }

   public String getPath(String executable) {
       if (SystemToolkit.doesSupportRegistry() == false) {
           return null;
       }
       try {
           RegistryManager regManager = SystemToolkit.getRegistryManager();
           RegistryKey key = regManager.openRegistryKey(topkey, subkey + executable);

           if (key.exists()) {
               // enumerate properties
               Enumeration e = key.properties();
               while (e.hasMoreElements()) {
                   String name = (String)e.nextElement();
                   String val = key.getStringProperty(name);
                   //System.out.println("'" + name + "'" + " = " + val );
                   if (name.length() == 0) {
                       return val;
                   }
               }
           }
       } catch (RegistryException re) {
           re.printStackTrace();
       }
       return null;
   }

   public static void main(String[] args) throws Exception {
       WinRegPath regpath = new WinRegPath();
       String path = regpath.getPath(args[0]);
       if (path != null) {
           System.out.println("exec'ing: " + path);
           Runtime.getRuntime().exec(path);
       }
   }
}
---------- end ----------

As for printing from MS Word, winword does not have a command line switch for printing. I've achieved limited success using the command line switch to invoke standard macros. For example, using the above code:
    java WinRegPath winword.exe junk.doc /mFilePrintDefault

will print the document but leaves the word window running. The FileExit macro will exit word but you'll get a dialog because it is trying to print in the background. So there may be a solution with macros but I don't know it. What I do understand will work is DDE. I haven't tried it, but the technique (using another freeware package since you can't 'DDE' from Java natively) is demonstrated at http://www.home.fh-karlsruhe.de/~sehe0012/studies/neusoft/week22.html.

Best regards,
Jim Cakalic
Hello sir,
i am using MS NT OS.
I want to print a Doc file present in c:/MyProject folder.
and i have downloaded the jar file for the specified URL.
now can you tell me how to send the parameter to the file you have given.

Hello expert i am not getting the subkey in you code. can you please elaborate on this.
OK. I assume you already did the following:

- download jst12.zip at the JObjects URL in my previous post
- unzip the archive in a folder of your choosing which I'll refer to as JST_HOME
- added JST_HOME/lib/jst.jar to your CLASSPATH
- cut the code from my post and paste it into a file named WinRegPath.java
- compile WinRegPath.java

Use the Start/Run dialog to run regedit. Navigate to the subkey identified in the code: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths. At this location you will find all the registered 32-bit applications. Verify that the application you are trying to invoke is registered. The Acrobat Reader will be registered as AcroRd32.exe. Microsoft Word will be registered as Winword.exe. If you don't find these keys then either you don't have the applications installed or they are not installed correctly.

The registry keys are not case sensitive. But otherwise the values must match exactly. This means you cannot leave off the extension as you might when invoking an application from the DOS command line. So when attempting to find the path for Word, you cannot simply use "winword" as the first argument to WinRegPath. You _must_ use "winword.exe". The same is true for Acrobat: you _must_ use "acrord32.exe".

Best regards,
Jim Cakalic
Hello expert thanks for the solution but it is a partial.
i am able to print the pdf file from the command prompt but, if I give the same from the java file it is showing the error as "There was an error opening this document. the file does not exist".

i am using the following command in my java file.


Runtime.getRuntime().exec("C:\\Program Files\\Adobe\\Acrobat 5.0\\Reader\\AcroRd32.exe /p naren.pdf");


i am using
"C:\\Program Files\\Adobe\\Acrobat 5.0\\Reader\\AcroRd32.exe /p naren.pdf"

in command prompt.
can you tell me what is the problem with me.
please it is very urgent.
Sounds like the working directory of AcroRd32 is not where naren.pdf is located. Try specifying the full path name to the pdf file and see if that solves your problem.

Jim
Thank you Jim.
I got the solution for my problem
can you do me one more favour. what is the command in command prompt to print a word file.
tell me the correct format to print naren.doc which in c:\naren.doc folder.

Thank you very much jim.
Ok Jim i got the solution for all the issues.
Thankyou very much jim
So you can now print both pdf and word files from the command prompt? Great! Glad to help.
Jim
Jim I have one more problem.
How to see the priveiw of the document what we are going to print.
is there any method to achieve this.Please send me the same.
I don't know about PDF. Word has a macro FilePrintPreview that can be called from the command line to present the normal Word print preview window. But I'm not sure whether that is what you want.

Jim
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.

PLEASE DO NOT AWARD THE POINTS TO ME.
____________________________________________

----------------------->>>>>>>>>>>>>>    Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.  Please let me know if no response the next 3 days.

NEW topic areas added Feb 8, 2002.  https://www.experts-exchange.com/jsp/zonesAll.jsp

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange
These are all your remaining open questions as of today, Narendranath

Please update and finalize them today.  If you need help to split points, or other special handling needs, just comment here with details and I'll respond when I can.

EXPERTS --- if Asker chooses not to respond, please provide me with feedback as to the fair outcome to close these.

https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20154148
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20152011
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20151333
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20149703
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20145043
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20144972
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20144884
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20144268

Thank you,
Moondancer
Community Support Moderator @ Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial