Solved

How to print a word document in java?

Posted on 2001-07-12
20
781 Views
Last Modified: 2011-11-02
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?
0
Comment
Question by:Narendranath
  • 10
  • 6
  • 2
  • +1
20 Comments
 

Author Comment

by:Narendranath
ID: 6276562
Please send me the solution with example.
0
 

Expert Comment

by:anhphuongnn
ID: 6276637
hi,

You can read this docment

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


Best Regard
PhuongNguyen
0
 

Expert Comment

by:anhphuongnn
ID: 6276674
hi,

You can read this docment

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


Best Regard
PhuongNguyen
0
 

Author Comment

by:Narendranath
ID: 6276776
http://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.
0
 

Author Comment

by:Narendranath
ID: 6277966
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.
0
 

Author Comment

by:Narendranath
ID: 6279580
Hello expert
I am waiting for your reply please send me the solution for my problem.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6286739
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
0
 

Author Comment

by:Narendranath
ID: 6288589
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.

0
 

Author Comment

by:Narendranath
ID: 6290042
Hello expert i am not getting the subkey in you code. can you please elaborate on this.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6290390
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Narendranath
ID: 6290472
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.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6290504
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
0
 

Author Comment

by:Narendranath
ID: 6292206
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.
0
 

Author Comment

by:Narendranath
ID: 6292529
Ok Jim i got the solution for all the issues.
Thankyou very much jim
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6294743
So you can now print both pdf and word files from the command prompt? Great! Glad to help.
Jim
0
 

Author Comment

by:Narendranath
ID: 6296821
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.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6298428
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
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6791788
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.  http://www.experts-exchange.com/jsp/zonesAll.jsp

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6795363
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.

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

Thank you,
Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6795386
Hi, Moondancer. On this one, I think that I should get the points.

Thanks,
Jim
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now