How to merge documents in LibreOffice Writer?

How can I merge documents in LibreOffice Writer in such a way that the formatting of the individual documents is maintained (headers, footers, page orientation, margins etc.)?

What is a good forum to ask questions about LibreOffice Writer?

The only way to avoid conflicts with Style names is to export each document to PDF and merge the pdf documents using a tool such as PDFtk.

I wonder if the end result would be as easy to read as a uniformly styled document?

Do you mean “not as bad as this one”?

Short answer: You can’t
A more detailed answer describing what’s possible would depend on more details given in the question.

Text documents can generally be extremely complicated. There may be endnotes, indexes, page numbering, caption sequences,and a lot more things not easily mergeable. A more general main problem, however, are name conflicts regarding the named styles, and incompatible settings.

The concatenation of plain text pieces with a bit of direct formatting is obvious, of course. Changes in page orientation and Hedader/Footer always depend on special page breaks starting a new sequence. Between the two “merged” parts such a break may be necessary.

Maybe by Copy - Paste.

Thank you very much for taking the time to answer my question, Lupp!

Let me make some clarifications:

The purpose of the merging is to create one big document, Preview.odt, that can be used as a preview document of the source when translating many subdocuments. BTW: Most of the time these will be .docx documents.

Since these subdocuments are created by the same client, it is very likely that the styles will have the same settings. So they won’t be a problem.

Automatically generated Tables of Contents can be skipped completely, they are of no relevance to the translation process (the items will be presented in the “body text”).

Endnotes: these need to be translated, but the numbering isn’t important in the Preview.odt, same for caption sequences and page numbers.

For Indexes the same argument is valid as for TOCs: the ref fields need to be translated.

Keep in mind that the translator will translate the original .docx files and that the Preview.odt document “just” needs to give an exact representation as possible, but doesn’t need to be perfect.

And you are right, I should have mentioned all that before, if I just had been aware of all these aspects :).

So, the big question, can such a merging of subdocuments be automated?

The automation concern wasn’t mentioned at all in the original question, but there was a related question recently, and I considered the idea of merging Writer documents automatically (by “macro”). My result: Much too complicated if not extreme restrictions can be made due to assurances.
Your case is further complicated by the alien source file format.
To get a single file providing the desired preview effect I would consider to export every contributing document to pdf, and to merge the resulting files with a pdf merger like " PDF Split & Merge". I don’t know if this can work with “hybrid” pdf files.
Please note: The actual solution is outside my scope.

1 Like

It depends on what you call “automation”.

The main problem is your documents are .docx which will bring their lot of problems. If you don’t care for formatting (you only translate contents) and the merged document is a one-shot dependable one, you can try to create a master document and add all the subdocuments one after the other. You can then print the master.

I wrote print but this could be a PDF file as most printing drivers can output to a file.

However, I wouldn’t do that if you intend to hand over a separate translation for each original document. In the master, all endnotes will be merged together in single part at end of composite document; similarly, all indexes will be merged. This (intended for a master) behaviour will complicate your translation process.

1 Like

Thank you Wolfgang!
I already assumed that PDF should be the route. I’ll exploit this further and communicate it with the developer of the translation tool I’m using.

Would it theoretically be possible to rewrite this macro for LO’s Basic?

Option Explicit
Dim wdDocTgt As Document, strTgt As String
Sub CombineDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, StrFile As String
Dim wdDocSrc As Document, HdFt As HeaderFooter
Set wdDocTgt = ActiveDocument: strTgt = ActiveDocument.FullName
strFolder = "/Users/hl/Documents/merge"

StrFile = Dir(strFolder & "/*.odt", vbNormal)
While StrFile <> ""
  If strFolder & StrFile <> strTgt Then
    Set wdDocSrc = Documents.Open(FileName:=strFolder & "/" & StrFile, AddToRecentFiles:=False, Visible:=False)
    With wdDocTgt
      .Characters.Last.InsertBefore vbCr
      .Characters.Last.InsertBreak (wdSectionBreakNextPage)
      With .Sections.Last
        For Each HdFt In .Headers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
            .PageNumbers.RestartNumberingAtSection = True
            .PageNumbers.StartingNumber = wdDocSrc.Sections.First.Headers(HdFt.Index).PageNumbers.StartingNumber
          End With
        Next
        For Each HdFt In .Footers
          With HdFt
            .LinkToPrevious = False
            .Range.Text = vbNullString
            .PageNumbers.RestartNumberingAtSection = True
            .PageNumbers.StartingNumber = wdDocSrc.Sections.First.Headers(HdFt.Index).PageNumbers.StartingNumber
          End With
        Next
      End With
      Call LayoutTransfer(wdDocTgt, wdDocSrc)
      .Range.Characters.Last.FormattedText = wdDocSrc.Range.FormattedText
      With .Sections.Last
        For Each HdFt In .Headers
          With HdFt
            .Range.FormattedText = wdDocSrc.Sections.Last.Headers(.Index).Range.FormattedText
            .Range.Characters.Last.Delete
          End With
        Next
        For Each HdFt In .Footers
          With HdFt
            .Range.FormattedText = wdDocSrc.Sections.Last.Footers(.Index).Range.FormattedText
            .Range.Characters.Last.Delete
          End With
        Next
      End With
    End With
    wdDocSrc.Close SaveChanges:=False
  End If
  StrFile = Dir()
Wend
Set wdDocSrc = Nothing
Application.ScreenUpdating = True
End Sub

Sub LayoutTransfer(wdDocTgt As Document, wdDocSrc As Document)
Dim sPageHght As Single, sPageWdth As Single
Dim sHeaderDist As Single, sFooterDist As Single
Dim sTMargin As Single, sBMargin As Single
Dim sLMargin As Single, sRMargin As Single
Dim sGutter As Single, sGutterPos As Single
Dim lPaperSize As Long, lGutterStyle As Long
Dim lMirrorMargins As Long, lVerticalAlignment As Long
Dim lScnStart As Long, lScnDir As Long
Dim lOddEvenHdFt As Long, lDiffFirstHdFt As Long
Dim bTwoPagesOnOne As Boolean, bBkFldPrnt As Boolean
Dim bBkFldPrnShts As Boolean, bBkFldRevPrnt As Boolean
Dim lOrientation As Long
With wdDocSrc.Sections.Last.PageSetup
  lPaperSize = .PaperSize
  lGutterStyle = .GutterStyle
  lOrientation = .Orientation
  lMirrorMargins = .MirrorMargins
  lScnStart = .SectionStart
  lScnDir = .SectionDirection
  lOddEvenHdFt = .OddAndEvenPagesHeaderFooter
  lDiffFirstHdFt = .DifferentFirstPageHeaderFooter
  lVerticalAlignment = .VerticalAlignment
  sPageHght = .PageHeight
  sPageWdth = .PageWidth
  sTMargin = .TopMargin
  sBMargin = .BottomMargin
  sLMargin = .LeftMargin
  sRMargin = .RightMargin
  sGutter = .Gutter
  sGutterPos = .GutterPos
  sHeaderDist = .HeaderDistance
  sFooterDist = .FooterDistance
  bTwoPagesOnOne = .TwoPagesOnOne
'  bBkFldPrnt = .BookFoldPrinting
'  bBkFldPrnShts = .BookFoldPrintingSheets
'  bBkFldRevPrnt = .BookFoldRevPrinting
End With
With wdDocTgt.Sections.Last.PageSetup
  .GutterStyle = lGutterStyle
  .MirrorMargins = lMirrorMargins
  .SectionStart = lScnStart
  .SectionDirection = lScnDir
  .OddAndEvenPagesHeaderFooter = lOddEvenHdFt
  .DifferentFirstPageHeaderFooter = lDiffFirstHdFt
  .VerticalAlignment = lVerticalAlignment
  .PageHeight = sPageHght
  .PageWidth = sPageWdth
  .TopMargin = sTMargin
  .BottomMargin = sBMargin
  .LeftMargin = sLMargin
  .RightMargin = sRMargin
  .Gutter = sGutter
  .GutterPos = sGutterPos
  .HeaderDistance = sHeaderDist
  .FooterDistance = sFooterDist
  .TwoPagesOnOne = bTwoPagesOnOne
' .BookFoldPrinting = bBkFldPrnt
'  .BookFoldPrintingSheets = bBkFldPrnShts
'  .BookFoldRevPrinting = bBkFldRevPrnt
  .PaperSize = lPaperSize
  .Orientation = lOrientation
End With
End Sub