Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing .doc file with java applet

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.

like image 276
kant Avatar asked Aug 27 '12 13:08

kant


People also ask

Is Java applet still supported?

No, there isn't. Java Applets are dead and there is no viable way to run them for the vast majority of users on the public Internet. If you are a software developer, you should abandon applets and start learning a modern full-stack framework. There are many to choose from.

How do I open a DOC file in my browser?

From the document library select- Settings > Document Library Settings > General Settings > Advanced Settings > Browser-enabled Documents > Select the "Display as a Web page" option.

What is appletviewer used for?

AppletViewer is a standalone command-line program from Sun to run Java applets. Appletviewer is generally used by developers for testing their applets before deploying them to a website. As a Java developer, it is a preferred option for running Java applets that do not involve the use of a web browser.

What is applet in MS Word?

Applet is most often associated with Java. In general, when referring to a small program, use the name of the program or the most appropriate term, such as item, app, program, add-in, or applet. Example. A Java applet in an HTML document supports animation, music, and page updates.


1 Answers

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.

What you should do is the following:

(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();

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.

like image 151
Tyler Durden Avatar answered Oct 13 '22 23:10

Tyler Durden