You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by Bart W Jenkins <ba...@mac.com> on 2012/08/23 16:11:25 UTC

POI - HWPF - Documentation - Page/Name cross-reference example provided.

All,
  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.  

PROBLEM:
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.");
p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
CTText pgcttext = p.createRun().getCTR().addNewInstrText();
pgcttext.setSpace(Space.PRESERVE);
pgcttext.setStringValue("PAGEREF "+target.getName()+" \\h ");
p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
XWPFRun pgtextRun = p.createRun();
pgtextRun.getCTR().addNewRPr().addNewNoProof();
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.
pgtextRun.setBold(true);
pgtextRun.setUnderline(UnderlinePatterns.SINGLE);
p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.END);
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".  

Bart

Re: POI - HWPF - Documentation - Page/Name cross-reference example provided.

Posted by Yegor Kozlov <ye...@dinom.ru>.
Contributions are submitted to the POI Bug Database:
https://issues.apache.org/bugzilla/enter_bug.cgi?product=POI

Can you create an isolated program with a main() method that creates a
document with page/name cross-references ? We can then include it in
the XWPF examples module:
https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/

Yegor

On Thu, Aug 23, 2012 at 6:11 PM, Bart W Jenkins <ba...@mac.com> wrote:
> All,
>   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.
>
> PROBLEM:
> 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.");
> p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
> CTText pgcttext = p.createRun().getCTR().addNewInstrText();
> pgcttext.setSpace(Space.PRESERVE);
> pgcttext.setStringValue("PAGEREF "+target.getName()+" \\h ");
> p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
> XWPFRun pgtextRun = p.createRun();
> pgtextRun.getCTR().addNewRPr().addNewNoProof();
> 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.
> pgtextRun.setBold(true);
> pgtextRun.setUnderline(UnderlinePatterns.SINGLE);
> p.createRun().getCTR().addNewFldChar().setFldCharType(STFldCharType.END);
> 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".
>
> Bart

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