ms-worddocx4j

Docx4j convert HTML with two or more CSS classes


I see that in my HTML to Docx code, I can only use ONE CSS class per p tag? How can I have attributes of two or more CSS applied.

Sample HTML code

    <html>
    <head>
        <style>
            .txcenter {
                text-align: center;
            }
    
            .txred {
                color: red;
            }
        </style>
    </head>
    <body>
        <p class="txred txcenter">My Text</p>
    </body>
    </html>

Then only the txred class applied. The word "My Text" turn red in Word document, but not align center. And if I define class txcenter before txred, "My Text" is aligned center in Word but the color stays black.

This is my Java code, variable htmlContent is the HTML string:

WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();

wordPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, 
                                                      htmlContent.getBytes());
wordPackage.save(new File(MY_FILE_PATH));

Solution

  • If you merely add an altChunk like that, you are relying on Word to convert it to docx content on opening (which it apparently does badly in this case).

    docx4j-ImportXHTML can convert it for you, if you add that to your classpath and invoke wordPackage.getMainDocumentPart().convertAltChunks()