将文档文件中的图像和图形导出到OpenOffice中的图像 [英] export images and graphics in doc files to images in openoffice

查看:104
本文介绍了将文档文件中的图像和图形导出到OpenOffice中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openoffice sdk从doc文件中获取所有信息,我现在需要的是能够提取doc文件中的所有图像并将其另存为png或gif图片.

I am using openoffice sdk to get all info from doc file, what I need now is to be able to extract all images in a doc file and save them as images png or gif.

我正在使用Java,有没有可行的示例?

I am using Java, Is there any working example?

谢谢;

推荐答案

此处是示例如何从MS Word/OpenOffice文档中提取图像的示例.它是用C#编写的,但是将其转换为Java却很简单.

Here it is example how to extract images from MS Word/OpenOffice documents. It is written in C# but it trivial to convert it to Java.

链接: http://blog-of-darius.blogspot.com/2011/03/extract-images-from-word-openoffice.html

已更新: 我将代码转换为Java

Updated: I converted code to Java

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.container.XNameAccess;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.graphic.XGraphic;
import com.sun.star.graphic.XGraphicProvider;
import com.sun.star.io.IOException;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextGraphicObjectsSupplier;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XCloseable;

public class Program {

    public static void exportGraphicObject(XGraphicProvider xGraphicProvider, XGraphic xGraphic, String fileName) throws Exception {
        if (xGraphicProvider == null){
            throw new Exception("XGraphicProvider is null.");
        }

        if (xGraphic == null){
            throw new Exception("XGraphic is null.");
        }

        PropertyValue[] properties = new PropertyValue[2];
        properties[0] = new PropertyValue();
        properties[0].Name = "URL";
        properties[0].Value = fileName;
        properties[1] = new PropertyValue();
        properties[1].Name = "MimeType";
        properties[1].Value = "image/" + fileName.trim().substring(fileName.length() - 3);

        xGraphicProvider.storeGraphic(xGraphic, properties);
    }

    public static void exportAllEmbeddedGraphics(XGraphicProvider xGraphicProvider, XTextDocument xTextDocument, String outDir) throws Exception {
        if (xGraphicProvider == null){
            throw new Exception("XGraphicProvider is null.");
        }

        if (xTextDocument == null){
            throw new Exception("XTextDocument is null.");
        }

        XTextGraphicObjectsSupplier xTextGraphicObjectsSupplier = (XTextGraphicObjectsSupplier) UnoRuntime.queryInterface(
                XTextGraphicObjectsSupplier.class, xTextDocument);

        XNameAccess xNameAccess = xTextGraphicObjectsSupplier.getGraphicObjects();
        if (xNameAccess != null && xNameAccess.hasElements()) {
            String[] names = xNameAccess.getElementNames();

            for (int i = 0; i < names.length; i++) {
                Object oGraphics = xNameAccess.getByName(names[i]);

                XServiceInfo xServiceInfo = (XServiceInfo)UnoRuntime.queryInterface(
                        XServiceInfo.class, oGraphics);
                if (xServiceInfo != null
                        && xServiceInfo.supportsService("com.sun.star.text.TextContent")
                        && xServiceInfo.supportsService("com.sun.star.text.TextGraphicObject")) {

                    XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
                            XPropertySet.class, oGraphics);
                    String url = (String) xPropertySet.getPropertyValue("GraphicURL");

                    PropertyValue[] properties = new PropertyValue[1];
                    properties[0] = new PropertyValue();
                    properties[0].Name = "URL";
                    properties[0].Value = url;

                    XGraphic xGraphic = xGraphicProvider.queryGraphic(properties);
                    if (xGraphic != null){
                        //String fileName = UUID.randomUUID() + ".png";
                        String fileName = names[i] + ".png";
                        System.out.println("Export: " + names[i]);
                        System.out.println("Filename: " + outDir + fileName);

                        exportGraphicObject(xGraphicProvider, xGraphic, outDir + fileName);

                        xGraphic = null;
                    }

                    xServiceInfo = null;
                }

                oGraphics = null;
            }

            names = null;
            xNameAccess = null;
        }

        if (xTextGraphicObjectsSupplier != null)
            xTextGraphicObjectsSupplier = null;
    }

    public static void main(String[] args) throws Exception {
        String fileName = "file:///C:/code/_other/java-OOo/test.odt";
        String outDir = "file:///C:/code/_other/java-OOo/out/";

        XComponentContext xContext = null;
        XMultiComponentFactory xMultiComponentFactory = null;
        Object oDesktop = null;
        XComponentLoader xComponentLoader = null;
        XGraphicProvider xGraphicProvider = null;

        System.out.println("Starting OOo.");
        try {
            xContext = Bootstrap.bootstrap();
        } catch (BootstrapException e) {
            System.out.println("Boostrap failed! Failed to start OpenOffice.");
            return;
        }

        xMultiComponentFactory  = (XMultiComponentFactory) xContext.getServiceManager();
        oDesktop = xMultiComponentFactory.createInstanceWithContext(
                "com.sun.star.frame.Desktop", xContext);
        xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
                com.sun.star.frame.XComponentLoader.class, oDesktop);
        if (xComponentLoader == null) {
            System.out.println("Failed to create XComponentLoader");
            return;
        }

        try {
            Object oGraphicProvider = xMultiComponentFactory.createInstanceWithContext(
                    "com.sun.star.graphic.GraphicProvider", xContext);
            xGraphicProvider = (XGraphicProvider) UnoRuntime.queryInterface(
                    XGraphicProvider.class, oGraphicProvider); 
        } catch (Exception e) {
            System.out.println("Failed to create XGraphicProvider!");
            return;
        }

        PropertyValue[] properties = new PropertyValue[2];
        properties[0] = new PropertyValue();
        properties[0].Name = "Hidden";
        properties[0].Value = false; // put true to hide OpenOffice window
        properties[1] = new PropertyValue();
        properties[1].Name = "CharacterSet";
        properties[1].Value = "Unicode (UTF-8)";

        XComponent document = null;
        try {
            document = xComponentLoader.loadComponentFromURL(fileName, "_blank", 0, properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
                XTextDocument.class, document);
        exportAllEmbeddedGraphics(xGraphicProvider, xTextDocument, outDir);

        XCloseable xCloseable = (XCloseable)UnoRuntime.queryInterface(
                XCloseable.class, xContext);
        if(xCloseable != null)
        {
            xCloseable.close(true);
            xCloseable = null;
        } 

        xGraphicProvider = null;
        xComponentLoader = null;
        oDesktop = null;
        xMultiComponentFactory = null;
        xContext = null;

        try {
            Runtime.getRuntime().exec("tskill soffice");
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } 

        System.out.println("Done.");
    }
}

这篇关于将文档文件中的图像和图形导出到OpenOffice中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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