Java 如何将docx转换为xhtml
我正试图找到一种将docx文件转换为XHTML的解决方案 我找到了xdocreport,它看起来不错,但我有一些问题。(我是xdocreport的新手) 根据他们在github上的文档,我应该能够使用以下代码进行转换:Java 如何将docx转换为xhtml,java,apache-poi,xhtml,docx4j,xdocreport,Java,Apache Poi,Xhtml,Docx4j,Xdocreport,我正试图找到一种将docx文件转换为XHTML的解决方案 我找到了xdocreport,它看起来不错,但我有一些问题。(我是xdocreport的新手) 根据他们在github上的文档,我应该能够使用以下代码进行转换: String source = args[0]; String dest = args[1]; // 1) Create options DOCX to XHTML to select well converter form the registry
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):
fr.opensagres.xdocreport
fr.opensagres.xdocreport.document.docx
2.0.2
fr.opensagres.xdocreport
fr.opensagres.xdocreport.template.freemarker
2.0.2
fr.opensagres.xdocreport
fr.opensagres.xdocreport.converter.docx.xwpf
2.0.2
我的问题:
- 图像丢失了
- 缺少背景色(所有页面都有背景色,不是白色,我也必须转换)
(或者如何使用带有格式/编号/图像的Docx4j将docx转换为xhtml?将
*.docx
转换为xhtml
使用XDocReport
和apachepoi
的XWPFDocument
作为您需要的xhtml>源代码。这些选项可以让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
还不能正确处理XWPFDocument
的页面背景色。它提取并处理段落背景色,但不处理页面背景色。谢谢,工作正常:)我有两个问题,如果页脚中有文本/图像怎么办?我怎么处理这些?有没有一种方法可以将图像存储在XHTML内部(这样就不用在文件外部使用带有图片的文件夹?XDocReport
直到现在才处理XWPFDocument
的页眉和页脚。而XHTML
是超文本标记。这不是嵌入图像。它通过IMG
元素链接图像:
。
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();
}
}