Viewing .doc file with java applet

前端 未结 4 841
予麋鹿
予麋鹿 2021-01-13 09:50

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 usi

相关标签:
4条回答
  • 2021-01-13 10:17

    I'd suggest looking at the openoffice source code and implement that. It's supposed to be written in java.

    0 讨论(0)
  • 2021-01-13 10:21

    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.

    0 讨论(0)
  • 2021-01-13 10:30

    docx4all is a Swing-based applet which does Word 2007 XML (ie not Word 2003 XML), which we wrote several years ago.

    Get it from svn.

    That's a possible approach for editing. If all you want is a viewer, which not convert to HTML or PDF? You can use docx4j for that. (Disclosure: "my" project).

    0 讨论(0)
  • 2021-01-13 10:31

    You might have a look at the Apache POI - Java API to Handle Microsoft Word Files which is able to read all kinds of word documents (OLE2 and OOXML formats, .doc and .docx extensions respectively).

    Reading a doc file can be easy as:

    import java.io.*;
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.extractor.WordExtractor;
    
    public class ReadDocFile {
    public static void main(String[] args) {
    File file = null;
    WordExtractor extractor = null ;
    try {
    
    file = new File("c:\\New.doc");
    FileInputStream fis=new FileInputStream(file.getAbsolutePath());
    HWPFDocument document=new HWPFDocument(fis);
    extractor = new WordExtractor(document);
    String [] fileData = extractor.getParagraphText();
    for(int i=0;i<fileData.length;i++){
    if(fileData[i] != null)
    System.out.println(fileData[i]);
    }
    }
    catch(Exception exep){}
    }
    }
    

    You can find more at: HWPF Quick-Guide (specifically HWPF unit tests)

    Note that, according to the POI site:

    HWPF is still in early development.

    0 讨论(0)
提交回复
热议问题