Customer requirements Word must support advanced features (such as support for printing copies, single-sided printing), and we can not use third-party plug-ins, open-source packages such as POI, Jacob's support for word printing is a little helpless, Wood has a way, search, access to countless information, has not been a good solution.
One night, small hands shook, search for the ultimate solution, hereby share :
This is probably isn't the most efficient method, but it works if you have MS Word. You can use this command to get Word print the file:
start /min winword <filename> /q /n /f /mFilePrint /mFileExit
Replace<filename>
With the filename. It must is enclosed in double-quotation marks if it contains spaces. (e.g.file.rtf
,"A File.docx"
)
Here is a Java method and C + + function that takes the filename as an argument and prints the file:
Java
public void printWordFile(String filename){ System.getRuntime().exec("start /min winword \"" + filename + "\" /q /n /f /mFilePrint /mFileExit");}
C++
//Be sure to #include <string.h> void wordprint(char* filename){ char* command = new char[64 + strlen(filename)]; strcpy(command, "start /min winword \""); strcat(command, filename); strcat(command, "\" /q /n /f /mFilePrint /mFileExit"); system(command); delete command;}
Explanation of switches used
start/min
says to run the program that follows minimized. You must does this or Word would stay open after the file is opened.
winword
tells the start
program to run Microsoft Word.
/q
tells Word not to display the splash screen.
/n
says to open a new instance of Word so we don ' t interfere with other FIL Es the user has open.
/f
says to open a copy of the file to prevent modification.
/mfileprint
tells Word to diplay It print dialog so the user can choose WH Ich printer they want to use and how many copies, etc.
/mFileExit
Says to close as soon as everything else are done. This won't work unless Word is minimized.
Original address:
Http://stackoverflow.com/questions/2446721/how-to-get-print-out-of-a-ms-word-file-from-java-application
The most powerful Java Word file printing scheme in history