General

Components

Community

Development

TDF

ODFDOM - Search and Replace API - Changing Document Content

The ODF Toolkit provides an easy to use API to modify an existing Text or a Spreadsheet document. The following sections will explain the core concepts and how to use this API.

Text Document

Changing the content of a Text Document you first need to identify the part of a document you want to modify. To search a specific part in a Text document you use the Navigation Interface which allows to navigate through a document with a java Iterator.

The TextNavigation

The class TextNavigation allows you to search for a text phrase and navigate through all occurrences in a document. The following example searches for all text phrases Open Document Format

    // Load an existing text document from local file
    OdfDocument odt = OdfDocument.loadDocument("MyFilename.odt");
    TextNavigation search = new TextNavigation("Open Document Format", doc);
    // Navigate through the document....
    while (search.hasNext()) {
        TextSelection selection = search.next();
        ....
    }

Regular Expressions

The TextNavigation also supports regular expressions. The following example searches for all words starting with an upper case letter:

    // Load an existing text document from local file
    OdfDocument odt = OdfDocument.loadDocument("MyFilename.odt");
    // Find all words starting with Uppercase
    TextNavigation search = new TextNavigation("([A-Z])\\w+", doc);
    while (search.hasNext()) {
        TextSelection selection = search.next();
        ....
    }

The TextSelection

The TextNavigation iterates through a document and returns instances of TextSelection. A TextSelection represents the selection of a document matching the search phrase. The Selection class provides useful methods to modify the corresponding part in the document.

The following example replaces the text phrase Open Document Format with the shortcut ODF:

    // Load an existing text document from local file
    OdfDocument odt = OdfDocument.loadDocument("MyFilename.odt");
    TextNavigation search = new TextNavigation("Open Document Format", doc);
    // Navigate through the document....
    while (search.hasNext()) {
        TextSelection selection = search.next();
        // replace selection with 'ODF'
        selection.replaceWith("ODF");
    }

If you want to delete the text selection you can call the method cut():

    while (search.hasNext()) {
        TextSelection selection = search.next();
        // remove the selection
        selection.cut()
    }

Apply a Style

Another way to modify a part of a text document is to just apply a new style to a text selection. The following example applies the font weight 'bold' to all occurrences of "Open Document Format"

    // create a new style....
    OdfStyle styleBold = new OdfStyle(contentDOM);
    styleBold.setProperty(StyleTextPropertiesElement.FontWeight, "bold");
    styleBold.setStyleFamilyAttribute("text");
    // bold all occurrences of "Open Document Format"
    TextNavigation search = new TextNavigation("Open Document Format", doc);
    while (search.hasNext()) {
       TextSelection selection = search.next();
       selection.applyStyle(styleBold);
    }

Insert a TextSelection

The method pasteAtEndOf allows you to append a part of a text document at th end of a another part of this document. This can be useful if you want ot duplicate or move a text selection within the document.

The following example first finds the text selection 'is cool' and appends this selection to all occurrences of 'ODF Toolkit':

    // find text selection 'is cool'
    TextNavigation searchIsCool = new TextNavigation("is cool", doc);
    if (searchIsCool.hasNext()) {
        // get first selection
        TextSelection isCoolSelection = searchToolkit.next();
        // next paste this selection after each 'ODF Toolkit'
        TextNavigation searchToolkit = new TextNavigation("ODF Toolkit", doc);
        while (search.hasNext()) {
            TextSelection selection = searchToolkit.next();
            isCoolSelection.pasteAtEndOf(selection);
        }
    }

If you want to paste a selection at front of another selection use the method pasteAtFrontOf.

Spreadsheet Document

Changing the content of a Spreadsheet Document is much easier as you can identify cells of a spreadsheet by its coordinates.

The following example finds the cell 'A10' and replaces the content with the number 100.

    // Load an existing text document from local file
    OdfDocument odt = OdfDocument.loadDocument("MyFilename.ods");
	// get first table sheet of the current document...
	OdfTable tbl = doc.getTableList(true).get(0);
    // select cell..
	OdfTableCell cell = tbl.getCellByPosition("A10");
	// update the cell value
    cell.setStringValue(100);

Recalculate Formulas

The ODF Toolkit is a lightweight Java library to create, read and update the data of ODF documents. Unlike other approaches, which rely on runtime manipulation of heavy-weight editors via an automation interface, you can only update the content of the (xml)document. But you have no access to any application interface like LibreOffice. So in case of a SpreadSheet document you can not call a method to recalculate a formula.

In case that you update cell values that affect a formula in a SpreadSheet document, you have to recalculate the results of your changes manually and update the corresponding cells before you save the document.

Note: If you update spreadSheet cells with the ODFToolkit by recalculating its values in the backend, your original formulas will not be changed. In this way changes, made later in an editor like LibreOffice or Collabora, are automatically recalculated by your origin formulas.

Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.