| View previous topic :: View next topic |
| Author |
Message |
Asus Newbie

Joined: 23 Jan 2012 Posts: 2
|
Posted: Mon Jan 23, 2012 1:32 pm Post subject: [Java] problem to put text in bold |
|
|
Hello,
I have a small problem to change the style of the text of the document that I create. I try to put some sentences in bold. If I put a sentence in bold with my function "write", all words in the document are also in bold ...
I show you my code:
| Code: |
private void createDocument(Courbe c,App app) throws Exception {
if (context == null) {
try {
context = BootstrapSocketConnector.bootstrap("C:\\Users\\Thomas\\workspace\\Lac\\Library\\Open\\program\\soffice.exe");
} catch (BootstrapException e) {
throw new java.lang.Exception(e);
}
}
XMultiComponentFactory serviceManager = context.getServiceManager();
Object desktop = serviceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context);
XComponentLoader aLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
xDocument = aLoader.loadComponentFromURL("private:factory/swriter","_blank", 0, new PropertyValue[ 0 ] );
xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xDocument);
...
...
XTextRange xTextRange = xTextDocument.getText().getStart();
xTextRange = write("\nEn gras\n",xTextRange,true);
//insertion of the table CC
insererCC(c,app,xTextRange);
xTextRange = write("\nEt une image pour voir :\n",xTextRange,false);
//insertion image of the curve
insererImage(app.sauverImg(false));
}
|
And the method "write" which I use to write in the document ...
| Code: |
private XTextRange write(String message, XTextRange xTextRange, boolean bold)
{
XText xText = xTextDocument.getText();
xText.insertString(xTextRange, message, bold);
XWordCursor xWordCursor =
(com.sun.star.text.XWordCursor)UnoRuntime.queryInterface(
com.sun.star.text.XWordCursor.class, xText.getStart());
xWordCursor.gotoNextWord(bold);
xWordCursor.gotoEnd(bold);
com.sun.star.beans.XPropertySet xPropertySet =
(com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
com.sun.star.beans.XPropertySet.class, xWordCursor );
try {
if(bold)
xPropertySet.setPropertyValue("CharWeight",new Float( com.sun.star.awt.FontWeight.BOLD ));
else
xPropertySet.setPropertyValue("CharWeight",new Float( com.sun.star.awt.FontWeight.NORMAL ));
} catch (Exception e) {
}
return xWordCursor.getEnd();
}
|
So if you see the error, or if you have an example workable and allows to vary the style between bold and normal, it could help me.
Thanks in advance.
Cordially. |
|
| Back to top |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1414 Location: France
|
Posted: Tue Jan 24, 2012 3:20 am Post subject: |
|
|
Hi,
RTFM !
Editing Text _________________ Bernard
OpenOffice.org 1.1.5 fr / OpenOffice.org 3.4.1 en-US + langpacks, MS-Windows XP Home SP3
This forum is unusable, use instead Apache OpenOffice forums |
|
| Back to top |
|
 |
Asus Newbie

Joined: 23 Jan 2012 Posts: 2
|
Posted: Tue Jan 24, 2012 11:36 am Post subject: |
|
|
Thanks, but that does not help, my problem was mainly at the alternating bold / normal.
I found a solution by myself:
| Code: |
private void write(String message,boolean bold)
{
XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextRange);
try {
if(bold)
xCursorProps.setPropertyValue("CharWeight", new Float(com.sun.star.awt.FontWeight.BOLD));
else
xCursorProps.setPropertyValue("CharWeight", new Float(com.sun.star.awt.FontWeight.NORMAL));
} catch (Exception e) {
}
xText.insertString(xWordCursor,message, true);
String[] mess = message.trim().split(" ");
for(int i=0; i < mess.length+1 ; ++i)
{
xWordCursor.gotoNextWord(false);
}
xWordCursor.gotoEndOfWord(false);
}
private void jumpLine()
{
xText.insertString(xWordCursor,"\n", true);
xWordCursor.goRight((short) 1,false);
}
Post closed. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|