Centering text

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
RhinoCan
Posts: 80
Joined: Fri Jun 10, 2011 11:36 pm

Centering text

Post by RhinoCan »

I'm writing a Java program to generate a document using the UNO API. The document is a resume and I want the person's name and address, which appear at the top of the resume, to be centered. How do I go about centering text programmatically?

An example would be especially helpful but anything would be useful.
OpenOffice 4.1.2 and Libre Office 5.2.3.3 on Windows 10
FJCC
Moderator
Posts: 9278
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Centering text

Post by FJCC »

To center the first paragraph of a document you can do this (in Basic)

Code: Select all

oText = ThisComponent.getText()
oCurs = oText.createTextCursor()
oCurs.gotoStart(False)
oCurs.Paraadjust = com.sun.star.style.ParagraphAdjust.CENTER
However, wouldn't it be better to load a template that has styles already in place to arrange the document the way you want? Then you could just apply the style and get the font, size, alignment etc in one step.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
RhinoCan
Posts: 80
Joined: Fri Jun 10, 2011 11:36 pm

Re: Centering text

Post by RhinoCan »

I truly don't know if templates would help or just be a complex step that I really don't need in the first place. I haven't found a section on templates yet so I don't know how to create them or customize them. That means I have no idea how much work it is or what complications it introduces.

To me, the idea of saying, "Starting now, I want this next chunk of text to be a table or bulleted list or just a plain paragraph" is nice and simple. I establish the rules for how the next chunk should be formatted, provide the text, then do it all again for the next chunk of text. Creating several styles and then having to navigate back and forth through the document to apply them just seems like extra work offhand.

Then again, I only started working with templates a short time ago when creating documents manually. I'm still not proficient at it and it doesn't feel natural yet. Maybe that's why I'm not that excited by using them for building documents programatically. So maybe it's just a matter of working with them more before I start to see a strong benefit to using them. Some good examples would go a long way to helping me but good examples of Java programs using the UNO API are few and far between. I've got a lot of complaints with the Developer's manual but it seems to be the only thing I have aside from this forum. And it seems like most people on this forum are not using Java, preferring Basic or Javascript or whatever instead.

I'm really starting to think this whole approach is a bad idea. I'd be a lot happier to be using a better documented, more widely used approach. This is feeling like an orphan techology that is never going to get a large user base. Without a really good manual and/or a decent base of fellow users to call upon, development with the UNO API is likely to take a lot longer than using a more popular approach.
OpenOffice 4.1.2 and Libre Office 5.2.3.3 on Windows 10
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Centering text

Post by B Marcelly »

RhinoCan wrote:Some good examples would go a long way to helping me but good examples of Java programs using the UNO API are few and far between. I've got a lot of complaints with the Developer's manual but it seems to be the only thing I have aside from this forum.
The Developer's Guide is the best teacher for you. It contains lots of Java examples. But you won't go anywhere blindly copying examples. You have to read, understand, learn. :mrgreen:
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
RhinoCan
Posts: 80
Joined: Fri Jun 10, 2011 11:36 pm

Re: Centering text

Post by RhinoCan »

Marcelly, it's very patronizing of you to imply that I am just blindly copying examples without thinking about them. That is not the case at all.

As for the Developer's Guide, if that's the best resource on the subject, then I'm not a very happy camper. While the Developer's Guide contains SOME useful information, it leaves a lot to the imagination. Time after time, I've seen sentences that start to tell you something that might be important but then they don't flesh it out with details that you need to do what you're trying to do. The API is also frustrating, particularly for its lack of a master list of all the classes. To find a given class, I need to know what package it belongs to and I'm very new to the API so I have no idea which package might contain a given class.

So thanks for responding but your suggestions are not very helpful.
OpenOffice 4.1.2 and Libre Office 5.2.3.3 on Windows 10
Post Reply