Wednesday, April 17, 2013

[android help] Displaying a MS Word File in View(say TextView) in Android


I want to display a .docx file in a View in Android. The file has mathematical symbols and also images in between the text. I want to display many such files and flip through them via swipe gesture. I have successfully done the same for .txt files. And can now very easily go to the next page on swipe. The code for .txt file is as follows:



public String readTxt(String fileName)
{


try {
InputStream is;
is = context.getAssets().open(fileName + ".txt");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

int i;
i = is.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = is.read();
}

is.close();

return byteArrayOutputStream.toString();

}

catch (IOException e)
{
e.printStackTrace();
}

return fileName;
}


This code returns the text which I then display in my TextView. This way I only need to change the name of the file dynamically and on swipe the Text changes.


Now I want to modify this code so that I can display MS Word files(.docx) which contain text,images and mathematical symbols.


I have already checked many similar threads on this topic on stack overflow as well as on other forums: These are the links many people have suggested as answers to similar questions and I have already given these a try: Link1 and link2


Also on many other threads people have recommended Jopendocument. I have also read about that and learnt that Android does not support open document format. so that option seems unlikely. But if you have any workaround or a good detailed explanation with respect to adding the JOpenDocument library to the project and displaying rich text then please share that solution because I have searched for it a lot but couldn't find any.


There is also another library called OliveDocLibrary to display rich word files on android. here is the link from where I downloaded the lib. The demo included in that download package works just fine.But the lib is a trial version. So am currently trying to work with this library and see where it goes. But am still in search of better options.


Any help regarding this is appreciated. Any pointers other than the ones mentioned above are most welcome.


Update:


I got a suggestion which told the use of Apache POI(HWPF more specifically) in the first bounty I started on this question. After exploring Apache POI for some time, I got few codes which were writing into a doc file, reading the doc file, updating the excel sheets etc.


Such Sample code(for Java) which I found off the internet goes something like this:



import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class ReadDocFile {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null ;
try {

file = new File("c:\\New.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;iif(fileData[i] != null)
System.out.println(fileData[i]);
}
}
catch(Exception exep){}
}
}


So I added this library(Apache POI) to my Android project in eclipse and tried this sample code with some changes. And tried displaying it on a TextView. The problem here though is it doesn't display the images like OliveDocLibrary does. So if Someone is going to suggest Apache POI, then I request for a solid pointer or a code which reads a docx file and all its contents(that includes images) and displays them in a custom view.


Apache POI is a great thing but sadly I didn't find any good examples/samples implementing those libraries. If you know a good source of examples(w.r.t MS word only) then please share them in comments.


Update 2:


In OliveDocLibrary package the code provided works fine. There is water mark of Olive on the View though. Currently am working on performing Swipe on that code. But the problem remains that its a trial version.


Update 3:


I think OliveDocLibrary is the most efficient way to do it. Though it has a disadvantage of being a trial version, I think no other library does a better job than this library to full fill my specific requirement. The detailed answer has been posted below. As the bounty time is about to get over. I would request the people who may have an alternate and better solution to post it as soon as possible. For now am going with OliveDocLibrary and accepting my own answer.



.

stackoverflow.comm

1 comment:

  1. Hi im trying something similar as to what you did
    but ive not been able to display documents using apache poi

    so if you have any working related code please do share the same

    thanks

    ReplyDelete

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...