Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report