Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Show or Read docx file

I am new to rendering files in android, and I want to render or display a docx file in my application.

I had already extract text from docx file, but now I want to extract images from the docx file as well.

I've found several ways to display images in pure Java, but are there any good examples for Android?

I tried this code to fetch Images but not working...

public void extractImages(Document xmlDoc)
{
    NodeList binDataList = xmlDoc.getElementsByTagName("w:drawings");
    String fileName = "";
    Node currentNode;
    for(int i = 0; i < binDataList.getLength(); i++)
    {
        currentNode = binDataList.item(i);
        if(currentNode.getNodeType() == Node.ELEMENT_NODE && ((Element)currentNode).hasAttribute("w:name"))
        {               
            File newImageFile = new File(picDirectory, ((Element)currentNode).getAttribute("w:name").replaceFirst("wordml://", ""));
            if(newImageFile.exists())
            {

            }
            else
            {
                if(writeImage(newImageFile, currentNode))
                {
                    //Print some success message
                }
            }
        }
    }
like image 869
SilentKiller Avatar asked May 04 '12 11:05

SilentKiller


People also ask

How do I view DOCX files for free?

Open any file from Microsoft Word with Doc Viewer. Doc Viewer is a fast, free, simple app for viewing Doc, DocX, and other text files. Access your files in one click from the handy live tile, or share and print your document, all completely free. You shouldn't need expensive software just to open and print a document.

Why can't I read DOCX files?

Disable the protected view settings; it can also resolve and open corrupt word DOC and DOCX files to fix the issue where the Word file would not open. Open the Microsoft Word application on your system. Click on 'Options' in the 'File' menu. Click on 'Trust Center' and then select 'Trust Center Settings.

Which app is used for viewing DOCX?

You can upload and download files with the Google Docs app for Android. Import: You can open and edit DOC, DOCX, ODT, TXT, RTF, and HTML files.

How do I display a DOCX file in HTML?

We will use <iframe> tag to embed word/excel document in HTML web page using Office Web Viewer link. You can use the above code to embed and display all types of the Microsoft Office documents (. doc, . docx, .


2 Answers

Have a look at AndroidDocxToHtml, which I made to demonstrate using docx4j on Android.

A couple of caveats.

First, that project does not include all docx4j dependencies, only the ones required for docx to HTML conversion. So if you want to do other things, you may need others of the dependencies.

Second, docx4j requires JAXB - see this blog post re JAXB on Android - and JAXB context init on app startup takes a while depending on the device. There are ways to work around this, but at extra effort.

If all you want to do is extract the images, and you don't care how they relate to the text, you could just look for image parts. You might use OpenXML4J for that, and avoid JAXB.

like image 137
JasonPlutext Avatar answered Nov 10 '22 00:11

JasonPlutext


The easiest way to create an image in Android is to use the BitmapFactory factory methods.

The BitmapFactory class has methods for creating a Bitmap from a byte array, a file or an InputStream.

Once you have a Bitmap object you can display it by setting it on an ImageView in your layout using the setImageBitmap method.

like image 35
twaddington Avatar answered Nov 10 '22 00:11

twaddington