2012-12-11 47 views
2

有一个文件.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改变(元素,布尔值,图片)方法

还有其他替代方法或转换器的示例,支持图像吗?

回答

2

在这种情况下,您最好选择使用Apache Tika,让它为您包装Apache POI。 Apache Tika将为您的文档生成HTML(或纯文本,但您希望为您的HTML使用HTML)。除此之外,它还会嵌入嵌入式资源的占位符,嵌入式图像的img标签,并为您提供一种获取嵌入式资源和图像内容的方法。

有一个非常好的例子,包括在Alfresco,HTMLRenderingEngine。您可能希望在那里查看代码,然后编写自己的代码来做类似的事情。其中的代码包括一个自定义的ContentHandler,它允许编辑img标签,重新编写src属性,根据将要写入图像的位置,您可能需要也可能不需要该属性。

+0

非常感谢您对您的回复,Gagravarr解析文档中的新类! 现在我会尽力去做。 –

+0

@Alexey,请你提供一些关于你如何解决这个问题的细节,任何有用的链接? –

2

扩展WordToHtmlConverter并覆盖processImageWithoutPicturesManager

import java.util.Base64; 

import org.apache.poi.hwpf.converter.WordToHtmlConverter; 
import org.apache.poi.hwpf.usermodel.Picture; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
public class InlineImageWordToHtmlConverter extends WordToHtmlConverter { 

    public InlineImageWordToHtmlConverter(Document document) { 
     super(document); 
    } 

    @Override 
    protected void processImageWithoutPicturesManager(Element currentBlock, 
     boolean inlined, Picture picture) 
    { 
     Element imgNode = currentBlock.getOwnerDocument().createElement("img"); 
     StringBuilder sb = new StringBuilder(); 
     sb.append(Base64.getMimeEncoder().encodeToString(picture.getRawContent())); 
     sb.insert(0, "data:"+picture.getMimeType()+";base64,"); 
     imgNode.setAttribute("src", sb.toString()); 
     currentBlock.appendChild(imgNode); 
    } 

} 

使用,而如下图所示

HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream("D:/temp/Temp.doc"));  
     WordToHtmlConverter wordToHtmlConverter = new InlineImageWordToHtmlConverter(
       DocumentBuilderFactory.newInstance().newDocumentBuilder() 
         .newDocument()); 
     wordToHtmlConverter.processDocument(wordDocument); 
+0

这仅支持将图像作为内联base64内容。要创建链接,您需要不同的方法。 – raok1997

+0

非常感谢您的回答! –

+0

感谢那个男人! – MarianP