Jak przekonwertować docx do xhtml

Sep 11 2020

Próbuję znaleźć rozwiązanie do konwersji pliku docx do XHTML.

Znalazłem xdocreport, który wygląda dobrze, ale mam pewne problemy. (i jestem nowy w xdocreport)

Zgodnie z ich dokumentacją na github tutaj i tutaj : powinienem być w stanie przekonwertować za pomocą tego kodu:

    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();
    }

Używam tych zależności (próbowałem różnych wersji, takich jak 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>

Moje problemy:

  • Brakuje obrazów
  • Brakuje koloru tła (wszystkie strony mają kolor tła, który nie jest biały i też muszę to przekonwertować)

Jak mogę sobie z tym poradzić? (Lub jak mogę przekonwertować docx na xhtml za pomocą Docx4j z formatami / numeracją / obrazami?)

Odpowiedzi

1 AxelRichter Sep 11 2020 at 15:28

Aby przekonwertować *.docxdo XHTMLkorzystania XDocReporti apache poi„s XWPFDocumentjako źródło będzie trzeba XHTMLOptions. Te opcje umożliwiają ImageManagerustawienie ścieżki do wyodrębnionych obrazów z XWPFDocument. Następnie XHTMLConverterjest potrzebny do konwersji.

Kompletny przykład:

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();     
 
 }
}

To obsługuje obrazy prawidłowo.

Jednak do tej pory XDocReportnie jest w stanie XWPFDocumentprawidłowo obsługiwać kolorów tła strony . Wyodrębnia i obsługuje kolory tła akapitu, ale nie kolory tła strony.