如何解决如何将docx转换为xhtml
我正试图找到一种将docx文件转换为XHTML的解决方案。
我找到了xdocreport,看起来不错,但是我遇到了一些问题。 (而且我是xdocreport的新手)
根据他们在github here和here上的文档:我应该能够使用以下代码进行转换:
String source = args[0];
String dest = args[1];
// 1) Create options DOCX to XHTML to select well converter form the registry
Options options = Options.getFrom(DocumentKind.DOCX).to(ConverterTypeTo.XHTML);
// 2) Get the converter from the registry
IConverter converter = ConverterRegistry.getRegistry().getConverter(options);
// 3) Convert DOCX to (x)html
try {
InputStream in = new FileInputStream(new File(source));
OutputStream out = new FileOutputStream(new File(dest));
converter.convert(in,out,options);
} catch (XDocConverterException | FileNotFoundException e) {
e.printStackTrace();
}
我正在使用这些依赖项(尝试了不同的版本,例如2.0.2、2.0.0、1.0.6):
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>2.0.2</version>
</dependency>
我的问题:
- 图像丢失
- 缺少背景色(所有页面都有背景色,不是白色,我也必须将其转换)
我该如何处理这些问题? (或者如何使用带有格式/编号/图像的Docx4j将docx转换为xhtml?)
解决方法
要使用*.docx
和XHTML
的{{1}}作为源将XDocReport
转换为apache poi
,则需要XWPFDocument
。这些选项能够使XHTMLOptions
设置从ImageManager
提取图像的路径。然后需要XWPFDocument
进行转换。
完整示例:
XHTMLConverter
这可以正确处理图像。
但是import java.io.*;
//needed jars: xdocreport-2.0.2.jar,import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import fr.opensagres.poi.xwpf.converter.core.ImageManager;
//needed jars: all apache poi dependencies
import org.apache.poi.xwpf.usermodel.*;
public class DOCXToXHTMLXDocReport {
public static void main(String[] args) throws Exception {
String docPath = "./WordDocument.docx";
String root = "./";
String htmlPath = root + "WordDocument.html";
XWPFDocument document = new XWPFDocument(new FileInputStream(docPath));
XHTMLOptions options = XHTMLOptions.create().setImageManager(new ImageManager(new File(root),"images"));
FileOutputStream out = new FileOutputStream(htmlPath);
XHTMLConverter.getInstance().convert(document,out,options);
out.close();
document.close();
}
}
到目前为止无法正确处理XDocReport
的页面背景颜色。它提取并处理段落背景颜色,但不提取页面背景颜色。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。