如何显示或读取DOCX文件 [英] how to Show or Read docx file

查看:193
本文介绍了如何显示或读取DOCX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的android的渲染文件,我想呈现或在我的应用程序中显示的docx文件。

我已经提取的docx文件中的文本,但现在我想从DOCX文件中提取图像,以及。

我已经找到了几种显示在纯Java的图像,但也为Android任何好的例子?

我想这code抓取图片,但没有工作...

 公共无效extractImages(文件xmlDoc中)
{
    节点列表binDataList = xmlDoc.getElementsByTagName(W:图);
    字符串文件名=;
    节点currentNode;
    的for(int i = 0; I< binDataList.getLength();我++)
    {
        currentNode = binDataList.item(ⅰ);
        如果(currentNode.getNodeType()== Node.ELEMENT_NODE和放大器;及((元)currentNode).hasAttribute(W:名字))
        {
            文件newImageFile =新的文件(picDirectory,((元)currentNode).getAttribute(W:名).replaceFirst(WordML中://,));
            如果(newImageFile.exists())
            {

            }
            其他
            {
                如果(writeImage(newImageFile,currentNode))
                {
                    //打印一些成功的消息
                }
            }
        }
    }
 

解决方案

看看 AndroidDocxToHtml ,该我做了演示采用Android上docx4j。

一个警告夫妇。

首先,该项目不包括所有docx4j的依赖,只需要DOCX到HTML转换的人。所以,如果你想要做其他的事情,你可能需要别人的依赖关系。

二,docx4j需要JAXB - 看到这博客文章重新JAXB在Android - 并在应用程序启动时JAXB上下文初始化需要一段时间,具体取决于设备。有办法解决这个问题,但额外的努力。

如果你想要做的是提取图像,你不关心他们是如何涉及到的文字,你可以随便找图像部分。您可以使用 OpenXML4J 是什么,以及避免JAXB。

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
                }
            }
        }
    }

解决方案

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.

这篇关于如何显示或读取DOCX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆