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

and as mentionned by @JohnSUN : Bin packing problem - Wikipedia

Yes you can change the order of “blocks” in document. Similarly, one more idea came in my mind. After printing and cutting the order sheet our peon used to sort the order sheets yearwise of the case no. means in the given example Case No.: R.C.S. / 77 /2021, Case No.: R.C.S. / 95 / 2021 etc. Is it possible that the Case no. get yearwise sorted in the same document or in the other document?

Is there (or could there be) a block longer than one page → IN THE COURT starts at the start of 1st page but the end of table is at 2nd page?


Let’s try to solve the “unsorted” version here, the sorting you asked Yearwise sort a block of texts - #2 by KamilLanda

The possibility is very rare so we can ignore the condition.

I think I’m starting to understand what problem you’re trying to solve. Correct me if I’m wrong.
So you get a big, very big file with court cases, which should be printed on paper and cut into individual units. Since individual orders often creep from page to page, the corresponding fragments should then be reconnected (glued together?) and at the same time try not to mix up the fragments.
If it is possible to programmatically redistribute all the blocks in the file in such a way that none of the court case would creep onto the next sheet, then firstly the number of cuts would be greatly reduced, and secondly there would be no need to glue the fragments together. This would save time and manual labor. But it would not be possible to greatly reduce the amount of paper used - the blank spaces at the bottom of the page would most likely become smaller, but would not disappear completely.
You still haven’t answered @ajlittoz’ question - how big documents should the macro process? It is important for us to know this in order to choose the right approach to finding a solution. For example, if each such file contains more than a hundred blocks, then recursive enumeration of options should be immediately excluded - for a large number of blocks, the program will simply terminate with the error “Stack overflow”

Yes, we can ignore such situations. But the macro needs to be told exactly what it should do if it encounters such a block. Or if it encounters two such blocks in the file. Or more…

This macro will be used in the entire state of Maharashtra in India everyday. Somedays there might be more than 150 cases on daily board somedays at some places there might be only ten cases. Some people may need to year wise sort the order sheet of the cases and some people will just required that the paper should not be wasted. So I required both types of macros.

Unfortunately, this task cannot be solved - the very fact that the file must be printed on paper means that paper will be wasted. A complete transition to paperless electronic document management can provide real paper savings, but this will not happen today or tomorrow.
The program for redistributing blocks can slightly reduce the amount of manual work on cutting printouts and the consumption of glue, but it will not save paper. Just for example. A file with 150 cases can be printed on 150 sheets, one case per sheet. This will completely eliminate manual work, but will lead to paper waste. If you manage to redistribute the blocks so that each sheet has exactly two cases and one cutting line between them, this will lead to the consumption of 75 sheets of paper and the need to use scissors 75 times. If you leave the file “as is”, all the cases one after another, then it can be printed, for example, on 70 sheets and make 90 cuts and another 20 gluing.
All these numbers are approximate, just to illustrate this idea:

You once said that this file is generated programmatically, you receive it for work already in this form. Perhaps it makes sense to contact the developers of that program so that they change their code and generate the file immediately in the form you need?