Geertjan's Blog

  • June 21, 2006

OpenOffice.org API (Part 2)

Geertjan Wielenga
Product Manager
This is what my report looks like right now (same u.i. as before, so no point in including a screenshot of that; just look in yesterday's blog entry—the whole report below, without any post-processing, is generated when the "Report" button in my NetBeans Platform-based application is clicked):

I'm finding the OpenOffice.org 1.1 Developer's Guide worth its weight in gold (and since it comes at 1047 pages, that's probably a fair amount of gold). A lot of the text formatting of the "Contacts Report" title is extrapolated from page 55 of the Developer's Guide. To learn about tables, with good code samples including a very good line-by-line explanation, start at page 490. Halfway down page 473 is a table entitled "Properties of com.sun.star.style.CharacterProperties", which is very useful (and would be even more useful if the properties were listed alphabetically).

Here's a little look into how the "Contacts Report" title (shown in the screenshot above) is constructed. What allows us to format text is the cursor—as you move it along the text, you can tell it how to format the text it encounters.

//First, create an object that represents the entire text:
com.sun.star.text.XText xText = xTextDocument.getText();
//Next, create a text cursor (there are various other types too) for selecting and formatting:
XTextCursor xTCursor = xText.createTextCursor();
//Next, create a property set to store all the properties you're going to set:
XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTCursor);
//Select 15 characters, which is the length of "Contacts Report":
xTCursor.goRight((short)15, true);
//Now, set Italics...
xCursorProps.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC);
xCursorProps.setPropertyValue("CharWeight", new Float(com.sun.star.awt.FontWeight.BOLD));
xCursorProps.setPropertyValue("CharHeight", 20);
xCursorProps.setPropertyValue("CharColor", 255);
//...cool shadowy effect thingy...
xCursorProps.setPropertyValue("CharShadowed", true);
//Finally, add the text itself, adding some tabs for indentation and a linebreak at the end:
xText.setString("\\t\\t\\t\\t\\tContacts Report\\n");

One annoying thing is that CharColor uses ARGB notation. Or just a plain number. I mean, until I trawled through the Developer's Guide, I had no idea that the color blue is represented by the number 255 (and white is 16777215, while black is 65536). I'd much prefer it if I could use this instead:

xCursorProps.setPropertyValue("CharColor", BLUE);

Another unfortunate thing is that, as you can see, all the properties are identified as Strings, so code completion doesn't help at all. You can't put your cursor, in the IDE, after Char in the line above and then call up code completion when you're thinking about other properties to use. (That's where the table referred to earlier comes in handy, because code completion doesn't.) For example, I expected there to be a property called CharSize, and spent about half an hour discovering that it is called CharHeight instead.

And, hurray, as you can see, I managed to get the whole JTable from my NetBeans Platform application into the table in the OpenOffice.org Writer document! Yesterday I only showed one column because... that's the only part of the table I managed to get into the Writer document. The way I got the whole table in there seems a bit convoluted, which is something else I need to work on.

Join the discussion

Comments ( 1 )
  • Wouter van Reeven Wednesday, June 21, 2006
    Hey Geertjan,
    The ARGB setting is not as hard as you'd might think. To set the color to White, just do xPropertySet.setPropertyValue("CharColor", new Integer(0x00ffffff)) and to make it blue, do xPropertySet.setPropertyValue("CharColor", new Integer(0x000000ff)).
    So, the R, G and B values are hex values ranging from 0 to ff (which is 255 in decimal). Simply find out the R, G and B value of the color you'd like to have using PhotoShop or Gimp and tansfer those to hex. Not as easy as BLUE but not as complicated as you think.
    Greets, Wouter
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.