這篇文章給大家分享的是有關Java怎么用文本、圖片、表格替換Word書簽的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
使用工具:Free Spire.Doc for Java (免費版)
Jar文件獲取及導入:
方法1:通過官網下載jar文件包。下載后,解壓文件。并將lib文件夾下的Spire.Doc.jar文件導入到java程序。參考如下導入效果:
方法2:可通過maven倉庫安裝導入??蓞⒖及惭b導入方法。
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(); } }
替換效果:
【示例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(); } }
替換效果:
【示例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書簽”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。