我想在Android的View
中显示 .docx 文件。该文件具有数学符号以及文本之间的图像。我想显示许多这样的文件并通过swipe
手势翻转它们。我已经为 .txt 文件成功完成了相同的操作。现在可以很容易地轻扫到下一页。 .txt 文件的代码如下:
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;
}
此代码返回我随后在TextView
中显示的文本。这样我只需要动态更改文件名,然后轻扫文本更改。
现在我想修改此代码,以便显示包含文本,图像和数学符号的MS Word文件(.docx)。
我已经在堆栈溢出以及其他论坛上检查过许多关于此主题的类似线程: 这些是许多人建议作为类似问题的答案的链接,我已尝试过这些:Link1和link2
同样在许多其他主题上,人们推荐 Jopendocument 。我也读到了这一点,并了解到Android不支持开放文档格式。所以选项似乎不太可能。但是,如果您有任何解决方法或关于将JOpenDocument库添加到项目并显示富文本的详细解释,请分享该解决方案,因为我已经搜索了很多但却找不到任何解决方案。
还有另一个名为 OliveDocLibrary 的库,用于在android上显示丰富的word文件。这是我下载lib的link。该下载包中包含的演示工作正常。但是lib是试用版。所以我现在正在尝试使用这个库并查看它的位置。但我仍然在寻找更好的选择。
对此有任何帮助表示赞赏。除了上面提到的指针之外的任何指针都是最受欢迎的。
更新
我得到了一个建议,告诉我在这个问题的第一个赏金中使用了 Apache POI (更具体地说是HWPF)。在探索Apache POI一段时间之后,我得到的代码很少,这些代码写入了doc文件,读取了doc文件,更新了excel表等。
我在互联网上找到的这样的示例代码(对于Java)是这样的:
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;i<fileData.length;i++){
if(fileData[i] != null)
System.out.println(fileData[i]);
}
}
catch(Exception exep){}
}
}
所以我在eclipse中将这个库(Apache POI)添加到了我的Android项目中,并尝试了这个示例代码并进行了一些更改。并尝试在TextView
上显示它。这里的问题是它不显示像 OliveDocLibrary 那样的图像。因此,如果有人要建议Apache POI,那么我要求一个实体指针或一个代码,它读取 docx 文件和所有其内容(包括图像)和显示他们在自定义视图中。
Apache POI 是一件好事,但遗憾的是我没有找到任何实现这些库的好示例/示例。如果你知道一个很好的例子来源(仅限MS字),请在评论中分享。
更新2 :
在 OliveDocLibrary 包中,提供的代码运行正常。虽然在视图上有Olive的水印。目前正在努力对该代码执行Swipe
。但问题仍然是它的试用版。
更新3:
我认为 OliveDocLibrary 是最有效的方法。虽然它有一个试用版的缺点,但我认为没有其他图书馆能比这个图书馆更好地完成我的特定要求。详细答案已发布below。随着赏金时间即将结束。我会请求可能有替代和更好解决方案的人尽快发布。现在我正在使用 OliveDocLibrary 并接受我自己的答案。
答案 0 :(得分:7)
大家好。经过深思熟虑,看了很多选项和解决方法,我认为 OliveDocLibrary 是最好的方法。这是link,它将引导您访问Android的三个库的下载页面,这些库用于DOC,XLS和PPT。所有这些都非常好。您将下载的包文件夹中将包含三个文件夹。这是:
在演示文件夹中,您将找到Word的示例项目。您可以直接将此项目导入Eclipse中的工作区并自行测试代码。为方便人们,我在这里发布该代码。我删除了一些我认为没有必要的代码部分(在这里我的问题的答案)。所以代码有两个文件,主要活动是FileChooser
,如下所示:
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();
}
}
}
这里我放置了一个名为 simple.docx 的doc文件,其中包含正确渲染和显示的图像和数学符号。此活动与OliveWordTrialDemoActivity
互动,如下所示:
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) {
}
}
在 lib_trial 文件夹中,如果要单独使用,可以找到可以添加到libs文件夹的库。
在 API 文件夹中,您将以pdf文件的形式找到该库及其方法的详细说明,该文件非常易于理解。所以人们可以直接使用这个库,并使用提供给他们特定要求的方法。
这就是我现在要解决的问题。欢迎任何更好的解决方案。赏金时间即将结束,所以请尽快提供您可能拥有的任何其他解决方案。感谢。
答案 1 :(得分:1)
正如您在问题中提到的那样,您已尝试使用Jopendocument,
OliveDocLibrary
和Apache POI
这样的库,但没有运气。
现在我要修改此代码,以便显示MS Word文件(.docx),其中包含文本,图像和数学符号。
在研究期间,我又遇到了另一个名为Tika的库,它也用于提取数据及其support listed documents甚至Libra Office,您可以在其中读取和管理文档。
最后的建议:
您可以转换doc to html
和html to pdf
作为提及here来实现。
要转换doc to html
,请参阅stack-overflow answer