在 MS-Word 2010 中,文件 -> 信息下有一个选项,用于在共享文档之前检查文档是否存在问题。这使得处理跟踪更改(到新的最新版本)并立即从文档中删除所有评论和注释成为可能。
这种可能性在 docx4j 中是否也可用,还是我需要研究相应的 JAXB 对象并编写遍历查找器?手动执行此操作可能需要大量工作,因为我必须将RunIns
( w:ins
) 添加到R
( w:r
) 并删除RunDel
( w:del
)。我还看到了一个w:del
曾经在里面的w:ins
。在这种情况下,我不知道反之亦然还是出现在更深的嵌套中。
进一步的研究提出了这个 XSLT: https ://github.com/plutext/docx4all/blob/master/docx4all/src/main/java/org/docx4all/util/ApplyRemoteChanges.xslt 我无法在 docx4j 中运行它,但是通过手动解压缩 docx 并提取 document.xml。在普通 document.xml 上应用 xslt 后,我再次将其包装在 docx 容器中,以使用 MS-Word 打开它。结果与接受 MS-Word 本身的修订不同。更具体:XSLT 删除了已删除的标记文本(在表格中),但没有删除文本前的列表点。这在我的文档中经常出现。
如果无法以简单的方式解决此请求,我将更改约束。我有一个方法来获取 ContentAccessor 的所有文本就足够了,作为String
. ContentAccessor 可以是 aP
或Tc
。字符串应该在里面R
或里面RunIns
(R
里面有那个)为此我在下面有一个半解决方案。有趣的部分从else if (child instanceof RunIns) {
. 但如上所述,我不确定嵌套的 del/ins 语句会如何出现,以及这是否能很好地处理它们。结果还是和以前用 MS-Word 准备文档不一样。
//Similar to:
//http://www.docx4java.org/forums/docx-java-f6/how-to-get-all-text-element-of-a-paragraph-with-docx4j-t2028.html
private String getAllTextfromParagraph(ContentAccessor ca) {
String result = "";
List<Object> children = ca.getContent();
for (Object child : children) {
child = XmlUtils.unwrap(child);
if (child instanceof Text) {
Text text = (Text) child;
result += text.getValue();
} else if (child instanceof R) {
R run = (R) child;
result += getTextFromRun(run);
}
else if (child instanceof RunIns) {
RunIns ins = (RunIns) child;
for (Object obj : ins.getCustomXmlOrSmartTagOrSdt()) {
if (obj instanceof R) {
result += getTextFromRun((R) obj);
}
}
}
}
return result.trim();
}
private String getTextFromRun(R run) {
String result = "";
for (Object o : run.getContent()) {
o = XmlUtils.unwrap(o);
if (o instanceof R.Tab) {
Text text = new Text();
text.setValue("\t");
result += text.getValue();
}
if (o instanceof R.SoftHyphen) {
Text text = new Text();
text.setValue("\u00AD");
result += text.getValue();
}
if (o instanceof Br) {
Text text = new Text();
text.setValue(" ");
result += text.getValue();
}
if (o instanceof Text) {
result += ((Text) o).getValue();
}
}
return result;
}