You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by joejoe <st...@gmail.com> on 2011/09/06 16:38:16 UTC

Re: 1) need bookmarks from ".doc" OR 2) import a ".doc" and work with an XWPFDocument?

Sergey Vladimirov wrote:
> 
> On Fri, Jul 22, 2011 at 1:23 PM, gipsy &lt;spam.gipsy@gmail.com&gt; wrote:
>> 1) Is there any way to get the bookmarks from a ".doc"
> Yes, there is a new API in night builds of 3.8-beta4 to access
> HWPFDocument bookmarks (i.e. document.getBookmarks()). Currently there
> are name, start and end offsets (in respect to overall document
> range). Please, let me know if you need to extract additional
> properties (like disabled autonumbering) or need additional API
> methods like getRange().
> 
>> 2) If not - how is it possible to import a ".doc" and work with an
>> XWPFDocument?
> there is no such way yet, AFAIK, but contributions are always welcomed :)
> 

Hello, I was wondering about best practices when using the Bookmark class. 
I have a document with many bookmarks, that I need to replace with values. 
I have been able to load my "template" document, retrieve all the bookmarks,
and begin to replace values.  All of the bookmarks in my document are just
empty placeholders.  Generalized code snippet below:

Bookmarks bs = doc.getBookmarks();
Bookmark b = bs.getBookmark(i);
int start = b.getStart();
int end = b.getEnd();
range = new Range(start, end, doc);
range.insertAfter("Text I want to put into bookmark location");

The problem is that the document range becomes unstable after I do a couple
range.insertAfter commands.  The start and end of each bookmark is offset by
the amount of characters I have inserted.  This is a big problem when I try
to insert text into a bookmark near the beginning of a document, after I
have already inserted text later in the document.  Maybe I should not be
using the "Range" in this situation?  Any guidance would be appreciated.

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/1-need-bookmarks-from-doc-OR-2-import-a-doc-and-work-with-a-XWPFDocument-tp4622377p4774621.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: 1) need bookmarks from ".doc" OR 2) import a ".doc" and work with an XWPFDocument?

Posted by Sergey Vladimirov <vl...@gmail.com>.
Hi,

Today I updated trunk code, so your example should work. Please also
notice new method replaceText(String, boolean) that can help to
replace bookmark text without search-and-replace.

-- 
Sergey Vladimirov

On Tue, Sep 6, 2011 at 6:38 PM, joejoe <st...@gmail.com> wrote:
>
> Sergey Vladimirov wrote:
>>
>> On Fri, Jul 22, 2011 at 1:23 PM, gipsy &lt;spam.gipsy@gmail.com&gt; wrote:
>>> 1) Is there any way to get the bookmarks from a ".doc"
>> Yes, there is a new API in night builds of 3.8-beta4 to access
>> HWPFDocument bookmarks (i.e. document.getBookmarks()). Currently there
>> are name, start and end offsets (in respect to overall document
>> range). Please, let me know if you need to extract additional
>> properties (like disabled autonumbering) or need additional API
>> methods like getRange().
>>
>>> 2) If not - how is it possible to import a ".doc" and work with an
>>> XWPFDocument?
>> there is no such way yet, AFAIK, but contributions are always welcomed :)
>>
>
> Hello, I was wondering about best practices when using the Bookmark class.
> I have a document with many bookmarks, that I need to replace with values.
> I have been able to load my "template" document, retrieve all the bookmarks,
> and begin to replace values.  All of the bookmarks in my document are just
> empty placeholders.  Generalized code snippet below:
>
> Bookmarks bs = doc.getBookmarks();
> Bookmark b = bs.getBookmark(i);
> int start = b.getStart();
> int end = b.getEnd();
> range = new Range(start, end, doc);
> range.insertAfter("Text I want to put into bookmark location");
>
> The problem is that the document range becomes unstable after I do a couple
> range.insertAfter commands.  The start and end of each bookmark is offset by
> the amount of characters I have inserted.  This is a big problem when I try
> to insert text into a bookmark near the beginning of a document, after I
> have already inserted text later in the document.  Maybe I should not be
> using the "Range" in this situation?  Any guidance would be appreciated.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org