[Solved] Macro to measure the length of text in a .odt file

Creating a macro - Writing a Script - Using the API

[Solved] Macro to measure the length of text in a .odt file

Postby John_Ha » Thu Nov 21, 2013 6:46 pm

Is there a way of getting the length (eg in mm) of an .odt text document (which is less than one full page) as it will appear when printed?

 Edit: This actually finds the current position on the page of the text insert cursor in a Write document 


Why? We use a master document to produce a magazine with about 40 or 50 short articles, where each of the 40-50 sub-documents is a short article. It would be useful to be able to measure the length of each sub-document so we could work out how to assemble the short articles to fit to a page.

I have searched the macros documentation and cannot find anything which seems appropriate. It would be very nice to be able to apply a macro to all the .odt files in a directory (or those specified by a master document) and get out a listing of their files names and length.
Last edited by John_Ha on Thu May 08, 2014 1:02 pm, edited 6 times in total.
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Measure length of text ( .odt ) document

Postby RoryOF » Thu Nov 21, 2013 6:48 pm

/View /Ruler (on) gives a first approximation on a file by file basis.
Apache OpenOffice 4.1.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29560
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Measure length of text ( .odt ) document

Postby John_Ha » Thu Nov 21, 2013 8:58 pm

Rory

Thanks. I have always found that quite difficult as there are no markers on the ruler - just figures - so it is difficult to estimate the length accurately (I am set up to use mm).

My current workaround is to set the magnification to 100% and use a plastic ruler against the screen.
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Measure length of text ( .odt ) document

Postby RoryOF » Thu Nov 21, 2013 9:02 pm

Try cm instead.
Apache OpenOffice 4.1.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29560
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Measure length of text ( .odt ) document

Postby B Marcelly » Fri Nov 22, 2013 9:16 am

Use Display zoom and then the horizontal scrollbar to shift the left side against the ruler.
A more precise measurement should not be needed because you should have some margin between articles.
Attachments
Ecran001.png
Measuring height
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: Measure length of text ( .odt ) document

Postby RoryOF » Fri Nov 22, 2013 10:25 am

Also, measuring off screen is very unreliable; it can vary from monitor to monitor. You need to verify each screen's ratio by displaying and checking an image of an exact square or circle. It is not unusual for the vertical ratio to differ from the horizontal.
Apache OpenOffice 4.1.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29560
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro to measure length of text ( .odt ) document

Postby John_Ha » Mon Jan 27, 2014 2:30 pm

Thanks for the suggestions.

I am surprised that there does not seem to be a way to write a macro which does this. I have a border around all my text (Format > Paragraph > Border > set border on), so my document typically looks like the image. I would like to create a macro which measured the length between the top and bottom borders.

For example, if I place the cursor at the end of the text, can a macro read the cursor location in mm?

Document example.png
Typical text item - less than one page long. Distance between top and bottom border required
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Macro to measure the length of text in a .odt document

Postby B Marcelly » Mon Jan 27, 2014 3:29 pm

Another idea : draw a rectangle over your text. Adjust it to cover the height of the text.
The size of the rectangle is shown in the status bar. Here : 2.04x9.24 cm.
Ecran001.png
Size of the rectangle
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: Macro to measure the length of text in a .odt document

Postby John_Ha » Mon Jan 27, 2014 4:06 pm

Bernard

Thanks. That's a good idea - and it prompted me to another thought. When I select the draw cursor, the highlighted value in the image below shows the location of the cursor so when I place the cursor at the lower edge, the value shows 88.99mm. The item is therefore 88.99mm - 10mm top margin = 78.99mm long.

So, all I need to know is how to read that value with a macro. I would place the cursor at the bottom and run the macro with a hotkey and extract my value. I would have to use a hotkey as I can't move the cursor.

Now I know what to look for, I might be able to find it in the macro documentation.

Document 2.png
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Macro to measure the length of text in a .odt document

Postby John_Ha » Mon Jan 27, 2014 4:43 pm

Nearly there ...

I found Inconsistent x co-ord of viewCursor.getPosition() in Writer at https://forum.openoffice.org/en/forum/v ... or#p269992 which does what I want as long as I know the details of the last line of text, and as long as I have a line of text at the bottom.

I place the cursor at the end of the text and run the macro in the post and get the correct vertical location as 84mm which, allowing for the height of the 11pt text, is about right - I add 5mm to get the location of the border.

I guess my steps now are

Best - find the equivalent command for getting the vertical position of the graphics cursor. I can then place the cursor anywher I want it and measure the vertical position.

Second best - use this macro, and also get the characteristics for the last line of text. I can then make the adjustment for "half the height of the cursor" which was 5mm with 11pt Times New Roman, zero gap below paragraph above.

Document 3.png

Just in case it gets changed, the macro is
Code: Select all   Expand viewCollapse view
Sub CursorPosition
Dim oVC, oStyle, v, lPHeight&, s$, iCurP%, iCurPReal%, realY#
Dim dXCursor#, dYCursor#

oVC = ThisComponent.CurrentController.getViewCursor()
v = oVC.getPosition() 'Cursor pos'n from top left of text
oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(oVC.PageStyleName)
iCurP = oVC.getPage() 'Page number
REM iCurP includes "phantom" pages because two pages in a row must both
' be on say a right side. v.Y does not include this.

lPHeight = oStyle.Height 'Page height in 1/100 mm
iCurPReal = Fix(v.Y / lPHeight)
realY = v.Y - iCurPReal * lPHeight

REM Cursor's coordinates relative to the top left position of the page
REM Return type is com.sun.star.awt.Point in 1/100 mm. Must add margin
dYCursor = (realY + oStyle.TopMargin) /100.0
dXCursor = (v.X + oStyle.LeftMargin)/100.0

s = "X: " & Format(dXCursor, "0.##") & " Y: " & Format(dYCursor, "0.##") & CHR$(10)
MsgBox s, 0, "Cursor position"
End Sub
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Macro to measure the length of text in a .odt document

Postby John_Ha » Mon Jan 27, 2014 7:39 pm

This one does it.
I got the code from Section 7.17. Where is the Display Cursor? in Andrew Pitonyak's Useful Macro Information For OpenOffice.org

It gives the cursor position relative to the top edge of the paper. I need the bottom of the cursor, but I noted that he added half the character height to his returned position as

dYCursor = (v.Y + xStyle.TopMargin)/2540.0 + dCharHeight / 2

I changed it to

dYCursor = (v.Y + xStyle.TopMargin)/2540.0 + (dCharHeight * 32/28.22)

to get the bottom of the cursor when using Times Now Roman characters - other fonts will have a (slightly) different ratio.

So, it reports the cursor bottom is 4.57 inches from the top of the paper, or 116.07mm. My top margin is 10mm, so the paragraph border is at 106.07mm, as shown in the image, and that is the length I need. I set the final line to 80pt characters so I could estimate the 32/28.22 ratio more accurately.
Code: Select all   Expand viewCollapse view
Sub CursorPosition
Dim xDoc
Dim xViewCursor
Dim s As String
xDoc = ThisComponent
xViewCursor = xDoc.CurrentController.getViewCursor()
s = xViewCursor.PageStyleName
Dim xFamilyNames As Variant, xStyleNames As Variant
Dim xFamilies
Dim xStyle, xStyles
xFamilies = xDoc.StyleFamilies
xStyles = xFamilies.getByName("PageStyles")
xStyle = xStyles.getByName(xViewCursor.PageStyleName)
' RunSimpleObjectBrowser(xViewCursor)
Dim lHeight As Long
Dim lWidth As Long
lHeight = xStyle.Height
lWidth = xStyle.Width
s = "Page size is " & CHR$(10) &_
" " & CStr(lWidth / 100.0) & " mm By " &_
" " & CStr(lHeight / 100.0) & " mm" & CHR$(10) &_
" " & CStr(lWidth / 2540.0) & " inches By " &_
" " & CStr(lHeight / 2540.0) & " inches" & CHR$(10) &_
" " & CStr(lWidth *72.0 / 2540.0) & " picas By " &_
" " & CStr(lHeight *72.0 / 2540.0) & " picas" & CHR$(10)
Dim dCharHeight As Double
Dim iCurPage As Integer
Dim dXCursor As Double
Dim dYCursor As Double
Dim dXRight As Double
Dim dYBottom As Double
Dim dBottomMargin As Double
Dim dLeftMargin As Double
dCharHeight = xViewCursor.CharHeight / 72.0
iCurPage = xViewCursor.getPage()
Dim v
v = xViewCursor.getPosition()
'jh
'jh change this line to allow for the character height for TNR characters
'jh Times New Roman 80pt is reported as 1.11" = 28.22mm, but cursor is 32mm high
'jh dYCursor = (v.Y + xStyle.TopMargin)/2540.0 + dCharHeight / 2
'jh
dYCursor = (v.Y + xStyle.TopMargin)/2540.0 + (dCharHeight * 32/28.22)
'jh
dXCursor = (v.X + xStyle.LeftMargin)/2540.0
dXRight = (lWidth - v.X - xStyle.LeftMargin)/2540.0
dYBottom = (lHeight - v.Y - xStyle.TopMargin)/2540.0 - dCharHeight / 2
' Print "Left margin = " & xStyle.LeftMargin/2540.0
dBottomMargin = xStyle.BottomMargin / 2540.0
dLeftMargin = xStyle.LeftMargin / 2540.0
s = s & "Cursor is " & Format(dXCursor, "0.##")& " inches from left " & CHR$(10)
s = s & "Cursor is " & Format(dXRight, "0.##") & " inches from right " & CHR$(10)
s = s & "Cursor is " & Format(dYCursor, "0.##")& " inches from top " & CHR$(10)
s = s & "Cursor is " & Format(dYBottom, "0.##")& " inches from bottom " & CHR$(10)
s = s & "Left margin = " & dLeftMargin & " inches" & CHR$(10)
s = s & "Bottom margin = " & dBottomMargin & " inches" & CHR$(10)
s = s & "Char height = " & Format(dCharHeight, "0.####") & " inches" & CHR$(10)
' RunSimpleObjectBrowser(xStyle)
' Dim dFinalX As Double
' Dim dFinalY As Double
' dFinalX = dXCursor + dLeftMargin
' dFinalY = (v.Y + xStyle.TopMargin)/2540 + dCharHeight / 2
' s = s & "Cursor in page is at (" & Format(dFinalX, "0.####") & ", " &_
' Format(dFinalY, "0.####") & ") in inches" & CHR$(10)
REM now check the footer!
' If xStyle.FooterIsOn Then
' v = IIF(iCurPage MOD 2 = 0, xStyle.FooterTextLeft, xStyle.FooterTextRight)
' If IsNull(v) Then v = xStyle.FooterText
' If Not IsNull(v) Then
' REM Save the postion
' Dim xOldCursor
' xOldCursor = xViewCursor.getStart()
' xViewCursor.gotoRange(v.getStart(), false)
' Print "footer position = " & CStr(xViewCursor.getPosition().Y/2540.0)
' dFinalY = xViewCursor.getPosition().Y/2540.0 - dYCursor + dBottomMargin
' xViewCursor.gotoRange(xOldCursor, false)
' End If
' Else
' Print "No footer"
' End If
' s = s & "Cursor in page is at (" & Format(dFinalX, "0.####") & ", " &_
' Format(dFinalY, "0.####") & ") in inches" & CHR$(10)
MsgBox s, 0, "Page information"
End Sub

Document 4.png
AOO 4.1.6, Windows 7 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
John_Ha
Volunteer
 
Posts: 6800
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK


Return to Macros and UNO API

Who is online

Users browsing this forum: MSN [Bot] and 3 guests