In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to add and delete Word bookmarks through the Java program, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
In Word, the bookmark function is often used to find, locate, and mark specific characters or paragraphs, which is very useful for large documents. Examples of key points include:
1. Add bookmark
1.1 Bookmark a specified paragraph
1.2 Bookmark the specified string
two。 Delete bookmark
2.1 Delete bookmarks
2.2 Delete bookmark text
3. Read bookmark text
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.
[example 1] Bookmark a specified paragraph
Import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class AppendBookmark {public static void main (String [] args) {/ / load the Word document that needs to be bookmarked Document doc = new Document (); doc.loadFromFile ("sample.docx"); / / get the paragraph Paragraph para = doc.getSections () .get (0) .getParagraphs () .get (1) / / add the opening and closing tags of the bookmark at the beginning and end of the paragraph, and name the bookmark BookmarkStart start = para.appendBookmarkStart ("bookmark01"); para.getItems (). Insert (0minute start); para.appendBookmarkEnd ("bookmark01"); / / Save the document doc.saveToFile ("appendbookmark.docx", FileFormat.Docx_2013); doc.dispose ();}}
Bookmark addition effect:
[example 2] Bookmark the specified string
Import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.TextRange; public class AppendBookmarkToCharacter {public static void main (String [] args) {/ / load document Document doc = new Document (); doc.loadFromFile ("sample.docx") / / find the specified string TextSelection textSelection = doc.findString ("use meaningful symbolic language to reveal the philosophy of life." , false,false); TextRange range = textSelection.getAsOneRange (); Paragraph para = range.getOwnerParagraph (); int index = para.getChildObjects () .indexOf (range); / / Bookmark BookmarkStart start = new BookmarkStart (doc, "Bookmark 1"); BookmarkEnd end = new BookmarkEnd (doc, "Bookmark 1"); para.getChildObjects () .insert (index, start) Para.getChildObjects () .insert (index + 2, end); / / Save document doc.saveToFile ("appendbookmarktocharacter.docx", FileFormat.Docx_2013); doc.dispose ();}}
String bookmark addition effect:
[example 3] Delete bookmarks and bookmark text
Import com.spire.doc.*; import com.spire.doc.documents.BookmarksNavigator; public class DeleteBookmarkAndBookmarkcontent {public static void main (String [] args) {/ / load document Document doc = new Document (); doc.loadFromFile ("test.docx"); / / navigate to a specific bookmark BookmarksNavigator bookmarksNavigator = new BookmarksNavigator (doc); bookmarksNavigator.moveToBookmark ("bookmark1") / delete the content at the bookmark bookmarksNavigator.deleteBookmarkContent (true); / / delete the bookmark (delete only the bookmark tag, the content at the original bookmark) doc.getBookmarks () .remove (doc.getBookmarks () .get ("bookmark1")); / / delete doc.getBookmarks () .removeAt (0) through the book signature / / delete / / Save the document doc.saveToFile ("deletebookmark.docx", FileFormat.Docx_2013) through the index value;}}
[example 4] read bookmark text
Import com.spire.doc.*; import com.spire.doc.documents.BookmarksNavigator; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextBodyPart; import com.spire.doc.fields.TextRange; import java.io.IOException; import java.io.PrintWriter; public class GetBookmarkText {public static void main (String [] args) throws IOException {/ / load the Word document containing bookmarks Document doc = new Document (); doc.loadFromFile ("test.docx") / / get bookmark BookmarksNavigator bookmarksNavigator = new BookmarksNavigator (doc); bookmarksNavigator.moveToBookmark ("bookmark1"); / / get bookmark text TextBodyPart textBodyPart = bookmarksNavigator.getBookmarkContent (); / / create String variable String text = "" / / iterate through the item for (Object item: textBodyPart.getBodyItems ()) {/ / determine whether the item is a paragraph if (item instanceof Paragraph) {Paragraph paragraph = (Paragraph) item / / traverse the sub-object for (Object childObj: paragraph.getChildObjects ()) {/ / determine whether the sub-object is TextRange if (childObj instanceof TextRange) {/ / get the text TextRange textRange = (TextRange) childObj in TextRange Text = text + textRange.getText ();} / / write the acquired text to the Txt file PrintWriter printWriter = new PrintWriter ("BookmarkText.txt"); printWriter.println (text); printWriter.close ();}}
Bookmark reading result:
Thank you for reading this article carefully. I hope the article "how to add and remove Word bookmarks through the Java program" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.