0

The following code is from the sample code from docx4j.

public void convertDocxToHtml2() throws FileNotFoundException, Docx4JException{
    String inputfilepath = System.getProperty("user.dir") + myPath;
    boolean save = true;

    // Document loading (required)
    WordprocessingMLPackage wordMLPackage;
    System.out.println("Loading file from " + inputfilepath);
    wordMLPackage = Docx4J.load(new java.io.File(inputfilepath));

    // HTML exporter setup (required)
    // .. the HTMLSettings object
    HTMLSettings htmlSettings = Docx4J.createHTMLSettings();

    htmlSettings.setImageDirPath(inputfilepath + "_files");
    htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath.lastIndexOf("/")+1) + "_files");
    htmlSettings.setWmlPackage(wordMLPackage);        

    String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img,  ol, ul, li, table, caption, tbody, tfoot, thead, tr, th, td " +
            "{ margin: 0; padding: 0; border: 0;}" +
            "body {line-height: 1;} ";
    htmlSettings.setUserCSS(userCSS);        
    SdtWriter.registerTagHandler("HTML_ELEMENT", new SdtToListSdtTagHandler());

    // output to an OutputStream.       
    OutputStream os; 
    if (save) {
        os = new FileOutputStream(inputfilepath + ".html");
    } else {
        os = new ByteArrayOutputStream();
    }

    // If you want XHTML output
    Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
    Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);


    if (save) {
        System.out.println("Saved: " + inputfilepath + ".html ");
    } else {
        System.out.println( ((ByteArrayOutputStream)os).toString() );
    }

    // Clean up, so any ObfuscatedFontPart temp files can be deleted 
    if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
        wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
    }       
    // This would also do it, via finalize() methods
    htmlSettings = null;
    wordMLPackage = null;
}

And the following is a section of the output:

            <ol>
                <li class="ListParagraph Normal DocDefaults " style="position: relative; margin-left: 0.5in;text-indent: -0.25in;">
                    <span style="font-family: 'Calibri';">TestList1</span>
                </li>

                <li class="ListParagraph Normal DocDefaults " style="position: relative; margin-left: 0.5in;text-indent: -0.25in;">
                    <span style="font-family: 'Calibri';">TestList2</span>
                </li>
            </ol>

How do i override the CSS that is being autogenerated? I want to remove the text-indent and add display: list-item into the inline css.

  • Not an answer, but may be of interest: http://www.docx4java.org/forums/docx-java-f6/can-tohtml-emit-nested-list-tags-t2329.html – JasonPlutext Mar 14 '16 at 11:51
  • @JasonPlutext Hi Jason. It seems that this person managed to solve the problem. However i could not replicate his results. Any suggestions? http://www.docx4java.org/forums/xhtml-import-f28/converting-numbering-list-to-html-t2284.html – user2533611 Mar 15 '16 at 03:33
  • I haven't looked at this in any detail yet, but what does your input docx xml for one of the paragraphs look like? Does it use list numbering (numPr)? – JasonPlutext Mar 15 '16 at 06:54
  • @JasonPlutext My docx contains only a simple bulleted list with two rows. – user2533611 Mar 15 '16 at 10:47

0 Answers0