Displaying a MS Word File in View(say TextView) in Android
Hello guys After much consideration and looking at hell lot of options and workarounds I think OliveDocLibrary is the best way to do it. Here is the link which will give direct you to the downloads page of three libraries for Android which are for DOC, XLS and PPT. All these work excellently well. The package folder you will download will have three folders inside. which are:
- API
- lib_trial
- Demo
In the demo folder you will find a sample project for Word. You can directly import this project into your workspace in Eclipse and test the code yourself. For peoples convenience am posting that code here. I have deleted some part of the code which I felt was not necessary(w.r.t the answer to my question here). So the code has two files, The main activity is FileChooser
which is as follows:
public class FileChooser extends Activity {
private String filePath = Environment.getExternalStorageDirectory()
.getPath() + "/simple.docx";
MyBaseAdapter adapter;
private static String parentPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(2);
copyFileToSdcard();
Intent intent = new Intent(FileChooser.this,
OliveWordTrailDemoAcitivy.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(new File(filePath)));
startActivity(intent);
}
class MyBaseAdapter extends BaseAdapter {
private String[] list;
public MyBaseAdapter(String[] list) {
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(FileChooser.this);
((TextView) convertView).setTextSize(35);
}
((TextView) convertView).setText(list[position]);
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getCount() {
return list.length;
}
public void setList(String[] list) {
this.list = list;
}
};
class MyItemClickListener implements OnItemClickListener {
String[] list;
InputStream is;
public MyItemClickListener(String[] list) {
this.list = list;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
File file = new File(parentPath + list[position]);
if (file.isFile()) {
Intent intent = new Intent(FileChooser.this,
OliveWordTrailDemoAcitivy.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file));
startActivity(intent);
} else {
list = file.list();
adapter.setList(list);
adapter.notifyDataSetChanged();
parentPath = file.getAbsolutePath() + "/";
}
}
}
private void copyFileToSdcard() {
InputStream inputstream = getResources().openRawResource(
R.raw.simple);
byte[] buffer = new byte[1024];
int count = 0;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filePath));
while ((count = inputstream.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Toast.makeText(FileChooser.this, "Check your sdcard", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here I have placed a doc file named simple.docx which includes images and mathematical symbols which are rendered and displayed properly. This activity interacts with OliveWordTrialDemoActivity
which is as follows:
public class OliveWordTrailDemoAcitivy extends Activity implements
OnClickListener, CommentListener, NoteListener, HyperlinkListener, ProgressListener {
OliveWordOperator viu;
EditText searchEditText;
ArrayList<String> bookmarks;
Handler handler;
protected void onCreate(Bundle savedInstanceState) {
viu = new OliveWordOperator(this, this);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.demo_view);
OliveWordView view = (OliveWordView) findViewById(R.id.test_view);
try {
viu.init(view, getIntent().getData());
viu.start(viu.isEncrypted(), "111");
} catch (Exception e) {
e.printStackTrace();
}
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
setProgress(msg.what * 10);
super.handleMessage(msg);
}
};
}
@Override
protected void onDestroy() {
viu.release();
super.onDestroy();
}
@Override
public void getComment(ArrayList<String[]> comments) {
for (int i = 0; i < comments.size(); i++) {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(comments.get(i)[0]).setMessage(comments.get(i)[1])
.show();
}
}
@Override
public void getHyperlink(String hyperlink) {
if (Uri.parse(hyperlink).getScheme().contains("mailto")) {
try {
startActivity(new Intent(Intent.ACTION_SENDTO,
Uri.parse(hyperlink)));
} catch (ActivityNotFoundException anfe) {
Toast.makeText(this, "can't found email application",
Toast.LENGTH_SHORT).show();
}
} else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(hyperlink)));
}
}
@Override
public void getNote(SparseArray<String> notes) {
for (int i = 0; i < notes.size(); i++) {
AlertDialog.Builder builder = new Builder(this);
if (notes.keyAt(i) == NoteListener.FOOTNOTE) {
builder.setTitle("footnote").setMessage(notes.valueAt(i))
.show();
} else if (notes.keyAt(i) == NoteListener.ENDNOTE) {
builder.setTitle("endnote").setMessage(notes.valueAt(i)).show();
}
}
}
public void goToBookmarks(String name) {
viu.goToBookmark(name);
}
public void listBookmarks() {
this.bookmarks = viu.listBookmarks();
}
@Override
public void notifyProgress(int progress) {
handler.sendEmptyMessage(progress);
}
@Override
public void onClick(View v) {
}
}
In the lib_trial folder you will find the library which can be added to your libs folder if you want to use it separately.
And in the API folder you will find a detailed description of the library and its methods in form of a pdf file which is very easy to understand. so people can just use this library directly and use the methods provided to their specific requirement.
So that's the solution am going with for now. Any better solutions are welcome. The bounty time is about to get over soon so please provide any other solution you may have as soon as possible. Thanks.
As you mention in your question that you already tried few of library like Jopendocument,
OliveDocLibrary
and Apache POI
but no luck.
Now I want to modify this code so that I can display MS Word files(.docx) which contain text,images and mathematical symbols in between.
While research I came across one more library named Tika, which also used to extract data and its support listed documents and even Libra Office where you can read write and manage documents.
Last suggestion:
You can achieve by converting, doc to html
and html to pdf
as mention here.
To convert doc to html
refer stack-overflow answer