Leonidas A
Leonidas A

Reputation: 359

How to manipulate embedded file in odt file using Java and OpenOffice UNO API

I have found a way to store custom files inside ODF files by storing them in a new resource directory and including it in the manifest under a new mimetype.

What I want to know is how to accomplish the following in my Java OpenOffice add-on:

  1. Read a file in my resource: Is it possible to get an InputStreamReader of its contents?
  2. Create a new file: Can I somehow create and write out to a new file?

Any ideas on how to accomplish this. I am new to UNO API and kind of confused.

Upvotes: 1

Views: 2585

Answers (1)

Jariya Nilesh
Jariya Nilesh

Reputation: 66

/* 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="[email protected]";
            //  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 ::");
        }
    }
}

Upvotes: 2

Related Questions