2012-12-11 49 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