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

xqnpmsa8  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(349)

我试图使用apachepoi在每个页面中查找和替换不同页眉部分的文本,但是只得到空数据,但是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标题部分的屏幕截图。

2.docx标题另一部分的屏幕截图。

3.docx标题另一部分的屏幕截图。

4.屏幕截图

gz5pxeao

gz5pxeao1#

在一个 *.docx 文档,它包含多个节,每个节从一个段落开始,该段落设置了节属性。为了从节属性中获取页眉和页脚,有一个公共的xwpfheaderfooterpolicy(xwpfdocument doc,org.openxmlformats.schemas.wordprocessingml.x2006.main.ctsectpr)构造函数。
在文档的正文中仅设置最后一节的节属性。
因此,下面的代码应该从文档的所有部分中获取所有页眉和页脚。

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

 }
}
ig9co6j1

ig9co6j12#

这个功能应该可以完成这项工作

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

相关问题