有一个文件.doc包含一些图像。如何将它转换为* .html,这样图像会保留下来?Apache POI - сonverting* .doc to * .html与图像
我使用的例子,从这个话题 - Convert Word doc to HTML programmatically in Java
但图像丢失。 这里是我使用转换器 -
public class Converter {
private File docFile;
private File file;
public Converter(File docFile) {
this.docFile = docFile;
}
public void convert(File file){
this.file = file;
try{
FileInputStream finStream=new FileInputStream(docFile.getAbsolutePath());
HWPFDocument doc=new HWPFDocument(finStream);
WordExtractor wordExtract=new WordExtractor(doc);
Document newDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument) ;
wordToHtmlConverter.processDocument(doc);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(
new DOMSource(wordToHtmlConverter.getDocument()),
new StreamResult(stringWriter));
String html = stringWriter.toString();
FileOutputStream fos;
DataOutputStream dos;
try {
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
out.write(html);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(false);
editorPane.setPage(file.toURI().toURL());
JScrollPane scrollPane = new JScrollPane(editorPane);
JFrame f = new JFrame("Display Html File");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setSize(512, 342);
f.setVisible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
}
它说这里 - http://poi.apache.org/apidocs/org/apache/poi/hwpf/converter/WordToHtmlConverter.html
“此实现不创建图片或链接到他们这可以通过重写AbstractWordConverter.processImage改变(元素,布尔值,图片)方法“
还有其他替代方法或转换器的示例,支持图像吗?
非常感谢您对您的回复,Gagravarr解析文档中的新类! 现在我会尽力去做。 –
@Alexey,请你提供一些关于你如何解决这个问题的细节,任何有用的链接? –