Page 1 of 1

Set a bookmark Text

PostPosted: Sat Apr 27, 2013 1:08 am
by marquez
Hi,

I'm recently using the docx4j and i'm trying to set a bookmark on a Word document (it's my template file).

I already see the manual but i don't find the answer :S

I'm using version 2.8.1 and i'm getting a Bookmark based on the code available http://www.docx4java.org/forums/docx-java-f6/bookmarks-and-templates-advice-t681.html.

My code is:

Code: Select all
   
              PbookmarkedParagraphInPart = findBookmarkedParagraphInPart(documentPart, "bookmarktest");

               // 3. copy the found paragraph to keep styling correct
      P copy = (P) XmlUtils.deepCopy(bookmarkedParagraphInPart);

                String ptext = "Testing";

      // replace the text elements from the copy
      List<?> texts = getAllElementFromObject(copy, Text.class);
      if (texts.size() > 0) {
         Text textToReplace = (Text) texts.get(0);
         textToReplace.setValue(ptext);
      }

      // add the paragraph to the document
      bookmarkedParagraphInPart.getContent().add(copy);


With this code my ptext is set on bookmark location but not inside of bookmark :/

I also retrieve the CTBookmark but there is no method to set a value here.

Can anybody help, please?

Tks

Re: Set a bookmark Text

PostPosted: Tue Apr 30, 2013 10:16 am
by jason
If you unzip a docx containing a bookmark, you'll see it isn't an element with content at all. It is a pair of point tags, bookmarkStart and bookmarkEnd, which are paired by reference to a shared ID.

So you face the same challenge as Chetham (how to delete bookmark contents). Its a bit easier if you are sure your bookmarks are in a single paragraph.