英文:
How to insert a docx into another docx's pre-determined row
问题
我的目标是将一个docx(保留样式/格式)插入到另一个docx的特定行中。在第二个docx中有一个单词,“placeholder”,首先我必须找到这个单词,然后将其更改为第一个docx的文本,保持插入的docx样式和格式不变。
我有一个想法。也许我应该创建一个新的docx,使用“placeholder”将第二个docx分成两部分,将第一部分放入新的docx,然后放入整个docx,最后放入第二个docx的第二部分。但是如何保持样式和格式呢?我没有图像/表格或其他东西,只有文本和格式化内容,如列表、制表符、文本样式等。
目前我正在使用apache POI和Java。(我尝试过docx4j,但成功较少。)示例代码只做了简单的合并,没有更多操作。如何找到“placeholder”单词并将我的docx插入其中?
以下是您提供的代码的部分翻译:
public static void merge(InputStream src1, InputStream src2, OutputStream dest) throws Exception {
OPCPackage src1Package = OPCPackage.open(src1);
OPCPackage src2Package = OPCPackage.open(src2);
XWPFDocument src1Document = new XWPFDocument(src1Package);
CTBody src1Body = src1Document.getDocument().getBody();
XWPFDocument src2Document = new XWPFDocument(src2Package);
CTBody src2Body = src2Document.getDocument().getBody();
appendBody(src1Body, src2Body);
src1Document.write(dest);
}
private static void appendBody(CTBody src, CTBody append) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String suffix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + suffix);
src.set(makeBody);
}
请注意,以上翻译的代码仅为您提供的代码的一部分。如果您需要进一步的翻译或有其他问题,请随时提出。
英文:
My goal is to insert a docx (with keeping the style / formatting) into another docx's specific row. In the second docx there is a word, "placeholder" and first, I have to find this word, and then change it to first docx text, keeping the inserted docx styles and formats.
I have an idea. Maybe I should create a new docx, divide the second docx with the "placeholder", put the first part to the new docx, then put the whole docx, and then put the second part of the second docx. But how can I keep the styles and formats? I don't have images / tablets or anything, just texts and formatting stuff, like lists, tabs, text style, etc.
Currently I am using apache POI and java. (I tried docx4j, but I had less success)
The example code does a simple merging but nothing more. How can I find the "placeholder" word and insert my docx there?
public static void merge(InputStream src1, InputStream src2, OutputStream dest) throws Exception {
OPCPackage src1Package = OPCPackage.open(src1);
OPCPackage src2Package = OPCPackage.open(src2);
XWPFDocument src1Document = new XWPFDocument(src1Package);
CTBody src1Body = src1Document.getDocument().getBody();
XWPFDocument src2Document = new XWPFDocument(src2Package);
CTBody src2Body = src2Document.getDocument().getBody();
appendBody(src1Body, src2Body);
src1Document.write(dest);
}
private static void appendBody(CTBody src, CTBody append) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String suffix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + suffix);
src.set(makeBody);
}
答案1
得分: 0
关于docx4j,您可以使用我们的商业产品Docx4j Enterprise中的MergeDocx在特定位置(例如在表格单元格中)插入docx文件。
您可以从https://www.plutext.com/m/index.php/products 获取试用版本,然后查看MergeIntoTableCell示例和文档。
英文:
Re docx4j you can insert a docx at a specific location (eg in a table cell) using MergeDocx in our commercial Docx4j Enterprise.
You can get a trial version from https://www.plutext.com/m/index.php/products
Then see the MergeIntoTableCell sample and documentation.
答案2
得分: 0
另一种解决方案是:在我的示例中的mainPart
中,我们可以使用indexOf
、lastIndexOf
或substring
来查找文本(比使用正则表达式更好),然后将文本添加(并替换文本为)addPart
,然后就可以继续了。
可能存在两个问题:
- 1:如果在
addPart
中有编号列表/项目符号列表,那么在添加到其他文档后可能会混乱。 - 2:以这种方式无法插入图片,必须以其他方式处理。
英文:
Other solution is: in my example in mainPart
, we can find the text (using indexof / lastindexof / substring are better, than using regex) and add (and replace the text to) the addPart
and ready to go.
2 possible problem:
- 1: if we have numbered lists / bulleted lists in addPart, that can be be messy after adding to the other document.
- 2: inserting picture is not possible in this way, it has to be handle in other way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论