Drawing a line in Writer, determining end position later

Hi all!

I am putting together my Writer document and at some point (in time) I create a LineShape (a vertical Line). Then I go on with filling the page with content. When finished I want to correct the end position of my LineShape with the millimeter coordinates of the current cursor position.

The end result with code below looks good, but cLine.setSize(uno.createUnoStruct("com.sun.star.awt.Size", 0, YVALUE))
has to be guessed, and content size may vary.

# Creating the vertical Line
cLine = doc.createInstance("com.sun.star.drawing.LineShape")
cLine.LineStyle = SOLIDLINE
cLine.LineWidth = 10  # Width of the line in 1/100 mm
cLine.LineColor = 0x000000  # RGB color (black)
cLine.setPosition(uno.createUnoStruct("com.sun.star.awt.Point", 15350, 4800))
cLine.setSize(uno.createUnoStruct("com.sun.star.awt.Size", 0, 20500))
cLine.setPropertyValue("AnchorType", AT_PARAGRAPH)
text.insertTextContent(cursor, cLine, False)
doc.CurrentController.select(cLine)
dispatcher.executeDispatch(doc.CurrentController.Frame, ".uno:WrapThroughTransparent", "", 0, ())

I want to achieve something like that:

# Pseudo code
endSize = cursor.getAbsoluteDocumentPositionY() - 4800
cLine.setSize(uno.createUnoStruct("com.sun.star.awt.Size", 0, endSize))

Best ragards,
Robert

I am not versed in Writer macros and consequently don’t understand what you want to achieve. I simply note that your line is anchored at paragraph which suggest you want some some of vertical bar at left or right of paragraph.

Instead of inserting a Drawing Object (all shapes are drawing objects in Writer) which doesn’t interact well with text, you could add a border to your paragraph. Just request a border on the left or right side and the length automatically adjusts to paragraph height. You can also specify the (horizontal) distance to text.

To automate the process, record the border in the paragraph style(s) you format your text with. If you’re smart enough in your styles configuration, you can get a continuous vertical line across different styles. This is much more efficient than macros and operates automatically without the need to retrigger the macros to update the line.

First of all, thank you for your response!
I already have had this approach. Problem is it assumes one paragraph and I have more than one.

A similar approach would be taking a 2-column by 1-row table and make the middle vertical divider line coloured and all the borders colourless. Content may have multiple paragraphs. Problem is, the coloured line starts on the top not in the desired position.

I suggested to customise the paragraph style (PS). A PS is shared by several paragraphs, so there is no problem here. You apply the PS to the first paragraph in the sequence and when you hit Enter, you start a new paragraph with the same PS. If your text already exists, it is even simpler: select everything which should have this style and apply the PS.


Remember that a PS does not boast visual effects, it designates a “*semantic value” (a significance). Therefore, all paragraphs which belong in this significance should be styled the same.

And in case several “significances” need the side bar, customise these other PS.

1 Like

I tested your proposal and accept it as a solution. Thank you again!
A note to myself: programming is not yet done, although default style has to copied as stylewithline and as stylewithoutline. Style has to be chosen first, text entered and formatted afterwards.
EDIT:
I am using direct formatting, no paragraph styles, therefore I figured out the answer to the original question for Drawing Object.

text.insertString(cursor, ' ', 0) # workaround for proper position
cLine.setSize(uno.createUnoStruct("com.sun.star.awt.Size", 0, doc.CurrentController.getViewCursor().getPosition().Y - 4800))

This is not the correct way of using Writer. “Styling” is the founding principle of Writer. With styles, you need macros only in very exceptional circumstances. Styles are very (repeat 7 times) versatile. They provide automatic text look and layout update while macros must re-triggered. Formatting with styles is very neat (and straightforward once you have fully understood the lines) and reliable. You will never forget a change occurrence compared to direct formatting.

I am not saying that I disagreed.
In the meanwhile I gave it a try to reimplement my code with styles this time and I am stuck with what to me appears to be limitation of the styles or maybe a found a bug.
I have a base style derived from “standard” aka Default Paragraph Style.
This base style has a right border line with some padding.
The I derived 2 classes from base like title and content.
After one title paragraph I happen to have several content paragraphs.
Title paragraph has bottom border line as well.
Title and content have spacing below paragraph for better visual separation.
Now after the last content paragraph and the following title there is a gap in right sided border line I can’t get rid of. Everywhere else the right sided border line is continuous as expected.
A matter of detail the title bottom border line is touching the right sided border line, I see no option to have a gap there.

I have now direct styling side by side with proper paragraph styling, I made progress but the result is not there yet.