Bookmarks

Bookmarks (3)

Thursday, 08 August 2019 08:48

Get Bookmark Text in Java

Written by support iceblue

This article demonstrates how to get the text inside a bookmark in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
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.FileNotFoundException;
import java.io.PrintWriter;

public class GetBookmarkText {

    public static void main(String[] args) throws FileNotFoundException {

        //create a Document object
        Document doc = new Document();

        //load a sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //get the specific bookmark
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        navigator.moveToBookmark("MyBookmark");

        //get the bookmark content
        TextBodyPart textBodyPart = navigator.getBookmarkContent();

        //declare a String variable
        String text = "";

        //loop through body items
        for (Object item : textBodyPart.getBodyItems()) {

            //determine if an item is a paragraph
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //loop through the child objects of the paragraph
                for (Object childObj : paragraph.getChildObjects()) {

                    //determine if a child object is a text range
                    if (childObj instanceof TextRange) {

                        //get text from the text range
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                    }
                }
            }
        }

        //write the bookmark text to a .txt file
        PrintWriter printWriter = new PrintWriter("output/BookmarkText.txt");
        printWriter.println(text);
        printWriter.close();
    }
}

Get Bookmark Text in Java

Friday, 28 June 2019 08:34

Replace Bookmark Content in Word in Java

Written by support iceblue

This article will show you have to replace the content of a bookmark in Word using Spire.Doc for Java.

Replace bookmark content with text

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

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        //locate the bookmark."SimpleBookmark"
        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
        bookmarkNavigator.moveToBookmark("SimpleBookmark");

        //replace the content of the bookmark with text
        bookmarkNavigator.replaceBookmarkContent("This is new content", false);

        //save the resultant document
        doc.saveToFile("ReplaceWithText.docx", FileFormat.Docx);
    }
}

Replace Bookmark Content in Word in Java

Replace bookmark content with HTML string

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ParagraphBase;

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        //add a temp section
        Section tempSection = doc.addSection();
        //add a paragraph and append Html string
        String html = "

This Bookmark has been edited

"; tempSection.addParagraph().appendHTML(html); //Get the first item and the last item of the paragraph ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem(); ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem(); //create a TextBodySelection object TextBodySelection selection = new TextBodySelection(firstItem, lastItem); //create a TextBodyPart object TextBodyPart bodyPart = new TextBodyPart(selection); //locate the bookmark "SimpleBookmark" BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc); bookmarkNavigator.moveToBookmark("SimpleBookmark"); //replace the content of the bookmark with Html string bookmarkNavigator.replaceBookmarkContent(bodyPart); //remove the temp section doc.getSections().remove(tempSection); //save the resultant document doc.saveToFile("ReplaceWithHTMLString.docx", FileFormat.Docx); } }

Replace Bookmark Content in Word in Java

Replace bookmark content with table

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

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        String[][] data =
                {
                        new String[]{"Name", "Capital", "Continent", "Area"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147"},
                        new String[]{"Chile", "Santiago", "South America", "756943"},
                };

        //create a table
        Table table = new Table(doc,true);
        table.resetCells(5, 4);
        for (int i = 0; i < data.length; i++) {
            TableRow dataRow = table.getRows().get(i);
            for (int j = 0; j < data[i].length; j++) {
                dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
            }
        }

        //create a TextBodyPart object
        TextBodyPart bodyPart= new TextBodyPart(doc);
        bodyPart.getBodyItems().add(table);

        //locate the bookmark "SimpleBookmark"
        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
        bookmarkNavigator.moveToBookmark("SimpleBookmark");

        //replace the content of the bookmark with table
        bookmarkNavigator.replaceBookmarkContent(bodyPart);

        //save the resultant document
        doc.saveToFile("ReplaceWithTable.docx", FileFormat.Docx);
    }
}

Replace Bookmark Content in Word in Java

This article demonstrates how to add and delete bookmarks in an existing Word document using Spire.Doc for Java.

Insert Bookmark

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

public class InsertBookmark {

    public static void main(String[] args) {

        //create a Document object
        Document doc = new Document();

        //load an existing Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //get the the paragraph where you want to insert bookmark
        Paragraph paragraph = doc.getSections().get(0).getParagraphs().get(1);

        //append a bookmark start and remove it to the beginning of the paragraph
        BookmarkStart start = paragraph.appendBookmarkStart("myBookmark");
        paragraph.getItems().insert(0,start);
        
        //append a bookmark end at the end of the paragraph
        paragraph.appendBookmarkEnd("myBookmark");
        
        //save the file
        doc.saveToFile("output/AddBookmark.docx", FileFormat.Docx_2013);
    }
}

Insert and Remove Bookmarks in Word in Java

Remove Bookmark

import com.spire.doc.Bookmark;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RemoveBookmark {

    public static void main(String[] args) {

        //create a Document object
        Document doc = new Document();

        //load an existing Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\AddBookmark.docx");
        
        //get bookmark by its index
        Bookmark bookmark = doc.getBookmarks().get(0);

        //remove the bookmark
        doc.getBookmarks().remove(bookmark);

        //save the document.
        doc.saveToFile("output/RemoveBookmark.docx", FileFormat.Docx);
    }
}

Insert and Remove Bookmarks in Word in Java