docx4j转换html-> docx-> html

时间:2014-08-25 16:42:45

标签: java xhtml ckeditor docx docx4j

我正在使用docx4j处理我的第一个项目...我的目标是将xhtml从webapp(ckeditor创建的html)导出到docx,在Word中编辑它,然后将其导回到ckeditor wysiwyg。

(*来自http://www.docx4java.org/forums/xhtml-import-f28/html-docx-html-inserts-a-lot-of-space-t1966.html#p6791?sid=78b64a02482926c4dbdbafbf50d0a914的交叉路口 会在回答时更新)

我创建了一个包含以下内容的html测试文档:

<html><ul><li>TEST LINE 1</li><li>TEST LINE 2</li></ul></html>

我的代码然后从这个html创建一个docx,如下所示:         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage                 .createPackage();

    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();

    XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    xHTMLImporter.setHyperlinkStyle("Hyperlink");

    wordMLPackage.getMainDocumentPart().getContent()
            .addAll(xHTMLImporter.convert(new File("test.html"), null));

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

    wordMLPackage.save(new java.io.File("test.docx"));

我的代码然后尝试将docx BACK转换为html,如下所示:         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage                 .createPackage();

    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();

    XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    xHTMLImporter.setHyperlinkStyle("Hyperlink");

    WordprocessingMLPackage docx = WordprocessingMLPackage.load(new File("test.docx"));
    AbstractHtmlExporter exporter = new HtmlExporterNG2();
    OutputStream os = new java.io.FileOutputStream("test.html");
    HTMLSettings htmlSettings = new HTMLSettings();
    javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(
            os);
    exporter.html(docx, result, htmlSettings);

返回的html是:

<?xml version="1.0" encoding="UTF-8"?><html xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
<!--/*paged media */ div.header {display: none }div.footer {display: none } /*@media print { */@page { size: A4; margin: 10%; @top-center {content: element(header) } @bottom-center {content: element(footer) } }/*element styles*/ .del  {text-decoration:line-through;color:red;} .ins {text-decoration:none;background:#c0ffc0;padding:1px;}
 /* TABLE STYLES */ 

 /* PARAGRAPH STYLES */ 
.DocDefaults {display:block;margin-bottom: 4mm;line-height: 115%;font-size: 11.0pt;}
.Normal {display:block;}

 /* CHARACTER STYLES */ span.DefaultParagraphFont {display:inline;}
-->
</style>
<script type="text/javascript">
<!--function toggleDiv(divid){if(document.getElementById(divid).style.display == 'none'){document.getElementById(divid).style.display = 'block';}else{document.getElementById(divid).style.display = 'none';}}
--></script>
</head>
<body>

  <!-- userBodyTop goes here -->




<div class="document">


<p class="Normal DocDefaults " style="text-align: left;position: relative; margin-left: 17mm;text-indent: -0.25in;margin-bottom: 0in;">&bull;  <span class="DefaultParagraphFont " style="font-weight: normal;color: #000000;font-style: normal;font-size: 11.0pt;">TEST LINE 1</span>
</p>


<p class="Normal DocDefaults " style="text-align: left;position: relative; margin-left: 17mm;text-indent: -0.25in;margin-bottom: 0in;">&bull;  <span class="DefaultParagraphFont " style="font-weight: normal;color: #000000;font-style: normal;font-size: 11.0pt;">TEST LINE 2</span>
</p>
</div>







  <!-- userBodyTail goes here -->


</body>
</html>

现在每行之后会创建很多额外空间。不知道为什么会发生这种情况,转换似乎会增加很多额外的空格/回车。

2 个答案:

答案 0 :(得分:1)

从您的问题中不清楚您是在担心(X)HTML源文档中的空白,还是在您呈现的页面中(可能是在CKEditor中)。如果是后者,那么浏览器和CK版本可能是相关的。

空白可能是也可能不是 重要 ;尝试谷歌搜索&x; xhtml重要的空白&#39;更多。

通过后台方式,取决于docx4j属性docx4j.Convert.Out.HTML.OutputMethodXML,docx4j将使用

<xsl:output method="html" encoding="utf-8" omit-xml-declaration="no" indent="no" 
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="no" indent="no" 
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

注意@method值的不同。如果您想要不同的东西,可以分别修改docx2html.xsl或docx2xhtml.xsl。

答案 1 :(得分:0)

有没有办法将wordMLPackage转换为html而不需要额外的东西,如:

<?xml version="1.0" encoding="UTF-8"?>

和css?

它可以像原始的html和内联css一样简单吗<html><body><div style="...."></div></body></html>吗?