客户要求Word必须支持高级功能(如支持打印份数、单双面打印),而我们又不能使用第三方插件,开源的包如poi、jacob对word打印的支持实在有些无奈,木有办法,搜索、查阅了无数资料,一直没有好的解决方案。
某日深夜,小手一抖,搜到一终极方案,特此分享:
This is probably not 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 be 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
says to run the program that follows minimized. You must do this or Word will stay open after the file is opened.
/min
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 files the user has open.
/f
says
to open a copy of the file to prevent modification.
/mFilePrint
tells
Word to diplay its print dialog so the user can choose which printer they want to use and how many copies, etc.
/mFileExit
says
to close as soon as everything else is done. This will not work unless Word is minimized.
原文地址: