替换jar包_Java 用文本或表格替换Word书签内容

本文将使用Free Spire.Doc for Java免费控件来演示如何替换Word文档中添加有书签的文本段落。具体内容如下:

  • 文本替换书签内容
  • 表格替换书签内容

在运行代码前,需将jar包导入IDEA。可通过下载产品包手动导入或在Maven下的pom.xml文件中添加如下引用。

com.e-icebluehttp://repo.e-iceblue.cn/repository/maven-public/e-iceblue        spire.doc.free        3.9.0

示例1 用文本替换书签内容

import com.spire.doc.*;import com.spire.doc.documents.*;public class ReplaceWithText {    public static void main(String[] args) {        //加载Word文档        Document doc = new Document("C:甥敳獲Test1DesktopSample.docx");        //定位到书签"MyBookmark"        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);        bookmarkNavigator.moveToBookmark("MyBookmark");        //使用文本替换原书签的内容, false表示不保留原来的格式        bookmarkNavigator.replaceBookmarkContent("使用文本替换书签内容", false);        //保存文档        doc.saveToFile("output/ReplaceWithText.docx", FileFormat.Docx);    }}

或使用Html String替换书签内容

import com.spire.doc.*;import com.spire.doc.documents.*;import com.spire.doc.fields.ParagraphBase;public class ReplaceWithHTMLString {    public static void main(String[] args) {        //加载Word文档        Document doc = new Document("C:甥敳獲Test1DesktopSample.docx");        //临时添加一个section        Section tempSection = doc.addSection();        //添加段落到section并添加Html string到段落        String html = "使用文本替换书签内容";        tempSection.addParagraph().appendHTML(html);        //获取段落的第一项和最后一项        ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem();        ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem();        //创建TextBodySelection对象        TextBodySelection selection = new TextBodySelection(firstItem, lastItem);        //创建TextBodyPart对象        TextBodyPart bodyPart = new TextBodyPart(selection);        //定位到书签"MyBookmark"        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);        bookmarkNavigator.moveToBookmark("MyBookmark");        //使用Html string替换原书签的内容        bookmarkNavigator.replaceBookmarkContent(bodyPart);        //移除临时添加的section        doc.getSections().remove(tempSection);        //保存结果文档        doc.saveToFile("output/ReplaceWithHTMLString.docx", FileFormat.Docx);    }}

替换前后对比:

f95713614ba5a66f6f1889dc99bbcc07.png

示例2 用表格替换书签内容

import com.spire.doc.*;import com.spire.doc.documents.*;public class ReplaceWithTable {    public static void main(String[] args) {        //加载Word文档        Document doc = new Document("C:甥敳獲Test1DesktopSample.docx");        String[][] data =                {                new String[]{"名称", "额定容量", "电源", "工作时间"},                new String[]{"LED-901充电式手电筒", "900mAH", "AC110V/220V", "26个小时"},                };        //创建表格        Table table = new Table(doc, true);        table.resetCells(2, 4);        for (int i = 0; i < data.length; i++) {            TableRow dataRow = table.getRows().get(i);            for (int j = 0; j < data[i].length; j++) {                dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);            }        }        //创建TextBodyPart对象        TextBodyPart bodyPart= new TextBodyPart(doc);        bodyPart.getBodyItems().add(table);        //定位到书签"MyBookmark"        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);        bookmarkNavigator.moveToBookmark("MyBookmark");        //使用表格替换原书签的内容        bookmarkNavigator.replaceBookmarkContent(bodyPart);        //保存文档        doc.saveToFile("output/ReplaceWithTable.docx", FileFormat.Docx);    }}

替换效果:

b10bc8a8be6dc82383890f567ff350e7.png

推荐阅读:

如何添加、读取、删除Word书签

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值