Inserting text into presentation - java

Discuss the presentation application

Inserting text into presentation - java

Postby mattaspen » Thu May 22, 2008 12:37 am

Hi,

I've been trying to find a way to insert text into a presentation page.

I have a working example which creates several presentation pages. On one, it creates a rectangle ... What I want to do, is to insert text (Arial, 24pt font, bold) on a page as well. From the documentation, it appears that I have to crate a shape (ie. my rectangle), get the text for this object via
Code: Select all   Expand viewCollapse view
XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, lastAddedShape);

then
Code: Select all   Expand viewCollapse view
xShapeText.setString("<SF Name> Build X.X Public API Changes");
.
Finally, set the font using something link this (not 'something' b/c I can't get it to work)
Code: Select all   Expand viewCollapse view
XTextCursor xTextCursor = xShapeText.createTextCursor();
XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
xCursorProps.setPropertyValue("CharFontName", "Arial");


As I mentioned, this doesn't work. If there is an easier way or if you can help me fix what I have above, I'd greatly appreciate it.

Thanks,
--MA
mattaspen
 
Posts: 5
Joined: Thu Apr 17, 2008 5:53 pm

Re: Inserting text into presentation - java

Postby Villeroy » Thu May 22, 2008 1:37 pm

No, I can't see an error. I would operate on prepared templates with styles. The only thing you've got to do then is assigning the right style to the right object. Of course this is not an option when your code runs against arbitrary documents.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28359
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Inserting text into presentation - java

Postby mattaspen » Thu May 22, 2008 4:56 pm

To answer my own question (in hopes that it will help others :) )

To reiterate, I wanted to know how, in Java, to insert text with specific characteristics.

1. open a new presentation (impress document); "xDrawDoc"
2. add some pages to it
3. select page 1 (index 0); "xPage"
4. query the shapes for that page; "xShapes"
4. create a TextShape
Code: Select all   Expand viewCollapse view
XShape textShape = ShapeHelper.createShape(xDrawDoc, new Point( 1400, 1197 ), new Size( 25256, 2037 ), "com.sun.star.drawing.TextShape");


NOTE: one interesting thing about the "new Point( 1400, 1197 )" & "new Size( 25256, 2037 )":
In an existing presentation, I got the position and size of the text "area" that I wanted to replicate.

There measurements were:
x = 0.55"
y = 0.47"
w = 9.92"
h = 0.80"

To convert this to 1/1000 th of millimeter:
1" ~= 2564 mm

So, these values become:
x1 = 2564 * 0.55 ~= 1400
y1 = 2564 * 0.47 ~= 1197
w1 = 2564 * 9.92 ~= 25256
h1 = 2564 * 0.80 ~= 2037



5. add it to the page's shapes
Code: Select all   Expand viewCollapse view
xShapes.add(textShape);

6. create a new XText object
Code: Select all   Expand viewCollapse view
XText textForTextShape = (XText)UnoRuntime.queryInterface(XText.class, textShape);

7. create text cursor for selecting and formatting
Code: Select all   Expand viewCollapse view
XTextCursor xTextCursor = textForTextShape.createTextCursor();

8. get cursor's property set
Code: Select all   Expand viewCollapse view
XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);

9. set some font parameters
Code: Select all   Expand viewCollapse view
xCursorProps.setPropertyValue("CharWeight", new Float ( com.sun.star.awt.FontWeight.BOLD));
xCursorProps.setPropertyValue("CharHeight", new Float ( 20 ));
xCursorProps.setPropertyValue("CharFontName", "Arial");


And, Voila!
mattaspen
 
Posts: 5
Joined: Thu Apr 17, 2008 5:53 pm


Return to Impress

Who is online

Users browsing this forum: No registered users and 1 guest