Reputation: 115
Preferable using the ODFDOM API. I would like to have the entire file's contents in a string, if possible. If not, how would you search the file for a specific substring?
Thanks in advance.
Upvotes: 2
Views: 3637
Reputation: 4154
The accepted answer give me an empty String. Here is how I managed to get odf file text content
import java.io.*;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.incubator.doc.text.OdfEditableTextExtractor;
public static String getOdfDocumentText(File file)
{
try (OdfDocument document = OdfDocument.loadDocument(file))
{
OdfEditableTextExtractor extractor = OdfEditableTextExtractor.newOdfEditableTextExtractor(document);
return extractor.getText();
}
catch (Exception exception)
{
System.err.println(exception.getMessage());
}
return "";
}
Upvotes: 0
Reputation: 5064
you will need to load the odt document and then get the content root. From there, get the text content which will return you a string. So that should give you an idea on how to search using string? For example:
TextDocument document = TextDocument.loadDocument("test.odt");
String texts = document.getContentRoot().getTextContent());
Upvotes: 0