合并代码中的两个.odt文件 [英] Merging two .odt files from code

查看:99
本文介绍了合并代码中的两个.odt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何合并两个.odt文件?手动执行此操作,打开每个文件并复制内容是可以的,但这是不可行的.

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

我已经尝试使用odttoolkit Simple API(simple-odf-0.8.1-incubating)来完成该任务,创建一个空的TextDocument并将所有内容合并到其中:

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);
}

而且效果还算不错,但是它会释放字体信息-原始文件是Arial Narrow和Windings的组合(用于复选框),输出masterFile全部包含在TimesNewRoman中.起初,我怀疑insertContentFromDocumentAfter的最后一个参数,但是将其更改为false会破坏(几乎)所有格式.难道我做错了什么?还有其他办法吗?

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?

推荐答案

Ma情况很简单,我要合并的文件以相同的方式生成并使用相同的基本格式.因此,从我的一个文件而不是一个空文档开始,解决了我的问题.

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.

但是,在有人提出格式化保留的更一般的解决方案之前(可能基于ngulams答案和评论?),这个问题将一直存在.

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?).

这篇关于合并代码中的两个.odt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆