2016-07-25 2 views
2

TIKAで新しいです。 Tikaを使用して、MicrosoftのワードドキュメントをHTMLに変換しようとしています。私はTikaOnDotNetラッパーを使用して.NETフレームワークでTIKAを使用しています。私のコンバージョンコードは、次のようなものです:TIKAによって埋め込まれた画像を含むHTMLにワードドキュメントを変換する

 byte[] file = Files.toByteArray(new File(@"myPath\document.doc")); 
     AutoDetectParser tikaParser = new AutoDetectParser(); 

     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance(); 
     TransformerHandler handler = factory.newTransformerHandler(); 
     handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html"); 
     handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); 
     handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
     handler.setResult(new StreamResult(output)); 

     ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler); 

     tikaParser.parse(new ByteArrayInputStream(file), handler1, new Metadata()); 


     File ofile = new File(@"C:\toHtml\text.html"); 
     ofile.createNewFile(); 
     DataOutputStream stream = new DataOutputStream(new FileOutputStream(ofile)); 
     output.writeTo(stream); 

埋め込み画像以外はすべてうまく機能します。生成されたHTMLには、

<img src="embedded:image2.wmf" alt="image2.wmf"/> 

のような画像タグが含まれていますが、画像ソースはありません。助けてください

+2

あなたは、適切な[EmbeddedDocumentExtractor](http://tika.apache.org/1.13/api/org/apacheを設定するのを忘れました/tika/extractor/EmbeddedDocumentExtractor.html)を 'ParseContext'に追加して、保存するリソースと保存する場所を指定します。 – Gagravarr

+0

@Gagravarr私に例を教えてください。私はこれが主な理由だと思う。 – Mahdi

+2

'TikaCLI'には、[Gitのこのビット](https://github.com/apache/tika/blob/master/tika-app/src/main/java/org/apache/tika/cli/)のようなものがあります。 TikaCLI.java#L1004) – Gagravarr

答えて

2

クレジットは@Gagravarrに行きます。

これはコードの簡単な実装であり、元のコードは質問のコメントで利用できることに注意してください。

この実装はTikaOnDotNetラッパーに基づいています.....

public class DocToHtml 
{ 

    private TikaConfig config = TikaConfig.getDefaultConfig(); 
    public void Convert() 
    { 

     byte[] file = Files.toByteArray(new File(@"filename.doc")); 
     AutoDetectParser tikaParser = new AutoDetectParser(); 

     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance(); 
     var inputStream = new ByteArrayInputStream(file); 
     //   ToHTMLContentHandler handler = new ToHTMLContentHandler(); 
     var metaData = new Metadata(); 
     EncodingDetector encodingDetector = new UniversalEncodingDetector(); 
     var encode = encodingDetector.detect(inputStream, metaData) ?? new UTF_32(); 
     TransformerHandler handler = factory.newTransformerHandler(); 
     handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html"); 
     handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes"); 
     handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, encode.toString()); 
     handler.setResult(new StreamResult(output)); 

     ContentHandler imageRewriting = new ImageRewritingContentHandler(handler); 

     // ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler); 
     ParseContext context = new ParseContext(); 
     context.set(typeof(EmbeddedDocumentExtractor), new FileEmbeddedDocumentEtractor()); 

     tikaParser.parse(inputStream, imageRewriting, new Metadata(), context); 


     byte[] array = output.toByteArray(); 

     System.IO.File.WriteAllBytes(@"C:\toHtml\text.html", array); 

    } 


    private class ImageRewritingContentHandler : ContentHandlerDecorator 
    { 
     public ImageRewritingContentHandler(ContentHandler handler) : base(handler) 
     { 
     } 

     public override void startElement(string uri, string localName, string name, Attributes origAttrs) 
     { 
      if ("img".Equals(localName)) 
      { 
       AttributesImpl attrs; 
       if (origAttrs is AttributesImpl) 
        attrs = (AttributesImpl)origAttrs; 
       else 
        attrs = new AttributesImpl(origAttrs); 



       for (int i = 0; i < attrs.getLength(); i++) 
       { 
        if ("src".Equals(attrs.getLocalName(i))) 
        { 
         String src = attrs.getValue(i); 
         if (src.StartsWith("embedded:")) 
         { 
          var newSrc = src.Replace("embedded:", @"images\"); 

          attrs.setValue(i, newSrc); 
         } 
        } 
       } 
       attrs.addAttribute(null, "width", "width","width", "100px"); 
       base.startElement(uri, localName, name, attrs); 
      } 
      else 
       base.startElement(uri, localName, name, origAttrs); 
     } 
    } 

    private class FileEmbeddedDocumentEtractor : EmbeddedDocumentExtractor 
    { 
     private int count = 0; 
     public bool shouldParseEmbedded(Metadata m) 
     { 
      return true; 
     } 

     public void parseEmbedded(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, bool outputHtml) 
     { 
      Detector detector = new DefaultDetector(); 
      string name = metadata.get("resourceName"); 
      MediaType contentType = detector.detect(inputStream, metadata); 
      if (contentType.getType() != "image") return; 
      var embeddedFile = name; 
      File outputFile = new File(@"C:\toHtml\images", embeddedFile); 
      try 
      { 
       using (FileOutputStream os = new FileOutputStream(outputFile)) 
       { 
        var tin = inputStream as TikaInputStream; 
        if (tin != null) 
        { 
         if (tin.getOpenContainer() != null && tin.getOpenContainer() is DirectoryEntry) 
         { 
          POIFSFileSystem fs = new POIFSFileSystem(); 

          fs.writeFilesystem(os); 
         } 
         else 
         { 
          IOUtils.copy(inputStream, os); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 

       throw; 
      } 
     } 
    } 
} 
関連する問題