Now that we have access to OpenOffice.org's API, it's time to manipulate things a bit. Earlier, I mentioned that there was a service responsible for the current document. This service can be access by the name "com.sun.star.frame.Desktop". Using it, let's play around with the OpenOffice.org Writer document that was created when we started OpenOffice.org. First, we'll have to get the service using the aforementioned createInstanceWithContext method and passing the name of the service and the context: >>> desktop = context.ServiceManager.createInstanceWithContext Good, now we have access to the Desktop service, and the power to load documents and manage the documents that we have loaded. Let's start off by getting the current document, which we can modify. This is done by calling the getCurrentComponent method: >>> document = desktop.getCurrentComponent() Before we do anything interesting with our document, let's create a cursor with which to actually do work. This is quite simple: >>> cursor = document.Text.createTextCursor() Using our cursor, we are able to add some content to our document. Let's start off by adding a simple string of text: >>> document.Text.insertString(cursor, "Adding text is very Upon execution of the above line, you should see a line of text appear in your document. To add a linebreak to the document, simply add in the linebreak character: >>> document.Text.insertString(cursor, "\n\nThis is a new Simple indentation is also fairly easy. It only involves adding an indentation ("\t") character: >>> document.Text.insertString(cursor, "\n\n\tAnd this is another Of course, adding text over and over again isn't that helpful, which is why we are allowed to manipulate the cursor. For example, let's say we wanted a bigger font size. This can be easily done by changing a property of our cursor. UNO objects contain properties, which can be changed by a simple method call. Before we change the font size of our cursor, let's get the current font size: >>> cursor.getPropertyValue("CharHeight") As you can see, the property for font size is called "CharHeight." Using this name, we can change the value of the property by calling another method. Let's change the font size to twenty: >>> cursor.setPropertyValue("CharHeight", 20) Our font size has now been changed, and we are free to add some text in the new size: >>> document.Text.insertString(cursor, "\n\nThis is in size 20.", We're not limited to changing the text size. We can change just about anything. Let's change the font face and the font weight: >>> cursor.setPropertyValue("CharFontName", "Arial")
blog comments powered by Disqus |
|
|
|
|
|
|
|