1

我正在尝试找到将 docx 文件转换为 XHTML 的解决方案。

我找到了 xdocreport,它看起来不错,但我有一些问题。(我是 xdocreport 的新手)

根据他们在 github 上的文档herehere:我应该能够使用此代码进行转换:

    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?)

4

1 回答 1

2

要转换*.docxXHTML使用XDocReport和作为源apache poiXWPFDocument您将需要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还无法正确处理页面背景颜色。XWPFDocument它提取和处理段落背景颜色,但不提取页面背景颜色。

于 2020-09-11T15:28:56.307 回答