Page 1 of 1

Insert Page Numbers / Remove Page Numbers

PostPosted: Fri Jan 15, 2010 10:16 pm
by jsimas
Hello!

I'm trying to execute the functions available in Word 2007 Insert > Page Numbers, and Insert > Page Number > Remove Page Numbers.

I already tried to test several values returned by the docx4j api, but i'm still lost.

Can anyone give me any pointers where to look?

Thanks in advance.
Best regards.

João Simas.

Re: Insert Page Numbers / Remove Page Numbers

PostPosted: Sat Jan 16, 2010 10:21 am
by jason
Inserting a page number is a 3 step operation:

1. create and add the header or footer part in which it is to appear

2. insert the page number in the header/footer contents in the header/footer part

3. add reference to that header/footer to sectPr

Step 1:
Code: Select all
   HeaderPart headerPart = new HeaderPart();
   Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
            .addTargetPart(headerPart);


Step 2: A footer, as created by Word 2007, might look like:
Code: Select all
            <w:ftr >
                <w:sdt>
                    <w:sdtPr>
                        <w:id w:val="349237360"/>
                        <w:docPartObj>
                            <w:docPartGallery w:val="Page Numbers (Bottom of Page)"/>
                            <w:docPartUnique/>
                        </w:docPartObj>
                    </w:sdtPr>
                    <w:sdtContent>
                        <w:p>
                            <w:pPr>
                                <w:pStyle w:val="Footer"/>
                                <w:jc w:val="center"/>
                            </w:pPr>
                            <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
                                <w:r>
                                    <w:rPr>
                                        <w:noProof/>
                                    </w:rPr>
                                    <w:t>17</w:t>
                                </w:r>
                            </w:fldSimple>
                        </w:p>
                    </w:sdtContent>
                </w:sdt>
                <w:p>
                    <w:pPr>
                        <w:pStyle w:val="Footer"/>
                    </w:pPr>
                </w:p>
            </w:ftr>


but you only really need:

Code: Select all
            <w:ftr >
                        <w:p>
                            <w:pPr>
                                <w:pStyle w:val="Footer"/>
                                <w:jc w:val="center"/>
                            </w:pPr>
                            <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
                                <w:r>
                                    <w:rPr>
                                        <w:noProof/>
                                    </w:rPr>
                                    <w:t>17</w:t>
                                </w:r>
                            </w:fldSimple>
                        </w:p>
            </w:ftr>


w:fldSimple being the critical bit. There are in general 2 approaches to creating these objects - see the Getting Started guide for details.

Code: Select all
      headerPart.setJaxbElement(yourFooter);


Step 3

Using the relId from the rel in step 1, add your w:footerReference to w:sectPr:
Code: Select all
     <w:sectPr>
            <w:footerReference w:type="default" r:id="rId40"/>


(HeaderFooterPolicy would be a good place to add code to help with steps 1 and 3, but at present it only helps with reading existing headers/footers.)

Removing page numbers would be a full or partial reversal of the above.

cheers .. Jason

Re: Insert Page Numbers / Remove Page Numbers

PostPosted: Mon Jan 18, 2010 10:47 pm
by jsimas
Hi.

First of all, thanks for your quick reply! Second, i apologise for my "newbieness", my next questions will probably sound stupid to you.

I've read your post, and i tried to create the next xml part:
Code: Select all
<w:ftr >
                        <w:p>
                            <w:pPr>
                                <w:pStyle w:val="Footer"/>
                                <w:jc w:val="center"/>
                            </w:pPr>
                            <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
                                <w:r>
                                    <w:rPr>
                                        <w:noProof/>
                                    </w:rPr>
                                    <w:t>17</w:t>
                                </w:r>
                            </w:fldSimple>
                        </w:p>
            </w:ftr>


However i'm unable to create a fldSimple part to add to the w:p part. Can you post here the code necessary to create the fldSimple field?

Thanks in advanced. Best regards!

João Simas

Re: Insert Page Numbers / Remove Page Numbers

PostPosted: Tue Jan 19, 2010 1:49 am
by jsimas
Hi.

I already succeeded at adding page numbering into a simple docx file. However i would like to find out if:

1:
To achive this
Code: Select all
<w:ftr >
                  <w:p>
                            <w:pPr>
                                <w:pStyle w:val="Footer"/>
                                <w:jc w:val="center"/>
                            </w:pPr>
                            <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
                                <w:r>
                                    <w:rPr>
                                        <w:noProof/>
                                    </w:rPr>
                                    <w:t>17</w:t>
                                </w:r>
                            </w:fldSimple>
                  </w:p>
     </w:ftr>


it is necessary to write all this code (or is there a simpler way???):

Code: Select all
ObjectFactory factory = Context.getWmlObjectFactory();
      
      FooterPart footerPart = new FooterPart();
      //add values to footer

      CTSimpleField ctSimple = factory.createCTSimpleField();
      ctSimple.setInstr(" PAGE \\* MERGEFORMAT ");

      RPr RPr = factory.createRPr();
      RPr.setNoProof(new BooleanDefaultTrue());
      
      Text t = factory.createText();
      t.setValue("17");
      
       R run = factory.createR();
       run.getRunContent().add(RPr);   
       run.getRunContent().add(t);      
      
       ctSimple.getParagraphContent().add(run);

      JAXBElement<CTSimpleField> fldSimple = factory.createPFldSimple(ctSimple);
      
      // P
      P para = factory.createP();
      para.getParagraphContent().add(fldSimple);
      
       // Now add our paragraph to the footer
       Ftr ftr = factory.createFtr();
       ftr.getEGBlockLevelElts().add(para);
      
       footerPart.setJaxbElement(ftr);
      
      Relationship addTargetPart = word.getMainDocumentPart().addTargetPart(footerPart);
      
      //Add footer reference
      FooterReference ft = factory.createFooterReference();
      ft.setType(HdrFtrRef.DEFAULT);
      ft.setId(addTargetPart.getId());
      
      
      SectPr sectPr = factory.createSectPr();
      sectPr.getEGHdrFtrReferences().add(ft);
      ft.setParent(sectPr);
      
      word.getMainDocumentPart().addObject(sectPr);


2: How can i find out if a footer has a numbering value or not? If i call the method
Code: Select all
word.getHeaderFooterPolicy().getDefaultFooter()

i will only find out that there is a existing footer. Do i have to parse the footer and search for the " PAGE \\* MERGEFORMAT " value?

Thanks for you help

João Simas

Re: Insert Page Numbers / Remove Page Numbers

PostPosted: Tue Jan 19, 2010 11:02 am
by jason
You can shorten it a bit, using unmarshalString:

Code: Select all

   private static String ftrXml = "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                +"<w:p>"
                    +"<w:pPr>"
                        +"<w:pStyle w:val=\"Footer\"/>"
                        +"<w:jc w:val=\"center\"/>"
                    +"</w:pPr>"
                    +"<w:fldSimple w:instr=\" PAGE \\* MERGEFORMAT \">"
                        +"<w:r>"
                            +"<w:rPr>"
                                +"<w:noProof/>"
                            +"</w:rPr>"
                            +"<w:t>17</w:t>"  // remove this?
                        +"</w:r>"
                    +"</w:fldSimple>"
                +"</w:p>"
    +"</w:ftr>";   




Code: Select all

      FooterPart footerPart = new FooterPart();
      Ftr ftr = (Ftr)XmlUtils.unmarshalString(ftrXml);
      footerPart.setJaxbElement(ftr);


Re: Insert Page Numbers / Remove Page Numbers

PostPosted: Tue Oct 29, 2013 11:53 pm
by duracell
As I understand, you want that your generated docx document should have page numbers.
What I did is, that i have a docx tamplate which is called "myTemplatete.docx".
I then selected "Insert > Page Numbers" and i chose one of the offered options. In german it is called "einfgen->seitenanzal".
At the button of myTemplatete.docx it appears the nummer "1".
Now if i generate a docx document with docx4j I use myTemplatete.docx and when there are more than one page, every document page has its own page number. i.e. it i have 4 pages, all my pages is numbered from 1 to 4.