如何将一个docx文件插入到另一个docx文件的预定行中

huangapple go评论116阅读模式
英文:

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(&quot;&gt;&quot;) + 1);
    String mainPart = srcString.substring(srcString.indexOf(&quot;&gt;&quot;) + 1, srcString.lastIndexOf(&quot;&lt;&quot;));
    String suffix = srcString.substring(srcString.lastIndexOf(&quot;&lt;&quot;));
    String addPart = appendString.substring(appendString.indexOf(&quot;&gt;&quot;) + 1, appendString.lastIndexOf(&quot;&lt;&quot;));
    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中,我们可以使用indexOflastIndexOfsubstring来查找文本(比使用正则表达式更好),然后将文本添加(并替换文本为)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.

huangapple
  • 本文由 发表于 2020年8月7日 22:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63303409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定