-1

How do i numbering of pages in word file by Java.

I am using Apache POI driver to interact JAVA and word . i want border and page number as well in my word file while i am creating file from JAVA.

Please help.

Maarten van Stam
  • 1,901
  • 1
  • 11
  • 16
varun dangi
  • 19
  • 1
  • 2
  • 1
    Duplicate of http://stackoverflow.com/questions/27410967/apache-poi-how-to-add-a-page-number – BudsNanKis Feb 23 '17 at 15:21
  • 1
    Possible duplicate of [Apache POI how to add a page number](http://stackoverflow.com/questions/27410967/apache-poi-how-to-add-a-page-number) – RamPrakash Feb 23 '17 at 17:59

1 Answers1

1

The question marked as a duplicate has a complex answer to a relatively simple question.

The simple answer (for page number) is very similar to this answer: https://stackoverflow.com/a/40264237/2296441. The difference is just which field to insert. The afore mentioned answer shows how to insert a TOC field. In your case you want a PAGE field.

XWPFParagraph p;
...
// get or create your paragraph
....
CTP ctP = p.getCTP();
CTSimpleField page = ctP.addNewFldSimple();
page.setInstr("PAGE");
page.setDirty(STOnOff.TRUE);

Note: setDirty tells Word to update the field which causes a dialog to be opened when the document is opened. This dialog is MS Word making sure you want to update the field. I don't think you can disable the dialog and still have the field calculated on open.

To set page borders you are once again going to have to break into the CT classes. In this case the appropriate location in the document is the section properties. Here is how to set a double line border around the whole page set back 24 points from the page edge.

// Page Borders
CTDocument1 ctDoc = doc.getDocument();
CTBody ctBody = ctDoc.getBody();
CTSectPr ctSectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr();
CTPageBorders ctPgBorders = ctSectPr.isSetPgBorders() ? ctSectPr.getPgBorders() : ctSectPr.addNewPgBorders();
ctPgBorders.setOffsetFrom(STPageBorderOffset.PAGE);
CTBorder ctBorder = CTBorder.Factory.newInstance();
ctBorder.setVal(STBorder.DOUBLE);
ctBorder.setSpace(new BigInteger("24"));
ctPgBorders.setTop(ctBorder);
ctPgBorders.setBottom(ctBorder);
ctPgBorders.setRight(ctBorder);
ctPgBorders.setLeft(ctBorder);

Disclaimer
The MS-Word functionality in POI is still largely unfinished, and subject to change.

Community
  • 1
  • 1
jmarkmurphy
  • 11,030
  • 31
  • 59
  • Thanks for your comment..But i am facing error to use your above code for border. I am using APACHE 3.14 version.. – varun dangi Mar 09 '17 at 10:20
  • CTDocument1 ctDoc = doc.getDocument(); CTBody ctBody = ctDoc.getBody(); CTSectPr ctSectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr(); CTPageBorders ctPgBorders = ctSectPr.isSetPgBorders() ? ctSectPr.getPgBorders() : ctSectPr.addNewPgBorders(); ctPgBorders.setOffsetFrom(STPageBorderOffset.PAGE); CTBorder ctBorder = CTBorder.Factory.newInstance(); ctBorder.setVal(STBorder.DOUBLE); ctBorder.setSpace(new BigInteger("24")); ctPgBorders.setTop(ctBorder); ctPgBorders.setBottom(ctBorder); ctPgBorders.setRight(ctBorder); ctPgBorders.setLeft(ctBorder); – varun dangi Mar 10 '17 at 05:26
  • STPageBorderOffset and ctPgBorders key throwing error... error was "can not find symbol" – varun dangi Mar 10 '17 at 05:27
  • do you have the `poi-ooxml-schemas` jar in your classpath? are you using matched jars (all from the same release of POI)? – jmarkmurphy Mar 10 '17 at 12:02