Search code examples
javahtmlswingjinternalframedocx4j

DOCX4J: Viewing HTML exported Document file in JEditorPane


I was trying to view an xHTML file generated by DOCX4J. I was able to successfully generate the mentioned file, all the formatting of text appears correctly in the browser. However, I was trying to view it on a JEditorPane, and it only displays the texts inside the HTML file but not the formatting it was As if the entire page used default text font. Here is my code for the JInternalFrame

package com.docx.ui.tools;

import java.awt.Desktop;
import java.awt.Dimension;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;

public class Preview extends JInternalFrame 
{
    private static JScrollPane scrollPane;
    private static JEditorPane htmlPane;

    public Preview()
    {
        htmlPane = new JEditorPane();
        htmlPane.setEditable(false);
        htmlPane.setContentType("text/html");
        scrollPane = new JScrollPane(htmlPane);
        scrollPane.setAutoscrolls(true);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(250, 145));
        setSize(800, 600);
        scrollPane.setMinimumSize(getSize());
        setVisible(true);
        setResizable(true);
        setMaximizable(true);
        setTitle("Document Preview");
        getContentPane().add(scrollPane);
        loadfile();
    }

    public void loadfile()
    {
        Desktop d;
        URL url = null;
        try {
            url = new URL("File:F:\\out.html");
            //          url = new URL("https://www.youtube.com/");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            htmlPane.setPage(url);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I tried changing the URL into a valid web address and the page is viewed correctly. I tried doing the same procedure in a JFrame and same results occurred. I was wondering if I was doing something wrong in the procedure or the problem lies in the HTML file generated by the DOCX4J library.

I've included the HTML file and DOCX file just in case it is needed HTML http://www.mediafire.com/?z3t8ksv3c2air27

DOCX http://www.mediafire.com/view/?p5yr4v6isai3452


Solution

  • Actually JEditorPane (HTMLEditorKit in fact) supports html 3.2 version only.

    You can try to display the docx file directly using the kit http://java-sl.com/docx_editor_kit.html It's not perfect though so there could be also some problems.