多语言展示
当前在线:1036今日阅读:26今日分享:39

Java 添加文本、图片、表格到Word书签

以下经验内容将分享通过java添加文本、图片、表格到Word已有书签中的方法。
工具/原料
1

Free Spire.Doc for Java (免费版)

2

IntelliJ IDEA

Jar文件获取及导入:
2

方法2:可通泉仗过maven仓库安装导入。

【示例1】添加图片、文本到书签
1

import com.spire.doc.*;  import com.spire.doc.documents.BookmarksNavigator;  import com.spire.doc.documents.Paragraph;  import com.spire.doc.fields.DocPicture;    public class AddImgToBookmarkcontent {      public static void main(String[]args){          //加载包含书签的文档          Document doc = new Document();          doc.loadFromFile('test.docx');            //定位到指定书签的起始标签位置,插入图片          BookmarksNavigator bookmarksNavigator1 = new BookmarksNavigator(doc);          bookmarksNavigator1.moveToBookmark('bookmark1',true,false);          Section sec = doc.addSection();          Paragraph para = sec.addParagraph();          DocPicture picture = para.appendPicture('eth.png');          bookmarksNavigator1.insertParagraph(para);            //定位到指定书签的末尾标签位置,插入文本          BookmarksNavigator bookmarksNavigator2 = new BookmarksNavigator(doc);          bookmarksNavigator2.moveToBookmark('bookmark1',false,true);          bookmarksNavigator2.insertText('新插入的文本!!!');            //保存文档          doc.saveToFile('addImgToBookmarkcontent.docx',FileFormat.Docx_2013);          doc.dispose();      }  }

2

图片、表格添加效果:

【示例2】添加表格到书签内容
1

import com.spire.doc.*;  import com.spire.doc.documents.*;  import com.spire.doc.fields.TextRange;    public class AddTableToBookmarkcontent {      public static void main(String[]args){          //加载包含书签的文档          Document doc = new Document();          doc.loadFromFile('test.docx');            //声明数组内容          String[][] data =                  {                          new String[]{'班级', '姓名', '学号'},                          new String[]{'1班', '刘楠', 'Y12534'},                          new String[]{'2班', '刘莉', 'Y12547'},                          new String[]{'3班', '方红', 'Y12365'},                  };            //创建表格          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);              }          }          //定位到指定书签位置,添加表格          BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);          bookmarksNavigator.moveToBookmark('bookmark1');          bookmarksNavigator.insertTable(table);            //保存文档          doc.saveToFile('addTableToBookmarkcontent.docx',FileFormat.Docx_2013);          doc.dispose();      }  }

2

表格添加效果:

推荐信息