LibreOffice Writer Macro to search for blank lines at the bottom of a page and paste there another text from remaining document below the blank line by cutting it

I came back here to irritate @karolus and @KamilLanda. I think this will be a more complex macro and it will need more IQ and smartness.

I am using Ubuntu 22.04 and Version: 24.2.6.2 (X86_64)

/ LibreOffice Community
Build ID: ef66aa7e36a1bb8e65bfbc63aba53045a14d0871
CPU threads: 20; OS: Linux 6.8; UI render: default; VCL: gtk3
Locale: en-IN (en_IN); UI: en-US
Calc: threaded

In the attached document file, from IN THE COURT OF to the very next table there is order sheet of one court case. In the given example the first order sheet on second page is bigger in size so it is not adjusted on the first page. But the next order sheet can be adjusted on the first page. So I want to know is it possible to create a macro by running it, it will search for number of blank lines on the page and cut and paste the order sheet of a court case which is smaller in size than the blank lines. The order sheet might be anywhere in the document below the blank lines.

After filling the first page if there is blank space below another pages it will again search for order sheets of court cases which is smaller in size than the blank space on the pages.

(Please note this macro will be used by thousands of people everyday to save time and pages.) Presently the part of blank pages got wasted.

Exp.odt (41.8 KB)

I uploaded the desired copy. I am facing another problem which I asked in another question that when I download a file from our software, since LibreOffice 22 it is giving an error and I have to save the file twice. Anyways in this file blank space can be identified at the bottom of first page and the block is from IN THE COURT OF till first complete table.

I got the message the file is corrupt but probably Libre repaired the ODT.
corrupt

Small question is: Is the empty line between the end of table and the dashed line necessary? Or is it possible to delete one?

And the main question: Is it possible to change the order of “blocks” in document? The block is from IN THE COURT OF to the end of DASHED LINE.
Because it is possible to get the “size” of each block with the coordinates of visible cursor.

Sub coordinateY 'show the Y-coordinate of visible cursor
	dim oDoc as object, oVCur as object
	oDoc=ThisComponent
	oVCur=oDoc.CurrentController.ViewCursor
	msgbox oVCur.Position.Y
End Sub

And then try to swap the blocks to maximally minimize the count of pages. Albeit I don’t know now the algorithm to get the optional combinations for the page from the queque of block-sizes.

A few remarks:

  • Writer is not page-oriented
    Consequently it is pointless to count “blank lines at bottom of page”, unless your document is entirely direst-formatted, i.e. you don’t rely on automatic detection of end of page and you fill up your partial pages with empty paragraphs to “align” with next page boundary.
  • trying to stuff things for maximal density leads to “combinatorial explosion”
    Assuming you can compute the height of all your “block”, trying to find an ordering which minimises “wasted” space is at best a polynomial complexity algorithm but I suspect it is NP (non-polynomial), probably akin to the travelling salesman problem, which has no known optimal solution.

So, if you decide to try and implement something, note that your macros don’t execute as native computer instructions; they go through an interpreter. LO Basic is not intended for advanced mathematical problems. So expect a very poor performance.

In the end, is it worth the effort?

You should try to enable built-in text flow attributes in your styles. With a smart usage of Keep with next paragraph, you could already minimise the number of split cases (but this perhaps results in more pages which contradicts your goal).

We still have no idea about the “waste”. How many pages are there in total? What is the percentage of recoverable bottom space? Below 20% I wouldn’t care. And if you print only 10 pages, I wouldn’t care at all.

What is the cost of printer paper in you country? Processing the document to maximise density requires computer time. During this time, your clerk probably does not do anything else. What is the corresponding clerk cost? How does it compare to paper cost?

1 Like

Knapsack problem - Wikipedia