Come convertire docx in xhtml
Sto cercando di trovare una soluzione per convertire un file docx in XHTML.
Ho trovato xdocreport, che sembra buono, ma ho alcuni problemi. (e sono nuovo su xdocreport)
Secondo le loro documentazioni su GitHub qui e qui : dovrei essere in grado di convertire con questo codice:
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();
}
Sto usando queste dipendenze (ho provato diverse versioni, come 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>
I miei problemi:
- Mancano le immagini
- Manca il colore di sfondo (tutte le pagine hanno un colore di sfondo, che non è bianco e devo convertire anche questo)
Come posso gestire questi problemi? (O come posso convertire docx in xhtml usando Docx4j con formati / numerazione / immagini?)
Risposte
Per convertire *.docxad XHTMLuso XDocReporte apache poi'il XWPFDocumentcome fonte avrete bisogno XHTMLOptions. Queste opzioni possono dover ImageManagerimpostare il percorso per le immagini estratte da XWPFDocument. Quindi XHTMLConverterè necessario convertire.
Esempio completo:
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();
}
}
Questo gestisce le immagini correttamente.
Ma fino ad ora XDocReportnon è in grado di gestire XWPFDocumentcorrettamente i colori di sfondo della pagina . Estrae e gestisce i colori di sfondo del paragrafo ma non i colori di sfondo della pagina.