将doc文件中的图像和图形导出为openoffice中的图像

Posted

技术标签:

【中文标题】将doc文件中的图像和图形导出为openoffice中的图像【英文标题】:export images and graphics in doc files to images in openoffice 【发布时间】:2011-02-16 09:50:41 【问题描述】:

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

我正在使用 Java,有任何工作示例吗?

谢谢;

【问题讨论】:

【参考方案1】:

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

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

更新: 我将代码转换为 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.");
    

【讨论】:

【参考方案2】:

使用 Apache POI。

这里有一个简短的介绍http://poi.apache.org/hwpf/quick-guide.html

库中有很多例子。

【讨论】:

以上是关于将doc文件中的图像和图形导出为openoffice中的图像的主要内容,如果未能解决你的问题,请参考以下文章

UG NX二次开发(C#)-文件-导出图像

R:如何将绘图/图形导出为 png 或 pdf 文件

导出 PDF 图形,形状轮廓和填充区域不分离

如何将复选框导出到 OpenOffice 中的 .doc 文档?

如何将一些数据导出为 .rtf 或 .doc?

将 Java 2d 图形图像保存为 .png 文件