[Solved] [Writer] Get text range coordinates

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

[Solved] [Writer] Get text range coordinates

Post by UbuntuUser »

Hello all,

I would like to get the position(x,y) and size(width,height) of a text range. I have tried and calculating it to the view cursor's position but, to no avail.

Any solutions or help would be greatly appreciated!

Edit:
I am using writer not calc.
Last edited by UbuntuUser on Fri May 26, 2017 1:34 am, edited 1 time in total.
OpenOffice 4, Ubuntu 14.04
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Basic] Get text range coordinates

Post by Zizi64 »

I would like to get the position(x,y) and size(width,height) of a text range. I have tried and calculating it to the view cursor's position but, to no avail.

I am using writer not calc.
Is it a textbox or other graphis object with some text?
Or: is it a simple text in some paragraphs?
Last edited by Zizi64 on Thu May 25, 2017 11:44 am, edited 1 time in total.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: [Basic] Get text range coordinates

Post by UbuntuUser »

Simple text within a paragraph.
OpenOffice 4, Ubuntu 14.04
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Basic] Get text range coordinates

Post by Zizi64 »

Sorry, I can not help you.

You can get the size and the position of the graphic objects by macros.
viewtopic.php?f=25&t=82919
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] Get text range coordinates

Post by RoryOF »

I don't think you can do this - the text range is embedded in the flow and layout of the text of the document.

It might be helpful if you told us why you needed to know the exact position and dimensions of a text range. What do you want to do with this?
 Edit: Also, a text range might be of uneven form - a partial line, some full lines, then a partial line. Supposing that it was possible to define the X,Y position and dimensions of such a text range, what about the text on the remainder of the partial lines, which does not form part of the defined range? 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: [Basic] Get text range coordinates

Post by UbuntuUser »

I would like to display text frames over sets of underscores.

I need the dimensions so I can automatically specify the width and height of the frame. Of course, using this method I would have to reposition the frames if the text flow changed, but I would be okay with that.

If I added a hyperlink to the said text, could I get the coordinates for the hypertext? I know pdf files store coordinates for annotations.
OpenOffice 4, Ubuntu 14.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] Get text range coordinates

Post by Villeroy »

What's wrong with annotations (aka comments)?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] Get text range coordinates

Post by RoryOF »

I use annotations/comments from various pseudo authors (such as Revise, Source?, Check) throughout my documents for steering and editing information.

One can apply an annotation to a selected text range: be advised, this does not seem to be very stable, as many corrupt files I have rescued seem to use this construct and it seems often to be the cause of the file corruption.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: [Basic] Get text range coordinates

Post by UbuntuUser »

I'll look into annotations. I was after a way to easily automate text field entry where 3 or more underscore are. If I overlay the new input text into a text frame it cannot effect the text formatting underneath. Which is ideal.

I am basically trying to replace a simple underline with a text form field that can be easily updated programatically.
OpenOffice 4, Ubuntu 14.04
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] Get text range coordinates

Post by RoryOF »

I'm trying to tease this out: you have underlined text of multiple words in your document. You wish to identify such underlined multiple word phrases by finding the inter-word underscore characters? Having found such phrases, you then replace them - with what? A modification of the found text would be a candidate for a Regular Expression Find and Replace; if your replacement text is new text, input by a user. then the process could be carried out by a BASIC macro (I hope Villeroy is not reading this - the word BASIC is not good for his blood pressure!), where the input text is acquired by an Input Box (or some other method) in the BASIC of the macro.

Some code for searching a document is given in
Searching for Text Portions
The accompanying text will give suggestions on how to replace the found text and update the file.
 Edit: A Find and Replace can find underscored text, without need to identify it by finding inter-word underscores. 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: [Basic] Get text range coordinates

Post by UbuntuUser »

Thanks for the search and replace tips. That will come in handy.

I'm only looking for underscores "_". Authors often use them to represent text entry fields in printed documents. I'm trying to convert underscores into form fields.

I don't think I will be able to do this within open office, but knowing that has helped a lot so thank you!
OpenOffice 4, Ubuntu 14.04
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Basic] Get text range coordinates

Post by JeJe »

If you anchor the text frame to a character you have the x,y sorted?

You can get the height of text from its point size.

If there's no other way, you could use a hidden dialog to get the width and height of some text by setting a label to it and using getpreferedsize.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
UbuntuUser
Posts: 17
Joined: Tue Mar 22, 2016 1:25 pm

Re: [Basic] Get text range coordinates

Post by UbuntuUser »

Thanks for the info JeJe. Anchoring to the character didn't work for me. The frame wouldn't stay strictly next to the text.

I'll keep your tips in mind for future reference.
OpenOffice 4, Ubuntu 14.04
Post Reply