分享

基于Android的Word文档阅读器

 software1 2011-07-13
 

        随着android系统的发展,android已经得到广泛的认可,作为一名普通的大学生,真的希望自己能在android系统上做一个可以让大家使用、方便大家工作的一个软件,最后决定做一个基于andriod的Word格式阅读器。

        经过一些查找工作,终于找到了可以在android系统上使用的用于读取Word格式文档的开源包--POI(The Java API For Microsoft Documents)。下载地址:http://poi./

       POI是Apache的一个子项目,其目的是提供对基于OOXML(Microsoft Office Open XML)和OLE2(Object Linking and Embedding)的各种文档操作的Java APIs包。该项目分为几个组件,其中包括一个叫做HWPF的组件,它只能操作Word文件。这就是我将要使用的组件。HWPF的全称是Horrible Word Processor Format。翻译成中文是“可怕的文档处理格式”,利用HWPF,开发者可以用纯Java代码实现在Android系统是读取Word文档。HWPF组件是POI项目中用来实现Word文档读取的一个重要组件,以下是该组件中几个重要的类:

(1)Range:是所有HWPF对象模型的核心类,Word文档中字符的所有属性都是继承这个类得到的。

(2)HWPFDocument:文件类。任何形式的Word文档的最终表现形式都是对该对象进行一些属性的定制。

(3)Paragraph:是Word文档中基本的组成部分,每个文档都被划分成一个一个的段落,所有的段落最终组成一个Word文档。

(4)Picture:Word文档中嵌入的每张图片都是由Picture对象来表示的,它包括了图片的大小、内容等一系列属性。

(5)Table:Word文档中嵌入的每张表格都是有Table对象来表示的,它包括了表格中每行的TableRow对象和行数等属性。

       其他的类文件对以上核心的类进行功能补充,最终完成了Word 文档的读取。

       由于Word格式文档中有图片、表格和字符串,为了能在android系统上全部进行显示,选择使用WebView进行显示。首先将Word格式文档中的内容读取出来,加上对应的HTML标签,然后写入HTML文件中,最后直接使用WebView进行读取HTML文件的内容。

判断当前段落是表格、图片还是一段文字的代码:

 

  1. public void writeParagraphContent(Paragraph paragraph){ Paragraph p = paragraph; int pnumCharacterRuns = p.numCharacterRuns(); forint j = 0; j < pnumCharacterRuns; j++){ CharacterRun run = p.getCharacterRun(j); if(run.getPicOffset() == 0 || run.getPicOffset() >= 1000){ if(presentPicture < pictures.size()){ writePicture(); } } elsetry{ String text = run.text(); if(text.length() >= 2 && pnumCharacterRuns < 2){ output.write(text.getBytes()); } elseint size = run.getFontSize(); int color = run.getColor(); String fontSizeBegin = "<font size=\"" + decideSize(size) + "\">"; String fontColorBegin = "<font color=\"" + decideColor(color) + "\">"; String fontEnd = "</font>"; String boldBegin = "<b>"; String boldEnd = "</b>"; String islaBegin = "<i>"; String islaEnd = "</i>"; output.write(fontSizeBegin.getBytes()); output.write(fontColorBegin.getBytes()); if(run.isBold()){ output.write(boldBegin.getBytes()); } if(run.isItalic()){ output.write(islaBegin.getBytes()); } output.write(text.getBytes()); if(run.isBold()){ output.write(boldEnd.getBytes()); } if(run.isItalic()){ output.write(islaEnd.getBytes()); } output.write(fontEnd.getBytes()); output.write(fontEnd.getBytes()); } } catch(Exception e){ System.out.println("Write File Exception"); } } } }  

 

在SDCARD上创建一个图片的代码:

  1. public void writePicture(){ Picture picture = (Picture)pictures.get(presentPicture); byte[] pictureBytes = picture.getContent(); Bitmap bitmap = BitmapFactory.decodeByteArray(pictureBytes, 0, pictureBytes.length); makePictureFile(); presentPicture++; File myPicture = new File(picturePath); try{ FileOutputStream outputPicture = new FileOutputStream(myPicture); outputPicture.write(pictureBytes); outputPicture.close(); } catch(Exception e){ System.out.println("outputPicture Exception"); } String imageString = "<img src=\"" + picturePath + "\""if(bitmap.getWidth() > screenWidth){ imageString = imageString + " " + "width=\"" + screenWidth + "\""; } imageString = imageString + ">"try{ output.write(imageString.getBytes()); } catch(Exception e){ System.out.println("output Exception"); } }  

运行效果截图:

读取图片和文字:



读取表格和文字:


 

源代码及测试Word文档打包:

http://download.csdn.net/source/3432624

本文系“暑期大学生博客大赛-2011 Android成长篇“参赛文章

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多