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

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

问题描述

我是在 android 中渲染文件的新手,我想在我的应用程序中渲染或显示 docx 文件.

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

我已经从 docx 文件中提取了文本,但现在我还想从 docx 文件中提取图像.

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

我已经找到了几种用纯 Java 显示图像的方法,但有没有适用于 Android 的好例子?

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

推荐答案

看看 AndroidDocxToHtml,我在 Android 上使用 docx4j 进行演示.

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

一些警告.

首先,该项目不包含所有 docx4j 依赖项,仅包含 docx 到 HTML 转换所需的依赖项.所以如果你想做其他事情,你可能需要其他的依赖.

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.

其次,docx4j 需要 JAXB - 参见 这篇博文是关于 Android 上的 JAXB - 应用程序启动时的 JAXB 上下文初始化需要一段时间,具体取决于设备.有很多方法可以解决这个问题,但需要额外的努力.

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.

如果您只想提取图像,并且不关心它们与文本的关系,则可以只查找图像部分.为此,您可以使用 OpenXML4J,并避免使用 JAXB.

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天全站免登陆