使用 java 小程序查看 .doc 文件 [英] Viewing .doc file with java applet

查看:25
本文介绍了使用 java 小程序查看 .doc 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序.我在服务器端生成了 xml 格式的 MS Word 文档(Word 2003 XML 文档).我需要使用某种查看器向客户端的用户显示此文档.所以,问题是:我可以使用哪些库来解决这个问题?我需要一个 API 来在客户端查看 word 文档使用 java.

I have a web application. I've generated MS Word document in xml format (Word 2003 XML Document) on server side. I need to show this document to a user on a client side using some kind of viewer. So, question is: what libraries I can use to solve this problem? I need an API to view word document on client side using java.

推荐答案

您无法使用 Java(或任何其他与此相关的简单技术)在网页中可靠地显示 Word 文档.有几个商业库可以呈现 Word,但您不会发现这些是简单、廉价或可靠的解决方案.

You cannot reliably display a Word document in a web page using Java (or any other simple technology for that matter). There are several commercial libraries out there to render Word, but you will not find these to be easy, cheap or reliable solutions.

你应该做的是:

(1) 使用.NET程序在服务器上打开Word引擎(2) 使用Word引擎将文档转换为Rich Text(3) 使用 RTF Swing 小部件显示富文本,或转换为 HTML:

(1) Open the Word engine on the server using a .NET program (2) Convert the document to Rich Text using the Word engine (3) Display the rich text either using the RTF Swing widget, or convert to HTML:

String rtf = [your document rich text];
BufferedReader input = new BufferedReader(new StringReader(rtf));

RTFEditorKit rtfKit = new RTFEditorKit();
StyledDocument doc = (StyledDocument) rtfKit.createDefaultDocument();
rtfEdtrKt.read( input, doc, 0 );
input.close();

HTMLEditorKit htmlKit = new HTMLEditorKit();       
StringWriter output = new StringWriter();
htmlKit.write( output, doc, 0, doc.getLength());

String html = output.toString();

这种方法的主要风险是 Word 引擎要么崩溃要么内存泄漏.出于这个原因,您必须有一种机制来定期重新启动它并对其进行测试以确保它可以正常工作并且不会占用内存.

The main risk in this approach is that the Word engine will either crash or have a memory leak. For this reason you have to have a mechanism for restarting it periodically and testing it to make sure it is functional and not hogging memory.

这篇关于使用 java 小程序查看 .doc 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆