使用Java查找并替换docx中每个部分的不同标题中的文本

时间:2018-12-07 05:55:05

标签: java apache apache-poi docx

我正在尝试使用Apache poi在每个页面的标题的不同部分中查找和替换文本,但仅获取空数据,但是Docx的标题和页脚也有所不同

    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.Docx标头部分的屏幕截图。

Docx header sections

2。Docx标头的屏幕截图另一部分。

Docx header another section

3.Docx标头的另一部分的屏幕截图。

Docx header another section

4。屏幕截图

Screen Shot

2 个答案:

答案 0 :(得分:1)

在包含多个节的*.docx文档中,每个节都从设置了节属性的段落开始。为了使页眉和页脚不在节属性中,有public XWPFHeaderFooterPolicy(XWPFDocument doc, org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr sectPr)构造函数。

仅在文档的正文中设置最后一节的节属性。

因此,以下代码应将文档中所有节中的所有页眉和页脚都删除。

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);

 }
}

答案 1 :(得分:0)

此功能可以完成工作

static void replaceHeaderText(XWPFDocument document, String searchValue, String replacement)
{
    List<XWPFHeader> headers = document.getHeaderList();
    for(XWPFHeader h : headers)
    {
        for (XWPFParagraph p : h.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if (text != null && text.contains(searchValue)) {
                        text = text.replace(searchValue, replacement);
                        r.setText(text, 0);
                    }
                }
            }
        }
        for (XWPFTable tbl : h.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph p : cell.getParagraphs()) {
                        for (XWPFRun r : p.getRuns()) {
                            String text = r.getText(0);
                            if (text != null && text.contains(searchValue)) {
                                text = text.replace(searchValue, replacement);
                                r.setText(text,0);
                            }
                        }
                    }
                }
            }
        }
    }
}