首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Java和OpenOffice UNO操作odt文件中的嵌入式文件

如何使用Java和OpenOffice UNO操作odt文件中的嵌入式文件
EN

Stack Overflow用户
提问于 2011-03-04 21:06:17
回答 1查看 2.3K关注 0票数 1

我找到了一种将自定义文件存储在ODF文件中的方法,方法是将它们存储在新的资源目录中,并将其包含在新mimetype下的清单中。

我想知道的是如何在我的Java OpenOffice插件中完成以下工作:

  1. 在我的资源中读取了一个文件:是否有可能获得它的InputStreamReader的一个新文件:我可以以某种方式创建并写入一个新文件吗?

关于如何实现这一点的任何想法。我刚接触过UNO,有点困惑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-07 11:37:36

代码语言:javascript
运行
复制
/* this java code need one jar file named with odfdom-java 0.8.6 , and it will able to read any odt file and can store in string and can also change the content of the original file on new or existing file what ever your requirement . if you want to create new file then give new file name and path at the method "document.save("D:/newFile.odt")".
*/

1)--> FileReaderTemp .java

package com.ik.candidate.util;

import java.io.File;

import org.apache.log4j.Logger;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.incubator.search.TextNavigation;
import org.odftoolkit.odfdom.incubator.search.TextSelection;

public class FileReaderTemp {

    public static void main(String[] args) throws Exception {
        parseAll("d:/odt_resume/vrunda_ashar_Rajkot_00.odt");
    }

    public static void parseAll(String fileName) throws Exception {
        Logger logger = Logger.getLogger(FileReaderTemp.class);
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                logger.debug("Sorry File does not Exists!");
            } else if (file.getName().endsWith(".odt")) {
                OpenOfficeParser odt = new OpenOfficeParser();

                //getting contents of odt file in string
                String string = odt.getText(fileName);
                TextNavigation search;
                OdfTextDocument document = (OdfTextDocument) OdfTextDocument.loadDocument(fileName);

                // for replacing Email id
                String emailID="jariya.nilesh99@gmail.com";
            //  String emailID = new FindEmailID().findEmail(string);
                System.out.println("Email is :: " + emailID);
                search = new TextNavigation(emailID, document);
                while (search.hasNext()) {
                System.out.println("Search is : " + search);
            TextSelection item = (TextSelection) search.getCurrentItem();
                item.replaceWith("");
            System.out.println("Email id Removed succesfully :: ");
                    }


                // for replacing contact no
                  String no="9856565698";   
                            //String no = new FindContactNo().findContact(string);
                System.out.println("Contact no is :: " + no);

                    no = no.replace("+", "");
                    // System.out.println("After removed + : " + no);

                    search = new TextNavigation(no, document);
                    // iterate through the search results
                while (search.hasNext()) {
                // System.out.println("Search is No : " + search);
            TextSelection item = (TextSelection) search.getCurrentItem();
                item.replaceWith("");
                        System.out.println("Contact no Removed succesfully :: ");
                    }


                // save the modified document back to a original file other wise it will create new odt file
                document.save(fileName);
            }
        }

        catch (Exception e) {
            //logger.error("Exception : " + e);
            System.out.println("Not found ::");
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5199254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档