0

Is there any way or example how to embed file\object in odt using Open\LibreOffice API for java ?

Or with some other API's or languages.

1 Answer 1

1

Here's a snippet of it in action:

    public static void main(String[] args) {
    try {
        OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/test.ods"));
        OdfFileDom odfContent = odfDoc.getContentDom();
        XPath xpath = odfDoc.getXPath();
        DTMNodeList nodeList = (DTMNodeList) xpath.evaluate("//table:table-row/table:table-cell[1]", odfContent, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node cell = nodeList.item(i);
            if (!cell.getTextContent().isEmpty()) {
                System.out.println(cell.getTextContent());
            }
        }
    } catch (Exception ex) {
        //Handle...
    }
}

Let's assume that the 'test.ods' file above has this content: enter image description here

From the above, the code listing would print the following:

Cuthbert
Algernon
Wilbert

And, as a second example, here's me reading the first paragraph of an OpenOffice Text document:

public static void main(String[] args) {
    try {
        OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/chapter2.odt"));
        OdfFileDom odfContent = odfDoc.getContentDom();
        XPath xpath = odfDoc.getXPath();
        OdfParagraphElement para = (OdfParagraphElement) xpath.evaluate("//text:p[1]", odfContent, XPathConstants.NODE);
        System.out.println(para.getFirstChild().getNodeValue());
    } catch (Exception ex) {
        //Handle...
    }
}

On my classpath I have "odfdom.jar" and "xerces-2.8.0.jar".

Sign up to request clarification or add additional context in comments.

1 Comment

Mmm, thanks. Example is about how to insert text and read it. But not hiw to attach file to document, like OLE Objects in Word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.