How to add page numbers in format X of Y while creating a word document using apache poi api?

前端 未结 1 1176
独厮守ぢ
独厮守ぢ 2020-12-11 11:36

Is there any method specified in POI API to get the total number of pages, I am able to add page number in the footer of the document but i am not able to add the total numb

相关标签:
1条回答
  • 2020-12-11 12:06

    Page count in Word is dependent of much things like font size, paragraph top/bottom margins and padding, printer settings, manually inserted page breaks and so on. So it cannot be stored directly in the file. It will be calculated on the fly while Word is rendering its pages.

    But we can use fields within the footer which also calculate the page number and the total number of pages.

    Example using up to apache poi 3.14:

    import java.io.*;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    //import org.apache.poi.wp.usermodel.HeaderFooterType;
    
    public class CreateWordHeaderFooter {
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument doc= new XWPFDocument();
    
      // the body content
      XWPFParagraph paragraph = doc.createParagraph();
      XWPFRun run=paragraph.createRun();  
      run.setText("The Body:");
    
      paragraph = doc.createParagraph();
      run=paragraph.createRun();  
      run.setText("Lorem ipsum.... page 1");
    
      paragraph = doc.createParagraph();
      run=paragraph.createRun();
      run.addBreak(BreakType.PAGE); 
      run.setText("Lorem ipsum.... page 2");
    
      paragraph = doc.createParagraph();
      run=paragraph.createRun();
      run.addBreak(BreakType.PAGE); 
      run.setText("Lorem ipsum.... page 3");
    
      // create header-footer
      XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
      if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();
    
      // create header start
      XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
      //XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
    
      paragraph = header.getParagraphArray(0);
      if (paragraph == null) paragraph = header.createParagraph();
      paragraph.setAlignment(ParagraphAlignment.LEFT);
    
      run = paragraph.createRun();  
      run.setText("The Header:");
    
      // create footer start
      XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
      //XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
    
      paragraph = footer.getParagraphArray(0);
      if (paragraph == null) paragraph = footer.createParagraph();
      paragraph.setAlignment(ParagraphAlignment.CENTER);
    
      run = paragraph.createRun();  
      run.setText("Page ");
      paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
      run = paragraph.createRun();  
      run.setText(" of ");
      paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
    
      FileOutputStream out = new FileOutputStream("CreateWordHeaderFooter.docx");
      doc.write(out);
      out.close();
      doc.close();
    
     }
    }
    

    The fields in Word then are {PAGE \* MERGEFORMAT} and {NUMPAGES \* MERGEFORMAT}.

    For using current apache poi 4.1.2 one can do this without XWPFHeaderFooterPolicy using XWPFDocument.createHeader, XWPFDocument.createFooter and HeaderFooterType:

    ...
    //import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.wp.usermodel.HeaderFooterType;
    ...
    
      // create header-footer
      //XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
      //if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();
    
      // create header start
      //XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
      XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
    ...
      // create footer start
      //XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
      XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
    ...
    
    0 讨论(0)
提交回复
热议问题