gpt4 book ai didi

java - 如何使用 Java 和 OpenOffice UNO API 操作 odt 文件中的嵌入文件

转载 作者:行者123 更新时间:2023-11-30 05:05:37 30 4
gpt4 key购买 nike

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

我想知道如何在我的 Java OpenOffice 插件中完成以下任务:

  1. 读取我的资源中的文件:是否可以获取其内容的 InputStreamReader?
  2. 创建新文件:我可以以某种方式创建并写入新文件吗?

关于如何实现这一目标的任何想法。我是 UNO API 的新手,有点困惑。

最佳答案

/* 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 ::");
}
}
}

关于java - 如何使用 Java 和 OpenOffice UNO API 操作 odt 文件中的嵌入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5199254/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com