Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java replace Word bookmarks with text, pictures, and tables?

Shulou Source: shulou.com Published: 2022-06-02 04:45:35 10月01日 Update

This article is about how Java replaces Word bookmarks with text, pictures, and tables. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Using tools: Free Spire.Doc for Java (free version)

Jar file acquisition and import:

Method 1: download the jar package through the official website. After downloading, extract the file. And import the Spire.Doc.jar file under the lib folder into the java program. Refer to the following import effect:

Method 2: it can be installed and imported through maven warehouse. You can refer to the installation and import method.

Java code example

[example 1] replace bookmarks with text

Import com.spire.doc.*; import com.spire.doc.documents.BookmarksNavigator; public class ReplaceBookmarkContentWithNewContent {public static void main (String [] args) {/ / load the Word document containing bookmarks Document doc = new Document (); doc.loadFromFile ("test.docx"); / / navigate to the specified bookmark location BookmarksNavigator bookmarksNavigator = new BookmarksNavigator (doc); bookmarksNavigator.moveToBookmark ("bookmark1") / / replace the text in the original bookmark position with the text content, and the new content is in the same format as the original text bookmarksNavigator.replaceBookmarkContent ("New text content!" , true); / / Save document doc.saveToFile ("replaceWithNewContent.docx", FileFormat.Docx_2013); doc.dispose ();}}

Replacement effect:

[example 2] replace bookmarks with pictures

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) {/ / load the document containing bookmarks Document doc = new Document (); doc.loadFromFile ("test.docx"); / / navigate to the specified bookmark location BookmarksNavigator bookmarksNavigator = new BookmarksNavigator (doc) BookmarksNavigator.moveToBookmark ("bookmark1"); / / add pictures to replace the original bookmark content Paragraph para= new Paragraph (doc); para.appendPicture ("eth.png"); TextBodyPart bodyPart = new TextBodyPart (doc); bodyPart.getBodyItems (). Add (para); bookmarksNavigator.replaceBookmarkContent (bodyPart); / / Save document doc.saveToFile ("replaceWithImg.docx", FileFormat.Docx_2013) Doc.dispose ();}}

Replacement effect:

[example 3] replace bookmarks with tables

Import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; public class ReplaceBookmarkContentWithTable {public static void main (String [] args) {/ / load the Word document containing bookmarks Document doc = new Document (); doc.loadFromFile ("test.docx") / / declare array contents String [] [] data = {new String [] {"Classification", "Grade", "number"}, new String [] {"A", "level 1", "01A"}, new String [] {"B", "level 2" "02B"}, new String [] {"C", "level 3", "03C"},} / / create the table 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(); } } 替换效果:

Thank you for reading! This is the end of the article on "how Java replaces Word bookmarks with text, pictures and tables". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Bookmarks content text documents tables files pictures locations effects examples methods references positioning more articles good practical consistent warehouse code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple MySQL macOS Redmi