The most powerful Java Word file printing scheme in history

Source: Internet
Author: User

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.

/mFileExitSays 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.