gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:58:23 25 4
gpt4 key购买 nike

我试图使用 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 header 部分的屏幕截图。

Docx header sections

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

Docx header another section

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

Docx header another section

4.屏幕截图

Screen Shot

最佳答案

在包含多个部分的 *.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);

}
}

关于java - 使用java查找并替换docx中每个部分的不同标题中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com