Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two .odt files from code

How do you merge two .odt files? Doing that by hand, opening each file and copying the content would work, but is unfeasable.

I have tried odttoolkit Simple API (simple-odf-0.8.1-incubating) to achieve that task, creating an empty TextDocument and merging everything into it:

private File masterFile = new File(...);

...

TextDocument t = TextDocument.newTextDocument();
t.save(masterFile);

...

for(File f : filesToMerge){
   joinOdt(f);
}

...

void joinOdt(File joinee){
   TextDocument master = (TextDocument) TextDocument.loadDocument(masterFile);
   TextDocument slave = (TextDocument) TextDocument.loadDocument(joinee);
   master.insertContentFromDocumentAfter(slave, master.getParagraphByReverseIndex(0, false), true);
   master.save(masterFile);
}

And that works reasonably well, however it looses information about fonts - original files are a combination of Arial Narrow and Windings (for check boxes), output masterFile is all in TimesNewRoman. At first I suspected last parameter of insertContentFromDocumentAfter, but changing it to false breaks (almost) all formatting. Am I doing something wrong? Is there any other way?

like image 541
Deltharis Avatar asked Aug 08 '14 09:08

Deltharis


People also ask

How do I compare two ODT files?

Open the edited document that you want to compare with the original document. Select Edit > Compare Document. An open document dialog appears. Select the original document and click Insert.

How do I repair a corrupted ODT file?

A simple trick to do if ODT file is corrupted Not everyone knows about this, but by opening a blank document and selecting the Insert tab on the taskbar, then File, you can get back a damaged file.

Is ODT editable?

The OpenDocument Text Document Format (ODT), Version 1.2 (given the short name ODF_text_1_2 here) is a format for editable textual documents.

Can Microsoft Word read .ODT files?

OpenDocument (. odt) files are compatible with Word and open source applications like OpenOffice and LibreOffice, but you might see formatting differences and some Word features aren't available in . odt files.


2 Answers

I think this is "works as designed".

I tried this once with a global document, which imports documents and display them as is... as long as paragraph styles have different names !

Using same named templates are overwritten with the values the "master" document have.

So I ended up cloning standard styles with unique (per document) names.

HTH

like image 105
ngulam Avatar answered Sep 28 '22 04:09

ngulam


Ma case was a rather simple one, files I wanted to merge were generated the same way and used the same basic formatting. Therefore, starting off of one of my files, instead of an empty document fixed my problem.

However this question will remain open until someone comes up with a more general solution to formatting retention (possibly based on ngulams answer and comments?).

like image 24
Deltharis Avatar answered Sep 28 '22 04:09

Deltharis