码迷,mamicode.com
首页 > 编程语言 > 详细

史上最强大Java Word文件打印方案

时间:2014-11-16 16:05:14      阅读:627      评论:0      收藏:0      [点我收藏+]

标签:java word 打印 解决方案

    客户要求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 /min says to run the program that follows minimized. You must do this or Word will 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 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.


原文地址: 

史上最强大Java Word文件打印方案

标签:java word 打印 解决方案

原文地址:http://blog.csdn.net/ylz2007/article/details/41172653

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!