温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java怎么用文本、图片、表格替换Word书签

发布时间:2021-12-30 15:06:00 来源:亿速云 阅读:223 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关Java怎么用文本、图片、表格替换Word书签的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

使用工具:Free Spire.Doc for Java (免费版)

Jar文件获取及导入:

方法1通过官网下载jar文件包。下载后,解压文件。并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果:

Java怎么用文本、图片、表格替换Word书签

方法2:可通过maven仓库安装导入。可参考安装导入方法。

Java代码示例

【示例1】用文本替换书签

 import com.spire.doc.*;
 import com.spire.doc.documents.BookmarksNavigator;
 
 public class ReplaceBookmarkContentWithNewContent {
     public static void main(String[]args){
         //加载包含书签的Word文档
         Document doc = new Document();
         doc.loadFromFile("test.docx");
 
         //定位到指定书签位置
         BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
         bookmarksNavigator.moveToBookmark("bookmark1");
 
         //用文本内容替换原有书签位置的文本,新替换的内容与原文格式一致
         bookmarksNavigator.replaceBookmarkContent("新文本内容!",true);
 
 
         //保存文档
         doc.saveToFile("replaceWithNewContent.docx",FileFormat.Docx_2013);
         doc.dispose();
     }
 }

替换效果:

Java怎么用文本、图片、表格替换Word书签

【示例2】用图片替换书签

 import com.spire.doc.*;
 import com.spire.doc.documents.BookmarksNavigator;
 import com.spire.doc.documents.Paragraph;
 import com.spire.doc.documents.TextBodyPart;
 
 public class ReplaceBookmarkWithImg {
     public static void main(String[]args){
         //加载包含书签的文档
         Document doc = new Document();
         doc.loadFromFile("test.docx");
 
         //定位到指定书签位置
         BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
         bookmarksNavigator.moveToBookmark("bookmark1");
 
         //添加图片,替换原有书签内容
         Paragraph para= new Paragraph(doc);
         para.appendPicture("eth.png");
         TextBodyPart bodyPart = new TextBodyPart(doc);
         bodyPart.getBodyItems().add(para);
         bookmarksNavigator.replaceBookmarkContent(bodyPart);
 
         //保存文档
         doc.saveToFile("replaceWithImg.docx",FileFormat.Docx_2013);
         doc.dispose();
     }
 }

替换效果:

Java怎么用文本、图片、表格替换Word书签

【示例3】用表格替换书签

 import com.spire.doc.*;
 import com.spire.doc.documents.*;
 import com.spire.doc.fields.TextRange;
 
 public class ReplaceBookmarkContentWithTable {
     public static void main(String[]args){
         //加载包含书签的Word文档
         Document doc = new Document();
         doc.loadFromFile("test.docx");
 
         //声明数组内容
         String[][] data =
                 {
                         new String[]{"分类", "等级", "编号"},
                         new String[]{"A", "一级", "01A"},
                         new String[]{"B", "二级", "02B"},
                         new String[]{"C", "三级", "03C"},
                 };
 
         //创建表格
         Table table = new Table(doc, true);
         table.resetCells(4, 3);
         for (int i = 0; i < data.length; i++) {
             TableRow dataRow = table.getRows().get(i);
             for (int j = 0; j < data[i].length; j++) {
                 TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                 range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                 range.getCharacterFormat().setFontName("楷体");
                 dataRow.getRowFormat().setHorizontalAlignment(RowAlignment.Center);
                 dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
             }
         }
 
         //创建TextBodyPart对象
         TextBodyPart bodyPart= new TextBodyPart(doc);
         bodyPart.getBodyItems().add(table);
 
         //定位到指定书签位置
         BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
         bookmarkNavigator.moveToBookmark("bookmark1");
 
         //使用表格替换原书签的内容
         bookmarkNavigator.replaceBookmarkContent(bodyPart);
 
         //保存文档
         doc.saveToFile("replaceWithTable.docx", FileFormat.Docx);
         doc.dispose();
     }
 }

替换效果:

Java怎么用文本、图片、表格替换Word书签

感谢各位的阅读!关于“Java怎么用文本、图片、表格替换Word书签”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI