[Solved] Get last Writer paragraph using XParagraphCursor

Java, C++, C#, Delphi, ??? - Using the UNO bridges

[Solved] Get last Writer paragraph using XParagraphCursor

Postby othmanelmoulat » Thu Nov 22, 2012 10:44 pm

How can i get the last Writer paragraph text string using the XParagraphCursor ?

in my below code it seems the XParagraphCursor doesn't get effect. it doesn't work and the printed string is empty.

Code: Select all   Expand viewCollapse view
        XText xText = xTextDocument.getText();
        XTextCursor curText = xText.createTextCursor();         
       
        // Position the cursor in the second paragraph
           // Get the XParagraphCursor interface of the document cursor
       
          XParagraphCursor xParaCursor = (XParagraphCursor)
             UnoRuntime.queryInterface( XParagraphCursor.class, curText );
          //curText.gotoEnd ( false );
         xParaCursor.gotoEnd(true);
         xParaCursor.gotoEndOfParagraph( true );
         log.println(xParaCursor.getString());
Last edited by Hagar Delest on Fri Nov 23, 2012 10:11 pm, edited 2 times in total.
Reason: tagged [Solved].
OOo 3.4 and LibreOffice 3.4 on openSuse 11.4 + windows 7
othmanelmoulat
 
Posts: 142
Joined: Sun Aug 03, 2008 4:39 am

Re: get the last Writer paragraph text using XParagraphCurso

Postby FJCC » Fri Nov 23, 2012 12:24 am

I recorded some code using MRI that creates a text cursor, moves to the end and gets the string.
Code: Select all   Expand viewCollapse view
import com.sun.star.text.XSimpleText;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;

static public void snippet(Object oInitialTarget)
{
   XTextDocument xTextDocument = UnoRuntime.queryInterface(
      XTextDocument.class, oInitialTarget);
   XText xText = xTextDocument.getText();
   
   XSimpleText xSimpleText = UnoRuntime.queryInterface(
      XSimpleText.class, xText);
   XTextCursor xTextCursor = xSimpleText.createTextCursor();
   
   xTextCursor.gotoEnd(true);
   
   XTextRange xTextRange = UnoRuntime.queryInterface(
      XTextRange.class, xTextCursor);
   String sString = xTextRange.getString();
   
}

I hope that helps.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7680
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: get the last Writer paragraph text using XParagraphCurso

Postby othmanelmoulat » Fri Nov 23, 2012 12:07 pm

FJCC wrote:I recorded some code using MRI that creates a text cursor, moves to the end and gets the string.
Code: Select all   Expand viewCollapse view
import com.sun.star.text.XSimpleText;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;

static public void snippet(Object oInitialTarget)
{
   XTextDocument xTextDocument = UnoRuntime.queryInterface(
      XTextDocument.class, oInitialTarget);
   XText xText = xTextDocument.getText();
   
   XSimpleText xSimpleText = UnoRuntime.queryInterface(
      XSimpleText.class, xText);
   XTextCursor xTextCursor = xSimpleText.createTextCursor();
   
   xTextCursor.gotoEnd(true);
   
   XTextRange xTextRange = UnoRuntime.queryInterface(
      XTextRange.class, xTextCursor);
   String sString = xTextRange.getString();
   
}

I hope that helps.

Thank you. i tried this code but it seems it returns the whole document text not only the last paragraph text. maybe my document is not well formated? i have attached my odt file. but if i use your code the whole text of the document is returned.
Attachments
Borger Borgersen illustrationsbrev.odt
(31.27 KiB) Downloaded 250 times
OOo 3.4 and LibreOffice 3.4 on openSuse 11.4 + windows 7
othmanelmoulat
 
Posts: 142
Joined: Sun Aug 03, 2008 4:39 am

Re: Get the last Writer paragraph text using XParagraphCurso

Postby B Marcelly » Fri Nov 23, 2012 2:18 pm

Hi,

  1. Go to the end of the text, with expand parameter false.
  2. Then go to the start of the paragraph, with expand parameter true, so the cursor covers the whole paragraph range.
Code: Select all   Expand viewCollapse view
xTextCursor.gotoEnd(false);
xParaCursor.gotoStartOfParagraph(true);
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Get the last Writer paragraph text using XParagraphCurso

Postby othmanelmoulat » Fri Nov 23, 2012 5:41 pm

B Marcelly wrote:Hi,

  1. Go to the end of the text, with expand parameter false.
  2. Then go to the start of the paragraph, with expand parameter true, so the cursor covers the whole paragraph range.
Code: Select all   Expand viewCollapse view
xTextCursor.gotoEnd(false);
xParaCursor.gotoStartOfParagraph(true);

thank you this works perfectly.
I have a question regarding the paragraph cursor and also the PageCursor: do they actually move the document cursor visually? i tested that the PageCursor.jumpToEnd() did have the effect of moving the cursor to the end of document. my question can we have the logic effect of the cursor without actually moving the document cursor visually? so that the user doesn't have the annoyance of a cursor moving without his interaction? in fact i want to get the number of pages in my document and used a PageCursor for that but it has the disadvantage of moving the cursor to end of document which is a nuisance i want to avoid.

thank you
OOo 3.4 and LibreOffice 3.4 on openSuse 11.4 + windows 7
othmanelmoulat
 
Posts: 142
Joined: Sun Aug 03, 2008 4:39 am

Re: Get the last Writer paragraph text using XParagraphCurso

Postby RoryOF » Fri Nov 23, 2012 6:07 pm

Surely you could make the program store the cursor position at the beginning of the operation and revert to that stored position at the end?
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31232
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Get the last Writer paragraph text using XParagraphCurso

Postby othmanelmoulat » Fri Nov 23, 2012 6:22 pm

RoryOF wrote:Surely you could make the program store the cursor position at the beginning of the operation and revert to that stored position at the end?

yes i did that but it stils gives this visual nuisance of a cursor moving forth and back. it is not very perceivable and can live with it. However it would be better to have the feature of navigating the OO document with a logical cursor that doesn't actually moves the cursor visually but only logically so that the programmer can do useful document navigation without the nuisance of a cursor moving visually. a Boolean flag would be sufficient so that it indicates whether to move cursor both logically and visually. This could be a good feature to be added to the OOo API.
other than the cursor api are there any other techniques to navigate the document programatically behind the scene?

thanks
OOo 3.4 and LibreOffice 3.4 on openSuse 11.4 + windows 7
othmanelmoulat
 
Posts: 142
Joined: Sun Aug 03, 2008 4:39 am

Re: Get the last Writer paragraph text using XParagraphCurso

Postby B Marcelly » Fri Nov 23, 2012 6:31 pm

othmanelmoulat wrote:question regarding the paragraph cursor and also the PageCursor: do they actually move the document cursor visually? i tested that the PageCursor.jumpToEnd() did have the effect of moving the cursor to the end of document.

As usual, answers are in the Developer's Guide (but you have to read it again and again).
See page Navigating, first section : Cursors.
Dev'Guide wrote:The text model cursor allows for free navigation over the model by character, words, sentences, or paragraphs. There can be several model cursors at the same time.
(...)
The text view cursor enables the user to travel over the document in the view by character, line, screen page and document page. There is only one text view cursor. Certain information about the current layout, such as the number of lines and page number must be retrieved at the view cursor.

The text model cursor is described in page Editing Text.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Get the last Writer paragraph text using XParagraphCurso

Postby othmanelmoulat » Fri Nov 23, 2012 11:58 pm

B Marcelly wrote:
othmanelmoulat wrote:question regarding the paragraph cursor and also the PageCursor: do they actually move the document cursor visually? i tested that the PageCursor.jumpToEnd() did have the effect of moving the cursor to the end of document.

As usual, answers are in the Developer's Guide (but you have to read it again and again).
See page Navigating, first section : Cursors.
Dev'Guide wrote:The text model cursor allows for free navigation over the model by character, words, sentences, or paragraphs. There can be several model cursors at the same time.
(...)
The text view cursor enables the user to travel over the document in the view by character, line, screen page and document page. There is only one text view cursor. Certain information about the current layout, such as the number of lines and page number must be retrieved at the view cursor.

The text model cursor is described in page Editing Text.

couldn't locate the developers guide section that talks about the text model cursor. they talsk abourt a certain text model cursor but they gave no example if i'm not wrong.. but thanks anyway.
OOo 3.4 and LibreOffice 3.4 on openSuse 11.4 + windows 7
othmanelmoulat
 
Posts: 142
Joined: Sun Aug 03, 2008 4:39 am


Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest