poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bart W Jenkins <bauha...@mac.com>
Subject POI - HWPF - Documentation - Page/Name cross-reference example provided.
Date Thu, 23 Aug 2012 14:11:25 GMT
  It is not clear how to provide code examples to the Apache POI project, so, I am sending
it to the dev email address in hopes someone can fold this into the documentation for example
usage.  This took me some time to figure out and so I felt strongly enough to share it.  

How to create cross-reference links to bookmarked areas that show the page number of the target.
 So, for example, if I have a 100 page document and I have an item named "FOO" on page 50
and I want to make a cross-reference link to that item, and I want the cross-reference to
be in the form of:

"(Pg. xxx)"  - where 'xxx' will be a field in word that points to page 100 where the FOO paragraph
is located, then the following code snippet will do that:

Given an XWPFParagraph, 'p' and a 'target' variable of type CTBookmark:

p.createRun().setText(" (Pg.");
CTText pgcttext = p.createRun().getCTR().addNewInstrText();
pgcttext.setStringValue("PAGEREF "+target.getName()+" \\h ");
XWPFRun pgtextRun = p.createRun();
pgtextRun.setText("0");  // here is the text, now as a bookmark xref, this is a placeholder
until one updates the fields in the document or goes to print.
p.createRun().setText(" )");

I hope you all find this example useful.

Note:  For a standard cross reference that keeps the NAME is just a "REF" so just replace
the string "PAGEREF" with "REF".  

View raw message