feat: bookmark replace
This commit is contained in:
parent
17fb5cefcb
commit
431a467c8a
@ -14,6 +14,7 @@ import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
@ -88,9 +89,11 @@ public class BookmarkExec {
|
||||
if (previousSibling != null) {
|
||||
String nodeName = previousSibling.getNodeName();
|
||||
if ("w:bookmarkEnd".equals(nodeName)) {
|
||||
// end前一个是start
|
||||
Node bookmarkStartNode = previousSibling.getPreviousSibling();
|
||||
if (bookmarkStartNode != null) {
|
||||
Node nameAttribute = bookmarkStartNode.getAttributes().getNamedItem("w:name");
|
||||
// 获取书签名称,确保同一行内多个书签情况能正确处理
|
||||
if (nameAttribute != null && name.equals(nameAttribute.getNodeValue())) {
|
||||
idx = i - 1;
|
||||
doR = true;
|
||||
@ -101,19 +104,25 @@ public class BookmarkExec {
|
||||
}
|
||||
}
|
||||
if (idx < runs.size() && doR) {
|
||||
if (idx < 0) {
|
||||
run = paragraph.createRun();
|
||||
if (idx < 0) {// 如果是-1,说明这一段的开头有书签
|
||||
// 在本段开头位置插入新的run
|
||||
run = paragraph.insertNewRun(0);
|
||||
// 并复制原有开头的run的所有内容,后续只修改文本内容,不改变样式
|
||||
run.getCTR().set(runs.get(0).getCTR().copy());
|
||||
} else {
|
||||
run = runs.get(idx);
|
||||
}
|
||||
} else {
|
||||
// 处理一段末尾的书签(一般不存在这种情况)
|
||||
run = runs.get(runs.size() - 1);
|
||||
}
|
||||
BookmarkReplaceDataModel model = map.get(name);
|
||||
BookmarkType type = model.getType();
|
||||
// 处理文本
|
||||
if (BookmarkType.TEXT.equals(type)) {
|
||||
run.setText(model.getValue());
|
||||
}
|
||||
// 处理图片,以嵌入式插入图片
|
||||
if (BookmarkType.PICTURE.equals(type)) {
|
||||
AbstractExData extData = model.getExtData();
|
||||
if (extData instanceof PictureExData picture) {
|
||||
|
Loading…
Reference in New Issue
Block a user