Page 1 of 1

adding page numbers to word docx

PostPosted: Sat Feb 18, 2012 11:41 am
by dkdev
Hello,

I'm using docx4j and have a requirement to add page numbers to my headers (page x or y for example).

I haven't been able to find any examples and can't seem to get this to work with CTPageNumber.

Has anyone had experience with this?

Thanks in advance for any info!

Re: adding page numbers to word docx

PostPosted: Tue Feb 21, 2012 11:26 pm
by sureshbabubv
Hi,

Please try for this.

Code: Select all
public static Relationship createHeaderPart(
      WordprocessingMLPackage wordprocessingMLPackage)
      throws Exception {


   HeaderPart headerPart = new HeaderPart();
   
   
   headerPart.setPackage(wordprocessingMLPackage);
   
   headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));
   
   
   return wordprocessingMLPackage.getMainDocumentPart()
         .addTargetPart(headerPart);

}

public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
      Part sourcePart) throws Exception {

   Hdr hdr = objectFactory.createHdr();


String strHeader="<w:p   xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" w:rsidR=\"00713B54\" w:rsidRDefault=\"00713B54\"><w:pPr><w:pStyle w:val=\"Header\"/><w:jc w:val=\"right\"/><w:rPr><w:noProof/><w:sz w:val=\"20\"/></w:rPr></w:pPr><w:r><w:rPr><w:noProof/></w:rPr><w:t xml:space=\"preserve\">Page </w:t></w:r><w:r><w:rPr><w:noProof/><w:sz w:val=\"32\"/><w:szCs w:val=\"40\"/></w:rPr><w:fldChar w:fldCharType=\"begin\"/></w:r><w:r><w:rPr><w:noProof/><w:sz w:val=\"32\"/><w:szCs w:val=\"40\"/></w:rPr><w:instrText xml:space=\"preserve\"> PAGE  \* Arabic  \* MERGEFORMAT </w:instrText></w:r><w:r><w:rPr><w:noProof/><w:sz w:val=\"32\"/><w:szCs w:val=\"40\"/></w:rPr><w:fldChar w:fldCharType=\"separate\"/></w:r><w:r><w:rPr><w:noProof/><w:sz w:val=\"32\"/><w:szCs w:val=\"40\"/></w:rPr><w:t>1</w:t></w:r><w:r><w:rPr><w:noProof/><w:sz w:val=\"32\"/><w:szCs w:val=\"40\"/></w:rPr><w:fldChar w:fldCharType=\"end\"/></w:r></w:p>";


Hdr hdr = (Hdr )XmlUtils.unmarshalString(strHeader);


   return hdr;

}


I hope this may resolve your issue.

Thanks & Regards,
B.V.Suresh Babu.