以下代码来自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;
}
以下是输出的一部分:
<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>
如何覆盖自动生成的CSS?我想删除text-indent并将display:list-item添加到内联css中。