Java operations Word bookmark (a): to add, delete, read bookmark

Word, bookmark function commonly used in the search, locate, mark a particular character or paragraph, space for larger documents, this feature very useful. The following will be introduced by Java methods to add and delete a bookmark in Word program. Important examples include:

1. Add Bookmark

Add a bookmark to the specified paragraph 1.1

1.2 Add a bookmark to the specified string

2. Delete bookmark

2.1 Delete bookmark

2.2 Delete bookmark text

3. Read the text Bookmarks

 

Use tools: as Free Spire.Doc for the Java (free version)

Jar file acquisition and import:

 

Method 1 : through the official website to download ja r package. Once downloaded, unzip the file. And the lib folder under the Spire.Doc.jar file into java program. Import reference to the following effects:

 

Method 2 : by maven repository installation introduced . Reference may be installed introduction method .

 

 

Java code examples

 

[Example 1] a specified paragraph bookmark

 

com.spire.doc * Import;. 
Import com.spire.doc.documents.Paragraph; 

public class AppendBookmark { 
    public static void main (String [] args) { 
        // load the need to add bookmarks Word document 
        Document doc = new Document ( ); 
        doc.loadFromFile ( "sample.docx"); 

        // Get bookmarked paragraph requires 
        the paragraph doc.getSections para = () GET (0) .getParagraphs () GET (. 1);.. 

        // the paragraph starting , add a bookmark at the end of the start and end tags, and name the bookmark 
        bookmarkstart start = para.appendBookmarkStart ( "bookmark01"); 
        para.getItems () INSERT (0, start);. 
        para.appendBookmarkEnd ( "bookmark01"); 

        // save the document 
        doc.saveToFile ( "appendbookmark.docx", FileFormat.Docx_2013); 
        doc.dispose (); ;
    }
}

 

 

Bookmark add effects:


 

[Example 2 ] to specify the bookmark 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){
        //加载文档
        Document doc = new Document();
        doc.loadFromFile("sample.docx");

        //查找指定字符串
        TextSelection textSelection = doc.findString("采用蕴含深意的象征语言,揭示人生的哲理。",false,false);
        TextRange range = textSelection.getAsOneRange();
        Paragraph para = range.getOwnerParagraph();
        int index = para.getChildObjects().indexOf(range);

        //添加书签
        BookmarkStart start = new BookmarkStart(doc,"书签1");
        BookmarkEnd end = new BookmarkEnd(doc, "书签1");
        para.getChildObjects().insert(index, start);
        para.getChildObjects().insert(index + 2, end);

        //保存文档
        doc.saveToFile("appendbookmarktocharacter.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

 书签添加效果:

 


 

【示例3】删除书签、书签文本

import com.spire.doc.*;
import com.spire.doc.documents.BookmarksNavigator;

public class DeleteBookmarkAndBookmarkcontent {
    public static void main(String[]args){
        //加载文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //定位到特定的书签
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
        bookmarksNavigator.moveToBookmark("bookmark1");

        //删除书签处的内容
        bookmarksNavigator.deleteBookmarkContent(true);

        //删除书签(仅删除书签标签,原文书签处的内容)
        doc.getBookmarks().remove(doc.getBookmarks().get("bookmark2"));//通过书签名删除
        doc.getBookmarks().removeAt(1);//通过索引值删除

        //保存文档
        doc.saveToFile("deletebookmark.docx",FileFormat.Docx_2013);
    }
}

 运行程序后,生成的文档可查看书签删除效果。

 

【示例4】读取书签文本

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 {
        //加载包含书签的Word文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //获取书签
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
        bookmarksNavigator.moveToBookmark("bookmark1");

        //获取书签文本
        TextBodyPart textBodyPart = bookmarksNavigator.getBookmarkContent();

        //创建String变量
        String text = "";

        //遍历书签内容的项目
        for (Object item : textBodyPart.getBodyItems()) {

            //判断项目是否为段落
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //遍历段落中的子对象
                for (Object childObj : paragraph.getChildObjects()) {

                    //判断子对象是否为TextRange
                    if (childObj instanceof TextRange) {

                        //获取TextRange中的文本
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                    }
                }
            }
        }

        //将获取到的文本写入Txt文件
        PrintWriter printWriter = new PrintWriter("BookmarkText.txt");
        printWriter.println(text);
        printWriter.close();
    }
}

 

书签读取结果:



 

(本文完)

 

 

Guess you like

Origin www.iteye.com/blog/miaonly-2443540