Writing HTML content in MS WORD using JAVA?

I am developing exam software in which I used subscript and superscript. so I have to store questions in HTML in a database, Now I want to write these questions with HTML tags for a word, I tried using the Apache POI Library, here is an example text:

  <html>
  <head> </head>
  <body><font face="Shruti"> MY QUESTION </font>
  </body>
  </html> 

but when I try to write text in a Word document using apache poi, it shows HTML tags

0
source share
2 answers

You can add your HTML as AltChunk and convert Word to docx source content the first time you open the file.

docx Java, docx4j-ImportXHTML

: .

+1

java api docx4j-ImportXHTML, :

public static void xhtmlToDocx(String xhtml, String destinationPath, String fileName)
    {
        File dir = new File (destinationPath);
        File actualFile = new File (dir, fileName);

        WordprocessingMLPackage wordMLPackage = null;
        try
        {
            wordMLPackage = WordprocessingMLPackage.createPackage();
        }
        catch (InvalidFormatException e)
        {
            e.printStackTrace();
        }


        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);

        OutputStream fos = null;
        try
        {
            fos = new ByteArrayOutputStream();

            System.out.println(XmlUtils.marshaltoString(wordMLPackage
                    .getMainDocumentPart().getJaxbElement(), true, true));

                        HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
            htmlSettings.setWmlPackage(wordMLPackage);
  Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML",
                    true);
            Docx4J.toHTML(htmlSettings, fos, Docx4J.FLAG_EXPORT_PREFER_XSL);
            wordMLPackage.save(actualFile); 
        }
        catch (Docx4JException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

docx4j-ImportXHTML, (3.3.1 - , . , ).

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>3.3.1</version>
</dependency>
0

All Articles