Help Needed: Creating Multipage Document with Form-Based Textboxes in LibreOffice Writer

I am working on a project that involves automating the creation of a multipage document in LibreOffice Writer using macros. Here are the details of what I need help with:

  1. Objective:
  • I want to create a multipage document where each page contains a form-based textbox. I also need to insert text from a text file into each of these textboxes.
  1. Requirements:
  • Number of Pages: The user should specify the total number of pages (which will be an even number).
  • Text File: The text file will contain lines of text that should be inserted into the text boxes on each page. Each line of text should go into a separate textbox.
  • Form-Based Textboxes: Each page should have a form-based textbox, and all textboxes should be created in a single form (if possible).
  1. Desired Outcome:
  • I would like a working macro that can:
    • Create the specified number of pages.
    • Add a form-based textbox to each page.
    • Insert the text from the text file into each corresponding textbox.
    • Ensure that textboxes are correctly positioned and formatted.

Sub CreateMultipageDocumentWithTextboxes()
Dim oDoc As Object
Dim oText As Object
Dim oTextCursor As Object
Dim oDrawPage As Object
Dim oForms As Object
Dim oForm As Object
Dim oTextBox As Object
Dim iNumPages As Integer
Dim i As Integer
Dim sTextFile As String
Dim sLine As String
Dim oTextInputStream As Object

' Get the active document
oDoc = ThisComponent
oText = oDoc.getText()
oTextCursor = oText.createTextCursor()

' Prompt user for input parameters
iNumPages = CInt(InputBox("Enter the number of pages:", "Number of Pages"))
sTextFile = InputBox("Enter the path to the text file:", "Text File Path")

' Validate inputs
If iNumPages < 1 Then
    MsgBox "The number of pages must be at least 1."
    Exit Sub
End If

If Dir(sTextFile) = "" Then
    MsgBox "Text file not found."
    Exit Sub
End If

' Open the text file for reading
oTextInputStream = CreateUnoService("com.sun.star.io.TextInputStream")
oTextInputStream.InputStream = CreateUnoService("com.sun.star.ucb.SimpleFileAccess").openFileRead(ConvertToURL(sTextFile))

' Access the DrawPage (drawing layer)
oDrawPage = oDoc.getDrawPage()

' Create the FormComponents container if not already present
If oDrawPage.getForms().getCount() = 0 Then
    oForms = oDrawPage.createInstance("com.sun.star.form.FormComponents")
    oDrawPage.Forms = oForms
Else
    oForms = oDrawPage.getForms()
End If

' Create one form and add it to FormComponents if not already created
If oForms.getCount() = 0 Then
    oForm = oForms.createInstance("com.sun.star.form.component.Form")
    oForms.insertByIndex(0, oForm)
Else
    oForm = oForms.getByIndex(0)
End If

' Loop to create pages and insert textboxes with text
For i = 1 To iNumPages
    ' Insert a page break after the first page
    If i > 1 Then
        oText.insertControlCharacter(oTextCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
        oTextCursor.BreakType = com.sun.star.style.BreakType.PAGE_BEFORE
    End If
    
    ' Create a new TextBox on the form for this page
    oTextBox = oForm.createInstance("com.sun.star.form.component.TextField")
    oTextBox.Name = "txt_Title_" & i ' Unique name for each textbox
    oTextBox.PositionX = 5000 ' X position (adjust as needed)
    oTextBox.PositionY = 5000 ' Y position (adjust as needed)
    oTextBox.Width = 10000 ' Width of the textbox
    oTextBox.Height = 1000 ' Height of the textbox
    
    ' Read the next line from the text file
    If Not oTextInputStream.isEOF() Then
        sLine = oTextInputStream.readLine()
    Else
        sLine = ""
    End If
    
    ' Set the line of text into the TextBox
    oTextBox.Text = sLine
    
    ' Add the TextBox to the form
    oForm.insertByIndex(oForm.getCount(), oTextBox)
Next i

' Close the input stream
oTextInputStream.closeInput()

MsgBox "Document created successfully with " & iNumPages & " pages and textboxes."

End Sub

Thinking aloud.

Writer is not an adequate tool to create page-oriented documents. It manages a “flow” which is then broken into pages as needed by the text (you can’t usually predict where page breaks will occur).

Text boxes (outside forms) are almost always a bad idea because they don’t cooperate well with text flow (but apparently, there won’t be any text flow in your final document).

You have described the final usage of the document. If everything stops once text is inserted, have you considered Mail Merge which may be easier to implement than your macros? Macros main purpose is to work around the document management philosophy implied by the ODF model. So is the choice of Writer some external constraint? Other applications/format could be better fit to the task, like HTML (with Django), DTP (desktop publishing) Scribus which is page-oriented, …