您好,请问怎么在文档指定的位置,比如说某个段落之后,添加一个段落。
已知一个段落,怎么修改这个段落的文本、格式。
@ouchli 您可以使用 CompositeNode.insertBefore
或 CompositeNode.insertAfter
方法来实现此目的。 例如,以下代码在指定段落后添加一个段落:
Document doc = new Document("C:\\Temp\\in.docx");
// Get some paragraph.
Paragraph p = doc.getFirstSection().getBody().getLastParagraph();
// Create a paragraph
Paragraph newPara = new Paragraph(doc);
newPara.appendChild(new Run(doc, "this is a new paragraph."));
// Insert the created paragraph before the specified.
p.getParentNode().insertBefore(newPara, p);
doc.save("C:\\Temp\\out.docx");
段落可以包含任何类型的内联节点,RUN节点代表文本,SHAPE代表图形对象等。请参阅我们的文档以了解有关Aspose.Words文档对象模型的更多信息:
https://docs.aspose.com/words/java/aspose-words-document-object-model/
因此,要更改段落的内容,您应该删除其子注释并添加新内容。
好的,谢谢。
Document doc = new Document("C:\\Temp\\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.setReplacingCallback(new SearchCallback());
doc.getRange().replace("Find Me", "", opt);
private static class SearchCallback implements IReplacingCallback
{
@Override
public int replacing(ReplacingArgs args) throws Exception {
// Get the matched node.
Node matchedNode = args.getMatchNode();
// Do something with the matched node
// ..................................
// Do nothing with the matched text.
return ReplaceAction.SKIP;
}
}
我在使用这种方式查找 某个文本 的位置时,其中的 matchedNode 是一个 Run 节点。假如我需要查找的 文本 是跨越了 两个 Run 节点的话,怎么能获取到这两个Run节点呢?
@ouchli 最简单的方法是用占位符本身替换占位符。 进行此类操作后,占位符将表示为单个 RUN。 例如看下面的代码:
Document doc = new Document("C:\\Temp\\in.docx");
// Replace placeholder with itself to make it to be represented as a single Run.
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace("Find Me", "$0", opt);
// Now when you call replace operation the second time the placeholder will be represented as a single Run.
// .......................
doc.save("C:\\Temp\\out.docx");
不好意思,有点没理解您的代码,文本跨越的两个Run节点值在哪里得到呢
@ouchli 使用上述代码后,占位符将表示为单个 RUN。 因此不需要获得两个或更多 RUN 节点。 请在运行上述代码后尝试使用您的代码,您将看到 args.getMatchNode() 返回整个占位符的 RUN。
运行代码之后,args.getMatchNode()返回的仍然是 第一个Run,不是整个
@ouchli 请尝试使用以下代码:
Document doc = new Document("C:\\Temp\\in.docx");
// Replace placeholder with itself to make it to be represented as a single Run.
FindReplaceOptions opt = new FindReplaceOptions();
opt.setUseSubstitutions(true);
doc.getRange().replace("Find Me", "$0", opt);
// Now when you call replace operation the second time the placeholder will be represented as a single Run.
// .......................
FindReplaceOptions opt1 = new FindReplaceOptions();
opt1.setReplacingCallback(new SearchCallback());
doc.getRange().replace("Find Me", "", opt1);
doc.save("C:\\Temp\\out.docx");
private static class SearchCallback implements IReplacingCallback
{
@Override
public int replacing(ReplacingArgs args) throws Exception {
// Get the matched node.
Node matchedNode = args.getMatchNode();
// Do something with the matched node
// ..................................
System.out.println(matchedNode.getText());
// Do nothing with the matched text.
return ReplaceAction.SKIP;
}
}