OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Work around found] Paragraph without its numbering (Java)

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Fri Mar 16, 2007 4:30 am    Post subject: [Work around found] Paragraph without its numbering (Java) Reply with quote

Hi,

In Writer, accessed with Java UNO API, I'm trying to get all paragraphs texts. When a paragraph is numbered (with a style), its number is comming with the text, sticked to it. I tried to build an Enumeration on the pragraph, but got only one object, providing with the same text result.

Example :
2The trade mark was registered...
When I would like
The trade mark was registered...

Here is a summary of my code :

Code:

XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, aXText);
XEnumeration xParaEnum = xParaAccess.createEnumeration();
while (xParaEnum.hasMoreElements()) {
  Object aElt = xParaEnum.nextElement();
  XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, aElt);
  if (xInfo.supportsService("com.sun.star.text.TextTable")) {
    continue;
  }
  XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, aElt);
  XTextRange aTextRange = xTextContent.getAnchor();
  String aTxt = aTextRange.getString();
  System.out.println(aTxt);
}


Is there a way to get numbering appart from the text, or only the paragraph text without the numbering ?

Thanks for your help..

Etienne.

PS : it seems to be an un solved trouble for a long time... Tue Nov 16, 2004
http://www.oooforum.org/forum/viewtopic.phtml?t=14265&highlight=paragraph+text+numbering


Last edited by EMLM on Mon Mar 19, 2007 3:14 am; edited 2 times in total
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Fri Mar 16, 2007 5:55 am    Post subject: Reply with quote

I think it is not easy to separate the numbering part from the text. You would have to explore the numbering rules associated to the paragraph (see Dev'Guide chapter 7.4.3).

If you are only interested in reading the text you can change the style of each paragraph (property ParaStyleName) to the basic style "Default" and then read the String of the paragraph. Of course don't save the document after these changes Wink
Back to top
View user's profile Send private message Visit poster's website
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Fri Mar 16, 2007 6:07 am    Post subject: I would find it incredible Reply with quote

Thanks for your quick reply.

I would find it incredible if the OOo's API can't enable to access properly the (text) content of a document in such a case !

I succeeded in finding the presence of a numbering by analysing the paragraph's styles, but then I have no good solution to be sure of what should be filtered from the bad extracted text. If the paragraph numering if of type "A, B, C", should I remove all caps ! It's impossible to do this properly.

Of course, the style of the paragraphe can be changed, but this makes impossible to work properly on a document for a serious application.

Waiting for some other infos..

Cheers,

Etienne.
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Fri Mar 16, 2007 7:09 am    Post subject: Completely stupid ! Reply with quote

I tried to enumerate the paragraph content with a XWordCursor.. The numbering is still fused to the first word !

Code:

XTextCursor aTmpCursor = aXText.createTextCursorByRange(aTextRange);
                  XWordCursor aWordCursor = (XWordCursor)UnoRuntime.queryInterface(XWordCursor.class,aTmpCursor);
                  aWordCursor.collapseToStart();
                  for(int w = 0;w < 5;w++){
                     aWordCursor.gotoStartOfWord(false);
                     aWordCursor.gotoNextWord(true);
                     String aStr = aWordCursor.getString();
                     System.out.println(aStr);
                  }


I got this on a numbered paragraph :
1By
an
application
which
was


In place of :
By
an
application
which
was


It's creasy !

I can understand that when getting the text of a whole paragraph it is providing with its number, but with the Word Cursor, how the hell the word decomposition/enumeration is built from the document text/structure ??
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Fri Mar 16, 2007 7:28 am    Post subject: Reply with quote

It appears that you can do this with a portion enumeration of your main enumeration. I think of the main enumeration as a paragraph enumeration and the portion enumeration as a word enumeration although that's an over simplification as it really picks out any piece that has different properties.

Anyway for just plain numbered paragraphs here's the code in Basic. My little test had the 1st paragraph numbered and the 2nd unnumbered. I'll leave it to you to deal with table text.
Code:
Sub Main
oDoc = thisComponent
pEnum = oDoc.Text.CreateEnumeration
While pEnum.hasMoreElements
 thisP = pEnum.nextElement
 If thisP.NumberingIsNumber = true then
   s = ""
   Print thisP.String '1.First paragraph.
   wEnum = thisP.createEnumeration
   While wEnum.hasMoreElements
    thisW = wEnum.nextElement
    s = s & thisW.String 'String any portions together.
   Wend
   Print s 'First paragraph.
  Else Print thisP.String 'Second paragraph.
 EndIf 
Wend   
End Sub
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Fri Mar 16, 2007 7:37 am    Post subject: Not the solution Reply with quote

Hi,

Thank you for your reply.

If I understand your code in the right way, this was already tested, as said on the first post :
Quote:
... I tried to build an Enumeration on the pragraph, but got only one object, providing with the same text result.


Any other idea ?

Cheers.

Etienne.
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Fri Mar 16, 2007 8:12 am    Post subject: More info Reply with quote

....

In fact, in the enumeration built on the paragraph itself, I get sub-part objects when there are bold, ital, or some other inner structures. With no special inner structure in the paragraph, I get only one object.

In all cases, the paragraph number is sticked to the first Word...
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Sat Mar 17, 2007 3:23 pm    Post subject: Reply with quote

Quote:
In fact, in the enumeration built on the paragraph itself, I get sub-part objects when there are bold, ital, or some other inner structures. With no special inner structure in the paragraph, I get only one object. That's why my code assembles all the portions into 1 string.
That's correct, you get one portion for each bit of text that has any attribute different than the prior portion so if it is all the same then you get only one object.

Did you try my code on your document? I tested it with numbering created by way of the numbering icon and numbering created via Tools > Outline Numbering. For me the text of the paragraph is separate from the numbering in the Print statement with the " 'First paragraph" comment which is in fact the content of this numbered paragraph.
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Mon Mar 19, 2007 1:01 am    Post subject: Your Basic script is OK Reply with quote

JohnV wrote:

Did you try my code on your document?


Your Basic script is OK.

So, now, the question is : why the same kind of code is not working in the same way when written with the java API... (!?)

Thanks for your help.

Cheers.

Etienne.
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Mon Mar 19, 2007 2:56 am    Post subject: Work around Reply with quote

I've found a way to get the good result with a small update in the document : insert a space at the beginning of the numbered paragraph, and remove it after getting the text.

Code:

...
  //Get next para
  Object aElt = xParaEnum.nextElement();

  //Get its text
  XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, aElt);
  XTextRange aTextRange = xTextContent.getAnchor();
  String aTxt = aTextRange.getString();
  System.out.println("Original para (bad when numbered) = " + aTxt);

  //Test for the numbering
  XPropertySet aPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aElt);
  Object xNR = aPropSet.getPropertyValue("NumberingRules");
  XIndexReplace aNR = (XIndexReplace)UnoRuntime.queryInterface(XIndexReplace.class,xNR);
  if(aNR != null){
    System.out.println("Found some numbering on this para");

    //Add a space at the beginning
    XTextRange aStart = aTextRange.getStart();
    aStart.setString(" ");
    aTxt = aTextRange.getString();
    System.out.println("Modified para (good) = " + aTxt);

    aTxt = ((XTextContent) UnoRuntime.queryInterface(XTextContent.class, aElt)).getAnchor().getString();
    System.out.println("Modified para (including the space) = " + aTxt);

    //Now restore original document
    aStart.setString("");
    aTxt = aTextRange.getString();
    System.out.println("Restored para (bad) = " + aTxt);
....


Remark: in this code, as the space is inserted BEFORE the "aTextRange", it is not provided by the getString().. the result is clean. But, when getting the text of the whole modified para, the numbering is also provided back with the inserted space.

Result :

Original para (bad when numbered) = 1By an application which
Found some numbering on this para
Modified para (good) = By an application which
Modified para (including the space) = 1 By an application which
Restored para (bad) = 1By an application which



Cheers,

Etienne.
Back to top
View user's profile Send private message
Danad
OOo Advocate
OOo Advocate


Joined: 22 Feb 2004
Posts: 293
Location: Brasil

PostPosted: Mon Mar 19, 2007 5:27 am    Post subject: Reply with quote

There's an issue on the subject:

http://www.openoffice.org/issues/show_bug.cgi?id=68511

HTH
Back to top
View user's profile Send private message
EMLM
General User
General User


Joined: 16 Mar 2007
Posts: 13

PostPosted: Mon Mar 19, 2007 5:51 am    Post subject: Right Reply with quote

The title of this issue is a bit strange, but yes, after a quick test with the provided document and explanations, I agree it's the same trouble.

What is strange now is that the Basic enumeration script of JohnV seems to work properly, while the same in Java is not working properly... and while the test of the issue also demonstrate the numbering trouble with Basic.
Back to top
View user's profile Send private message
shivakumar6g
General User
General User


Joined: 19 Jan 2011
Posts: 12

PostPosted: Wed Feb 22, 2012 1:46 am    Post subject: I too has similar problem Reply with quote

Hi,

I had a document with the table of contents, some 9 chapters and in that each one has 4 subparts. i want the bullet format for the content in the chapters with tab alignement.
How should i do?
For ex:

1.Information:
(bullet format)Somebody1 taking the information
(bullet format)Somebody2 taking the information
(tab) (bullet format)Somebody3 taking the information
(tab) (bullet format)Somebody4 taking the information
(bullet format)Somebody5 taking the information
Back to top
View user's profile Send private message Send e-mail
shivakumar6g
General User
General User


Joined: 19 Jan 2011
Posts: 12

PostPosted: Tue Feb 28, 2012 9:39 pm    Post subject: Reply with quote

protected void NumberingExample() {
try {
// Go to the end of the document
mxDocCursor.gotoEnd(false);
// Get the RelativeTextContentInsert interface of the document
XRelativeTextContentInsert xRelative = (XRelativeTextContentInsert)
UnoRuntime.queryInterface(XRelativeTextContentInsert.class, mxDocText);

// Use the document's factory to create the NumberingRules service, and get it's
// XIndexAccess interface
XIndexAccess xNum = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,
mxDocFactory.createInstance("com.sun.star.text.NumberingRules"));

// Also get the NumberingRule's XIndexReplace interface
XIndexReplace xReplace = (XIndexReplace) UnoRuntime.queryInterface(
XIndexReplace.class, xNum);

// Create an array of XPropertySets, one for each of the three paragraphs we're about
// to create
XPropertySet xParas[] = new XPropertySet[3];
for (int i = 0 ; i < 3 ; ++ i) {
// Create a new paragraph
XTextContent xNewPara = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class, mxDocFactory.createInstance(
"com.sun.star.text.Paragraph"));

// Get the XPropertySet interface of the new paragraph and put it in our array
xParas[i] = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xNewPara);

// Insert the new paragraph into the document after the fish section. As it is
// an insert
// relative to the fish section, the first paragraph inserted will be below
// the next two
xRelative.insertTextContentAfter (xNewPara, mxFishSection);

// Separate from the above, but also needs to be done three times

// Get the PropertyValue sequence for this numbering level
PropertyValue[] aProps = (PropertyValue []) xNum.getByIndex(i);

// Iterate over the PropertyValue's for this numbering level, looking for the
// 'NumberingType' property
for (int j = 0 ; j < aProps.length ; ++j) {
if (aProps[j].Name.equals ("NumberingType")) {
// Once we find it, set it's value to a new type,
// dependent on which
// numbering level we're currently on
switch ( i ) {
case 0 : aProps[j].Value = new Short(NumberingType.ROMAN_UPPER);
break;
case 1 : aProps[j].Value = new Short(NumberingType.CHARS_UPPER_LETTER);
break;
case 2 : aProps[j].Value = new Short(NumberingType.ARABIC);
break;
}
// Put the updated PropertyValue sequence back into the
// NumberingRules service
xReplace.replaceByIndex (i, aProps);
break;
}
}
}
// Get the XParagraphCursor interface of our text cursor
XParagraphCursor xParaCursor = (XParagraphCursor) UnoRuntime.queryInterface(
XParagraphCursor.class, mxDocCursor);
// Go to the end of the document, then select the preceding paragraphs
mxDocCursor.gotoEnd(false);
xParaCursor.gotoPreviousParagraph false);
xParaCursor.gotoPreviousParagraph true);
xParaCursor.gotoPreviousParagraph true);

// Get the XPropertySet of the cursor's currently selected text
XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, mxDocCursor);

// Set the updated Numbering rules to the cursor's property set
xCursorProps.setPropertyValue ("NumberingRules", xNum);
mxDocCursor.gotoEnd(false);

// Set the first paragraph that was inserted to a numbering level of 2 (thus it will
// have Arabic style numbering)
xParas[0].setPropertyValue ("NumberingLevel", new Short ((short) 2));

// Set the second paragraph that was inserted to a numbering level of 1 (thus it will
// have 'Chars Upper Letter' style numbering)
xParas[1].setPropertyValue ("NumberingLevel", new Short((short) 1));

// Set the third paragraph that was inserted to a numbering level of 0 (thus it will
// have 'Roman Upper' style numbering)
xParas[2].setPropertyValue("NumberingLevel", new Short((short) 0));
} catch (Exception e) {
e.printStackTrace (System.out);
}
}

Trying to execute the above method it is getting error as "Exception in thread "main" com.sun.star.uno.RuntimeException:
at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:177)
at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:143)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:335)
at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:304)
at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:91)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:639)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:151)
at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:133)
at $Proxy13.setPropertyValue(Unknown Source)"

The error is at setting the Numbering to xProps array is at ,.........

// Set the first paragraph that was inserted to a numbering level of 2 (thus it will
// have Arabic style numbering)
xParas[0].setPropertyValue ("NumberingLevel", new Short ((short) 2));

// Set the second paragraph that was inserted to a numbering level of 1 (thus it will
// have 'Chars Upper Letter' style numbering)
xParas[1].setPropertyValue ("NumberingLevel", new Short((short) 1));

// Set the third paragraph that was inserted to a numbering level of 0 (thus it will
// have 'Roman Upper' style numbering)
xParas[2].setPropertyValue("NumberingLevel", new Short((short) 0));
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
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