0

I'm making an extension in java for OpenOffice and I need to do something on each character.

On word I've been able to do it using :

foreach item in doc.characters

Is there something similar in OpenOffice Java API?

Benjamin
  • 5
  • 4

1 Answers1

0

Here is a simple example that counts all characters in a document.

xText = xTextDocument.getText();
XTextCursor xTextCursor = xText.createTextCursorByRange(xText.getStart());
int num_chars = 0;
while (xTextCursor.goRight((short)1, false)) { num_chars++; }
System.out.println("Found " + num_chars + " characters.");

However if there are objects such as tables or frames, then a single text cursor will not work. Using the view cursor handles tables and frames but is much slower. See section 7.14.2 of Andrew Pitonyak's macro document for more complete examples.

Another approach is to enumerate all text content in the document, as in section 7.16.3 of Andrew's document. Related information for Java is also in the Dev guide. Then for each text portion, iterate over it by creating a text cursor, which should be reasonably fast.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • with this code I have the number of word and spaces but not the number of characters, how can I change this? – Benjamin Jul 01 '16 at 13:54
  • When I tested this code, it showed the number of characters as expected. Calling [goRight()](https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextCursor.html#goRight) goes `nCount` characters at a time. Did you perhaps use a [word cursor](https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XWordCursor.html) instead? – Jim K Jul 01 '16 at 18:53
  • I use the exact same code as above (plus the code I want to be done) and it counts the words and spaces instead of characters. – Benjamin Jul 04 '16 at 08:01
  • You could filter the spaces or check whether the character is a space or not. – 476rick Sep 15 '16 at 07:21