You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@tika.apache.org by "Jana, Kumar Raja" <kj...@ptc.com> on 2009/05/15 12:38:33 UTC

Document creation using POI and parsing using TIKA

We are creating a word doc using the POI library as below

1.	import java.io.*;  
2.	import org.apache.poi.hwpf.HWPFDocument;  
3.	import org.apache.poi.hwpf.usermodel.*;  
4.	  
5.	public class TestPOIWordWrite {  
6.	        public static void main (String[] args) throws Exception
{  
7.	            // POI apparently can't create a document from
scratch,  
8.	            // so we need an existing empty dummy document  
9.	            HWPFDocument doc = new HWPFDocument(new
FileInputStream("D:\\tmp.doc"));  
10.	            Range range = doc.getRange();  
11.	            CharacterRun run = range.insertAfter("My Contents
Here");  
12.	            run.setBold(true);  
13.	            OutputStream out = new
FileOutputStream("D:\\result.doc");  
14.	            doc.write(out);  
15.	            out.flush();  
16.	            out.close();  
17.	       
18.	    }  
19.	}  

Case- I: When I open the generated document in MSWord it opens just fine
but when I parse it through TikaGUI it just shows me the first character
of the content. 'M' in the above example.

 

Case-II: If I modify the above line of code from in red from
range.insertAfter to range.insertBefore the generated document gets
parsed in TikaGUI and shows me the full content but does not open up in
MSWord now. It says the document might be corrupt.

 

Any help on this issue? I need to open this generated word document in
MS Word as well as want to parse it using Tika api's.