我有一些占位符的docx文档.现在我应该用其他内容替换它们并保存新的docx文档.我从docx4j开始,发现了这个方法:
public static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
public static void findAndReplace(WordprocessingMLPackage doc, String toFind, String replacer){
List<Object> paragraphs = getAllElementFromObject(doc.getMainDocumentPart(), P.class);
for(Object par : paragraphs){
P p = (P) par;
List<Object> texts = getAllElementFromObject(p, Text.class);
for(Object text : texts){
Text t = (Text)text;
if(t.getValue().contains(toFind)){
t.setValue(t.getValue().replace(toFind, replacer));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这只是很少工作,因为通常占位符分裂多个文本运行.
我尝试过UnmarshallFromTemplate但它也很少工作.
这个问题怎么解决?
你可以用VariableReplace它来实现在其他答案时可能不存在的这个.这不是查找/替换本身,但适用于占位符,例如$(myField)
java.util.HashMap mappings = new java.util.HashMap();
VariablePrepare.prepare(wordMLPackage);//see notes
mappings.put("myField", "foo");
wordMLPackage.getMainDocumentPart().variableReplace(mappings);
Run Code Online (Sandbox Code Playgroud)
请注意,您不传递$(myField)作为字段名称; 相反,传递未转义的字段名称myField- 这是相当不灵活的,因为它目前支持你的占位符必须是格式,$(xyz)而如果你可以传入任何东西,那么你可以使用它进行任何查找/替换.在docx4j.NET中,C#人员也可以使用它
见这里获取更多信息的VariableReplace或在此进行VariablePrepare
我创建了一个库来发布我的解决方案,因为它有很多代码:https ://github.com/phip1611/docx4j-search-and-replace-util
工作流程如下:
第一步:
// (this method was part of your question)
List<Text> texts = getAllElementFromObject(docxDocument.getMainDocumentPart(), Text.class);
Run Code Online (Sandbox Code Playgroud)
这样我们就能以正确的顺序获得所有实际的文本内容,但中间没有样式标记。我们可以编辑文本对象(通过 setValue)并保留样式。
结果问题:搜索文本/占位符可以拆分为多个文本实例(因为原始文档中可能存在不可见的样式标记),例如${FOOBAR}、${ + FOOBAR}或$ + {FOOB+AR}
第二步:
将所有文本对象连接为完整字符串/“完整字符串”
Optional<String> completeStringOpt = texts.stream().map(Text::getValue).reduce(String::concat);
Run Code Online (Sandbox Code Playgroud)
第三步:
创建一个类TextMetaItem。每个 TextMetaItem 都知道它的文本对象,其内容在完整字符串中开始和结束。例如,如果“foo ”和“bar”的文本对象产生完整的字符串“foobar”,则索引0-2属于"foo"-Text-object和。建个3-5"bar"-Text-objectList<TextMetaItem>
static List<TextMetaItem> buildMetaItemList(List<Text> texts) {
final int[] index = {0};
final int[] iteration = {0};
List<TextMetaItem> list = new ArrayList<>();
texts.forEach(text -> {
int length = text.getValue().length();
list.add(new TextMetaItem(index[0], index[0] + length - 1, text, iteration[0]));
index[0] += length;
iteration[0]++;
});
return list;
}
Run Code Online (Sandbox Code Playgroud)
第四步:
构建一个Map<Integer, TextMetaItem>,其中键是完整字符串中的索引/字符。这意味着地图的长度等于completeString.length()
static Map<Integer, TextMetaItem> buildStringIndicesToTextMetaItemMap(List<Text> texts) {
List<TextMetaItem> metaItemList = buildMetaItemList(texts);
Map<Integer, TextMetaItem> map = new TreeMap<>();
int currentStringIndicesToTextIndex = 0;
// + 1 important here!
int max = metaItemList.get(metaItemList.size() - 1).getEnd() + 1;
for (int i = 0; i < max; i++) {
TextMetaItem currentTextMetaItem = metaItemList.get(currentStringIndicesToTextIndex);
map.put(i, currentTextMetaItem);
if (i >= currentTextMetaItem.getEnd()) {
currentStringIndicesToTextIndex++;
}
}
return map;
}
Run Code Online (Sandbox Code Playgroud)
中期结果:
现在您有足够的元数据来将您想要对完整字符串执行的每个操作委托给相应的 Text 对象!(要更改文本对象的内容,您只需调用 (#setValue())这就是 Docx4J 中编辑文本所需的全部内容。所有样式信息等都将被保留!
最后一步:搜索并替换
构建一个方法来查找所有可能出现的占位符。您应该创建一个类FoundResult(int start, int end),将找到的值(占位符)的开始和结束索引存储在完整的字符串中
public static List<FoundResult> findAllOccurrencesInString(String data, String search) {
List<FoundResult> list = new ArrayList<>();
String remaining = data;
int totalIndex = 0;
while (true) {
int index = remaining.indexOf(search);
if (index == -1) {
break;
}
int throwAwayCharCount = index + search.length();
remaining = remaining.substring(throwAwayCharCount);
list.add(new FoundResult(totalIndex + index, search));
totalIndex += throwAwayCharCount;
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
使用它我构建了一个新的 s 列表ReplaceCommand。AReplaceCommand是一个简单的类,存储 aFoundResult和新值。
接下来,您必须从最后一项到第一项对该列表进行排序(按完整字符串中的位置排序)
现在您可以编写替换所有算法,因为您知道需要对哪个文本对象执行什么操作。我们做了(2),以便替换操作不会使其他FoundResults 的索引无效。
3.1.) 找到需要更改的文本对象 3.2.) 对它们调用 getValue() 3.3.) 将字符串编辑为新值 3.4.) 对文本对象调用 setValue()
这是发挥所有魔力的代码。它执行单个 ReplaceCommand。
/**
* @param texts All Text-objects
* @param replaceCommand Command
* @param map Lookup-Map from index in complete string to TextMetaItem
*/
public static void executeReplaceCommand(List<Text> texts, ReplaceCommand replaceCommand, Map<Integer, TextMetaItem> map) {
TextMetaItem tmi1 = map.get(replaceCommand.getFoundResult().getStart());
TextMetaItem tmi2 = map.get(replaceCommand.getFoundResult().getEnd());
if (tmi2.getPosition() - tmi1.getPosition() > 0) {
// it can happen that text objects are in-between
// we can remove them (set to null)
int upperBorder = tmi2.getPosition();
int lowerBorder = tmi1.getPosition() + 1;
for (int i = lowerBorder; i < upperBorder; i++) {
texts.get(i).setValue(null);
}
}
if (tmi1.getPosition() == tmi2.getPosition()) {
// do replacement inside a single Text-object
String t1 = tmi1.getText().getValue();
int beginIndex = tmi1.getPositionInsideTextObject(replaceCommand.getFoundResult().getStart());
int endIndex = tmi2.getPositionInsideTextObject(replaceCommand.getFoundResult().getEnd());
String keepBefore = t1.substring(0, beginIndex);
String keepAfter = t1.substring(endIndex + 1);
tmi1.getText().setValue(keepBefore + replaceCommand.getNewValue() + keepAfter);
} else {
// do replacement across two Text-objects
// check where to start and replace
// the Text-objects value inside both Text-objects
String t1 = tmi1.getText().getValue();
String t2 = tmi2.getText().getValue();
int beginIndex = tmi1.getPositionInsideTextObject(replaceCommand.getFoundResult().getStart());
int endIndex = tmi2.getPositionInsideTextObject(replaceCommand.getFoundResult().getEnd());
t1 = t1.substring(0, beginIndex);
t1 = t1.concat(replaceCommand.getNewValue());
t2 = t2.substring(endIndex + 1);
tmi1.getText().setValue(t1);
tmi2.getText().setValue(t2);
}
}
Run Code Online (Sandbox Code Playgroud)
美好的一天,我做了一个例子,如何通过正则表达式快速将文本替换为您需要的内容。我找到 ${param.sumname} 并将其替换到文档中。请注意,您必须将文本插入为“仅文本”!玩得开心!
WordprocessingMLPackage mlp = WordprocessingMLPackage.load(new File("filepath"));
replaceText(mlp.getMainDocumentPart());
static void replaceText(ContentAccessor c)
throws Exception
{
for (Object p: c.getContent())
{
if (p instanceof ContentAccessor)
replaceText((ContentAccessor) p);
else if (p instanceof JAXBElement)
{
Object v = ((JAXBElement) p).getValue();
if (v instanceof ContentAccessor)
replaceText((ContentAccessor) v);
else if (v instanceof org.docx4j.wml.Text)
{
org.docx4j.wml.Text t = (org.docx4j.wml.Text) v;
String text = t.getValue();
if (text != null)
{
t.setSpace("preserve"); // needed?
t.setValue(replaceParams(text));
}
}
}
}
}
static Pattern paramPatern = Pattern.compile("(?i)(\\$\\{([\\w\\.]+)\\})");
static String replaceParams(String text)
{
Matcher m = paramPatern.matcher(text);
if (!m.find())
return text;
StringBuffer sb = new StringBuffer();
String param, replacement;
do
{
param = m.group(2);
if (param != null)
{
replacement = getParamValue(param);
m.appendReplacement(sb, replacement);
}
else
m.appendReplacement(sb, "");
}
while (m.find());
m.appendTail(sb);
return sb.toString();
}
static String getParamValue(String name)
{
// replace from map or something else
return name;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12672 次 |
| 最近记录: |