Skip to content
Advertisement

find and replace a text in different header for each section in docx using java

I am trying to find and replace a text different sections of header in each page using Apache poi but getting only null data, but Docx has different header sections and footer too

    package com.concretepage;
    import java.io.FileInputStream;
    import org.apache.poi.openxml4j.opc.OPCPackage;
    import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFFooter;
    import org.apache.poi.xwpf.usermodel.XWPFHeader;
    public class ReadDOCXHeaderFooter {
        public static void main(String[] args) {
            try {
                FileInputStream fis = new FileInputStream("D:/docx/read-test.docx");
                XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis));
                XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(xdoc);
                //read header
 
                for(int i=0;i<90;i++)
                {
                 XWPFHeader header = policy.getHeader(i);
       List<XWPFRun> runs = header.getRuns();
  if (runs != null) {
         for (XWPFRun r : runs) {
          String text = r.getText(0);
          if (text != null && text.contains("$$key$$")) {
           text = text.replace("$$key$$", "ABCD");//your content
           r.setText(text, 0);
          }
         }
                    System.out.println(header.getText());

                    //read footer
                    XWPFFooter footer = policy.getFooter(i);
                    System.out.println(footer.getText());
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    }

1.Screen shot of Docx header sections.

Docx header sections

2.Screen shot of Docx header another section.

Docx header another section

3.Screen shot of Docx header another section.

Docx header another section

4.Screen Shot

Screen Shot

Advertisement

Answer

In a *.docx document, which contains multiple sections, each section starts in a paragraph which has section properties set. To get the headers and footers out of section properties there is public XWPFHeaderFooterPolicy(XWPFDocument doc, org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr sectPr) constructor.

Only the section properties for the last section are set in document’s body.

So the following code should get all headers and footers out of all sections in the document.

import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

public class ReadWordAllHeaderFooters {

 static void getAllHeaderFooterFromPolicy(XWPFHeaderFooterPolicy headerFooterPolicy) {
  XWPFHeader header;
  XWPFFooter footer;
  header = headerFooterPolicy.getDefaultHeader();
  if (header != null) System.out.println("DefaultHeader: " + header.getText());
  header = headerFooterPolicy.getFirstPageHeader();
  if (header != null) System.out.println("FirstPageHeader: " + header.getText());
  header = headerFooterPolicy.getEvenPageHeader();
  if (header != null) System.out.println("EvenPageHeader: " + header.getText());
  header = headerFooterPolicy.getOddPageHeader();
  if (header != null) System.out.println("OddPageHeader: " + header.getText());

  footer = headerFooterPolicy.getDefaultFooter();
  if (footer != null) System.out.println("DefaultFooter: " + footer.getText());
  footer = headerFooterPolicy.getFirstPageFooter();
  if (footer != null) System.out.println("FirstPageFooter: " + footer.getText());
  footer = headerFooterPolicy.getEvenPageFooter();
  if (footer != null) System.out.println("EvenPageFooter: " + footer.getText());
  footer = headerFooterPolicy.getOddPageFooter();
  if (footer != null) System.out.println("OddPageFooter: " + footer.getText());
 }

 public static void main(String[] args) throws Exception {
  XWPFDocument document = new XWPFDocument(new FileInputStream("MultipleHeaderFooters.docx"));
  XWPFHeaderFooterPolicy headerFooterPolicy;

  //are there paragraphs to start sections?
  int section = 1;
  for (XWPFParagraph paragraph : document.getParagraphs()) {
   if (paragraph.getCTP().isSetPPr()) { //paragraph has paragraph properties set
    if (paragraph.getCTP().getPPr().isSetSectPr()) { //paragraph property has section properties set
     //headers and footers in paragraphs section properties:
     headerFooterPolicy = new XWPFHeaderFooterPolicy(document, paragraph.getCTP().getPPr().getSectPr());
     System.out.println("headers and footers in section properties of section " + section++ + ":");
     getAllHeaderFooterFromPolicy(headerFooterPolicy);
    }
   }
  }

  //headers and footers in documents body = headers and footers of last section:
  headerFooterPolicy = new XWPFHeaderFooterPolicy(document);
  System.out.println("headers and footers in documents body = headers and footers of last section " + section + ":");
  getAllHeaderFooterFromPolicy(headerFooterPolicy);

 }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement