如何在不打开的情况下用Java打印Word文档?

时间:2022-10-30 23:41:18

I have a J2EE based web application, in which on clicking a button I need to create a word file from Java. I want to be able to sent the printing command to this file, so that the file is being printed without the user having to open the document and do it manually.

我有一个基于J2EE的Web应用程序,在单击按钮时我需要从Java创建一个word文件。我希望能够将打印命令发送到此文件,以便打印文件而无需用户打开文档并手动执行。

Could anyone please tell me if this is possible and if so how to proceed??

任何人都可以告诉我,如果这是可能的,如果是这样,怎么办?

Thanks in advance.

提前致谢。

5 个解决方案

#1


1  

It is easy to generate the file. Take a look at Apache POI, a Java API To Access Microsoft Format Files. The site is plenty of examples.

生成文件很容易。看一下Apache POI,一个访问Microsoft格式文件的Java API。该网站有很多例子。

About printing it, I don't really know if that's possible. I think you always need to open it in the client side in order to print it.

关于打印它,我真的不知道这是否可能。我认为你总是需要在客户端打开它才能打印出来。

#2


1  

You have to create an ActiveX to load the doc content and fire the print command. There is no other way to open a file in the client computer and fire comands.

您必须创建一个ActiveX来加载doc内容并触发print命令。没有其他方法可以在客户端计算机中打开文件并触发命令。

EDIT: If you can use HTML, you can just do

编辑:如果你可以使用HTML,你可以这样做

<script>
function load() {
window.print();
window.close();
}
</script>

and

<body onLoad="load()" ...>

on a popup window to open the document and print it. And then close the popup.

在弹出窗口中打开文档并打印它。然后关闭弹出窗口。

#3


1  

You can use simple AutoIt code of just 2 lines. And get your work done execute that code file in your program.

您可以使用仅2行的简单AutoIt代码。并完成您的工作在您的程序中执行该代码文件。

http://www.autoitscript.com/site/autoit/

Like this code:

喜欢这段代码:

$sRTFfilename = "C:\t\t.rtf" ;Change this path to one of your own 
ShellExecute('"' & $sRTFfilename & '"', "", @ScriptDir, "print", @SW_HIDE)

;-)

#4


0  

As you are specifically mentioning Java as your environment you might want to take OpenOffice or StarOffice into account - they have an API that's a lot easier to talk to from Java than ActiveX or remote-controlling Word.

正如您特别提到Java作为您的环境一样,您可能需要将OpenOffice或StarOffice考虑在内 - 他们使用的API比使用Java或远程控制Word更容易与Java交谈。

It's no more heavyweight than Word is. References and docs should be easy to find.

它不比Word更重要。应该很容易找到参考文献和文​​档。

I've worked in a company where we used Remote-controlled-Word a lot and finally switched to StarOffice. This wasn't for printing but for document conversion (e.g. from Word to HTML), but should be sufficiently similar.

我曾经在一家公司工作,我们经常使用Remote-controlled-Word,最后切换到StarOffice。这不是用于打印而是用于文档转换(例如从Word到HTML),但应该足够相似。

Of course I'm talking "old versions of Word" but we usually had the problem of Word locking up with some arbitrary dialog requesting to confirm whatever Word has found - an operation that our server (running in the background with no Desktop contact) obviously was not able to. This went a lot better after switching to StarOffice.

当然我说的是“Word的旧版本”但我们通常遇到Word锁定的问题,请求确认Word找到的任意对话 - 我们的服务器(在后台运行,没有桌面联系)的操作显然无法做到。切换到StarOffice后,这样做了很多。

#5


0  

Would it be possible to render the document in HTML instead? If you could do that then you could allow the users to print via the browser fairly simply. You might also have an easier time with PDF's, at the very least it would be more accessible across different platforms.

是否可以用HTML呈现文档?如果你可以这样做,那么你可以让用户通过浏览器相当简单地打印。您可能还可以更轻松地使用PDF,至少可以在不同平台*问它们。

If you're forced to use MS Word then you're going to be very limited in what you can do. As Leonel mentioned, I think ActiveX is going to be your only choice and even then the document would have to be opened, you just might be able to launch Word from the browser automatically. You might even be able to embed an instance of Word into IE via ActiveX, but I'm not 100% sure about that.

如果您*使用MS Word,那么您将能够做的事情非常有限。正如Leonel所提到的,我认为ActiveX将是您唯一的选择,即使这样,文档也必须打开,您可能只能自动从浏览器启动Word。你甚至可以通过ActiveX将一个Word实例嵌入到IE中,但我并不是百分之百确定。

#1


1  

It is easy to generate the file. Take a look at Apache POI, a Java API To Access Microsoft Format Files. The site is plenty of examples.

生成文件很容易。看一下Apache POI,一个访问Microsoft格式文件的Java API。该网站有很多例子。

About printing it, I don't really know if that's possible. I think you always need to open it in the client side in order to print it.

关于打印它,我真的不知道这是否可能。我认为你总是需要在客户端打开它才能打印出来。

#2


1  

You have to create an ActiveX to load the doc content and fire the print command. There is no other way to open a file in the client computer and fire comands.

您必须创建一个ActiveX来加载doc内容并触发print命令。没有其他方法可以在客户端计算机中打开文件并触发命令。

EDIT: If you can use HTML, you can just do

编辑:如果你可以使用HTML,你可以这样做

<script>
function load() {
window.print();
window.close();
}
</script>

and

<body onLoad="load()" ...>

on a popup window to open the document and print it. And then close the popup.

在弹出窗口中打开文档并打印它。然后关闭弹出窗口。

#3


1  

You can use simple AutoIt code of just 2 lines. And get your work done execute that code file in your program.

您可以使用仅2行的简单AutoIt代码。并完成您的工作在您的程序中执行该代码文件。

http://www.autoitscript.com/site/autoit/

Like this code:

喜欢这段代码:

$sRTFfilename = "C:\t\t.rtf" ;Change this path to one of your own 
ShellExecute('"' & $sRTFfilename & '"', "", @ScriptDir, "print", @SW_HIDE)

;-)

#4


0  

As you are specifically mentioning Java as your environment you might want to take OpenOffice or StarOffice into account - they have an API that's a lot easier to talk to from Java than ActiveX or remote-controlling Word.

正如您特别提到Java作为您的环境一样,您可能需要将OpenOffice或StarOffice考虑在内 - 他们使用的API比使用Java或远程控制Word更容易与Java交谈。

It's no more heavyweight than Word is. References and docs should be easy to find.

它不比Word更重要。应该很容易找到参考文献和文​​档。

I've worked in a company where we used Remote-controlled-Word a lot and finally switched to StarOffice. This wasn't for printing but for document conversion (e.g. from Word to HTML), but should be sufficiently similar.

我曾经在一家公司工作,我们经常使用Remote-controlled-Word,最后切换到StarOffice。这不是用于打印而是用于文档转换(例如从Word到HTML),但应该足够相似。

Of course I'm talking "old versions of Word" but we usually had the problem of Word locking up with some arbitrary dialog requesting to confirm whatever Word has found - an operation that our server (running in the background with no Desktop contact) obviously was not able to. This went a lot better after switching to StarOffice.

当然我说的是“Word的旧版本”但我们通常遇到Word锁定的问题,请求确认Word找到的任意对话 - 我们的服务器(在后台运行,没有桌面联系)的操作显然无法做到。切换到StarOffice后,这样做了很多。

#5


0  

Would it be possible to render the document in HTML instead? If you could do that then you could allow the users to print via the browser fairly simply. You might also have an easier time with PDF's, at the very least it would be more accessible across different platforms.

是否可以用HTML呈现文档?如果你可以这样做,那么你可以让用户通过浏览器相当简单地打印。您可能还可以更轻松地使用PDF,至少可以在不同平台*问它们。

If you're forced to use MS Word then you're going to be very limited in what you can do. As Leonel mentioned, I think ActiveX is going to be your only choice and even then the document would have to be opened, you just might be able to launch Word from the browser automatically. You might even be able to embed an instance of Word into IE via ActiveX, but I'm not 100% sure about that.

如果您*使用MS Word,那么您将能够做的事情非常有限。正如Leonel所提到的,我认为ActiveX将是您唯一的选择,即使这样,文档也必须打开,您可能只能自动从浏览器启动Word。你甚至可以通过ActiveX将一个Word实例嵌入到IE中,但我并不是百分之百确定。