Android-Based Word Document Reader

Source: Internet
Author: User

With the development of Android, Android has been widely recognized. As an ordinary college student, I really hope that I can build a software on the Android system for everyone to use and make it easier for everyone to work, and finally decide to create a word reader based on andriod.

After some searching work, I finally found the open-source package that can be used on the Android system to read documents in Word format-the Java API for Microsoft documents ). : Http://poi.apache.org/

Poi is a sub-project of Apache. It aims to provide Java APIs packages for operations on various documents based on ooxml (Microsoft Office open XML) and ole2 (Object Linking and Embedding. This project is divided into several components, including a component called hwpf, which can only operate on Word files. This is the component I will use. The full name of hwpf is horrible word processor format. Translating into Chinese is a "terrible document processing format". With hwpf, developers can use pure Java code to read Word documents in Android systems. The hwpf component is an important component used in the poi project to read Word documents. The following are several important classes of this component:

(1) range: it is the core class of all hwpf object models. All attributes of characters in Word documents are inherited from this class.

(2) hwpfdocument: File class. The final form of any form of Word document is to customize the attributes of the object.

(3) Paragraph: a basic component of a Word document. Each document is divided into one paragraph and all paragraphs form a Word document.

(4) picture: each image embedded in a Word document is represented by a picture object, which includes a series of attributes such as the image size and content.

(5) Table: Each table embedded in a Word document is represented by a table object, which includes attributes such as the tablerow object and number of rows in each row of the table.

Other class files supplement the functions of the above core classes, and finally complete the reading of Word documents.

Because Word documents contain images, tables, and strings, you can use webview to display all of them on the Android system. First, read the content in the Word format document, add the corresponding HTML Tag, then write it into the HTML file, and then directly use webview to read the content of the HTML file.

Code used to determine whether the current paragraph is a table, image, or text:

public void writeParagraphContent(Paragraph paragraph){Paragraph p = paragraph;int pnumCharacterRuns = p.numCharacterRuns();for( int j = 0; j < pnumCharacterRuns; j++){CharacterRun run = p.getCharacterRun(j);if(run.getPicOffset() == 0 || run.getPicOffset() >= 1000){if(presentPicture < pictures.size()){writePicture();}}else{try{String text = run.text();if(text.length() >= 2 && pnumCharacterRuns < 2){output.write(text.getBytes());}else{              int 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");}}}}

Code for creating an image on sdcard:

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 = " screenWidth){imageString = imageString + " " + "width=\"" + screenWidth + "\"";}imageString = imageString + ">";try{output.write(imageString.getBytes());}catch(Exception e){System.out.println("output Exception");}}

Running effect:

Read images and text:

Read tables and text:

Package source code and test Word documents:

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

This article is an article titled "Summer College Students' blog contest-2011 Android into a long article"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.