Page 1 of 1

Page number

PostPosted: Tue Nov 22, 2016 9:42 pm
by Olivier
Hi I have a question about page number and total number of page.

In a header I have (ex: 1/2) where 1 is the current page and 2 the total number of page. In the header XML I have
Code: Select all
<w:p w:rsidR="00000000" w:rsidRDefault="005907F7">
               <w:pPr>
                  <w:spacing w:line="320" w:lineRule="atLeast"/>
                  <w:jc w:val="right"/>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
               </w:pPr>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="begin"/>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:instrText>PAGE</w:instrText>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="separate"/>
               </w:r>
               <w:r w:rsidR="00986B03">
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:noProof/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:t>2</w:t>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="end"/>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:t>/</w:t>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="begin"/>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:instrText>NUMPAGES \* Arabic</w:instrText>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="separate"/>
               </w:r>
               <w:r w:rsidR="00986B03">
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:noProof/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:t>5</w:t>
               </w:r>
               <w:r>
                  <w:rPr>
                     <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
                     <w:color w:val="auto"/>
                  </w:rPr>
                  <w:fldChar w:fldCharType="end"/>
               </w:r>
            </w:p>

I am not sure how I can make it. I have write some methods for <w:fldChar w:fldCharType="begin"/> and <w:fldChar w:fldCharType="end"/>

Code: Select all
FldChar fldcharend = objectFactory.createFldChar();
      fldcharend.setFldCharType(STFldCharType.END);


Code: Select all
FldChar fldchar = objectFactory.createFldChar();
      fldchar.setFldCharType(STFldCharType.BEGIN);
      fldchar.setDirty(true);


but for NUMPAGES and PAGE... no idea

Thanks a lot for your help

Best regards

Re: Page number

PostPosted: Tue Nov 22, 2016 9:53 pm
by jason
Just feed your sample docx through the docx4j webapp (see menu above) or theDocx4j Helper Word AddIn, and let it generate the code for you :-)

Re: Page number

PostPosted: Tue Nov 22, 2016 10:27 pm
by Olivier

Re: Page number

PostPosted: Tue Nov 22, 2016 10:32 pm
by jason

Re: Page number

PostPosted: Tue Nov 22, 2016 10:37 pm
by Olivier
Yes I can see the constitution of my document, I can see the XML but where I can find the code generated ?

Re: Page number

PostPosted: Tue Nov 22, 2016 11:07 pm
by jason
Click through to the header of interest (eg /word/header1.xml),

then click on the opening tag of the element you want to generate code for (eg w:p)

Re: Page number

PostPosted: Thu Dec 15, 2016 7:13 pm
by docx4j
By reference org.docx4j.samples.HeaderFooterCreate in docx4j src

Source code org.docx4j.samples are a number of examples.

Below is a code snippet about the PAGE/NUMPAGES of header/footer in my project: :roll:


public static void main(String[] args) throws Exception {
WordprocessingMLPackage doc = WordprocessingMLPackage.createPackage();
MainDocumentPart mdp = doc.getMainDocumentPart();
Body body = mdp.getContents().getBody();
ObjectFactory factory = Context.getWmlObjectFactory();

// Header
HeaderPart headerPart = new HeaderPart();
Relationship rel = mdp.addTargetPart(headerPart);
Hdr hdr = factory.createHdr();
headerPart.setJaxbElement(hdr);

// <w:sectPr>
// <w:headerReference w:type="default" r:id="rId2"/>
// <w:footerReference w:type="default" r:id="rId3"/>
// </w:sectPr>
List<SectionWrapper> sections = doc.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr==null) {
sectPr = factory.createSectPr();
mdp.addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
HeaderReference headerReference = factory.createHeaderReference();
headerReference.setId(rel.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or footer

// Add table, image, p ... to Header
P p = factory.createP();
hdr.getContent().add(p);
R r = null;
Text t = null;

PPr pPr = factory.createPPr();
p.setPPr(pPr);
// style
PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
pStyle.setVal("a3"); // style21
pPr.setPStyle(pStyle);
Jc jc = factory.createJc();
jc.setVal(JcEnumeration.CENTER);
pPr.setJc(jc);

// PAGE
addVARToP(factory, p, "PAGE");

// /
r = factory.createR();
p.getContent().add(r);
t = factory.createText();
t.setSpace("preserve"); // preserve space
t.setValue(" / ");
r.getContent().add(t);

// NUMPAGES
addVARToP(factory, p, "NUMPAGES");

// new page
p = factory.createP();
r = factory.createR();
p.getContent().add(r);
Br br = factory.createBr();
br.setType(STBrType.PAGE);
r.getContent().add(br);
body.getContent().add(p);

doc.save(new FileOutputStream(new File("~/phf.docx")));
}

public static void addVARToP(ObjectFactory factory, P p, String var) {
R r = factory.createR();
p.getContent().add(r);
FldChar fldChar = factory.createFldChar();
r.getContent().add(fldChar);
fldChar.setFldCharType(STFldCharType.BEGIN); // begin

r = factory.createR();
p.getContent().add(r);
Text t = factory.createText();
t.setValue(var); // PAGE \* Arabic \* MERGEFORMAT
JAXBElement<Text> jaxbElement = factory.createRInstrText(t);
r.getContent().add(jaxbElement);

r = factory.createR();
p.getContent().add(r);
fldChar = factory.createFldChar();
r.getContent().add(fldChar);
fldChar.setFldCharType(STFldCharType.SEPARATE);

r = factory.createR();
p.getContent().add(r);
t = factory.createText();
t.setValue("1"); // start value set to 1
r.getContent().add(t);

r = factory.createR();
p.getContent().add(r);
fldChar = factory.createFldChar();
r.getContent().add(fldChar);
fldChar.setFldCharType(STFldCharType.END); // end
}
:P :P :P