java - 如何使用 Apache POI 清空 XWPFDocument (DOCX) 中的所有页眉和页脚?
问题描述
以下 Java 代码已成功删除特定 DOCX 文件的页眉和页脚中的所有内容,除了一个页脚(它是第一页页脚)。在检查 DOCX 时,顽皮的页脚有下面的 XML。您将如何删除其内容?
document = new XWPFDocument(new FileInputStream(filePath));
List<XWPFHeader> headers = document.getHeaderList();
for (XWPFHeader h : headers) {
ArrayList<XWPFParagraph> hParaArray = new ArrayList<XWPFParagraph>();
for (XWPFParagraph hPara : h.getParagraphs())
hParaArray.add(hPara);
hParaArray.forEach(hPara -> {
h.removeParagraph(hPara);
});
ArrayList<XWPFTable> hTblArray = new ArrayList<XWPFTable>();
for (XWPFTable hTbl : h.getTables())
hTblArray.add(hTbl);
hTblArray.forEach(hTbl -> {
h.removeTable(hTbl);
});
}
List<XWPFFooter> footers = document.getFooterList();
for (XWPFFooter f : footers) {
ArrayList<XWPFParagraph> fParaArray = new ArrayList<XWPFParagraph>();
for (XWPFParagraph fPara : f.getParagraphs())
fParaArray.add(fPara);
fParaArray.forEach(fPara -> {
f.removeParagraph(fPara);
});
ArrayList<XWPFTable> fTblArray = new ArrayList<XWPFTable>();
for (XWPFTable fTbl : f.getTables())
fTblArray.add(fTbl);
fTblArray.forEach(fTbl -> {
f.removeTable(fTbl);
});
}
页脚3.xml:
<?xml version="1.0" encoding="UTF-8"?>
<w:ftr mc:Ignorable="w14 w15 w16se wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rFonts w:cs="Arial" />
<w:color w:val="0000FF" />
<w:sz w:val="16" />
<w:szCs w:val="16" />
<w:lang w:val="en_US" />
</w:rPr>
<w:id w:val="6695195" />
<w:placeholder>
<w:docPart w:val="68B9E76BF9434A3FAABE5342BB8B54F7" />
</w:placeholder>
</w:sdtPr>
<w:sdtEndPr />
<w:sdtContent>
<w:p w:rsidR="00A47874" w:rsidRPr="004D34A5" w:rsidRDefault="00945F6E" w:rsidP="00A47874">
<w:pPr>
<w:pBdr>
<w:top w:val="single" w:sz="4" w:space="1" w:color="auto" />
</w:pBdr>
<w:rPr>
<w:rFonts w:cs="Arial" />
<w:color w:val="0000FF" />
<w:sz w:val="16" />
<w:szCs w:val="16" />
<w:lang w:val="en_US" />
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:cs="Arial" />
<w:color w:val="0000FF" />
<w:sz w:val="16" />
<w:szCs w:val="16" />
<w:lang w:val="en_US" />
</w:rPr>
<w:t>Some text that couldn't be removed</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
</w:ftr>
解决方案
w:sdt
页脚中的StructuredDocumentTag
aka ContentControl。Apache POI
为此只有实验课XWPFSDT。虽然它提供了removeParagraph
and ,但它直到现在removeTable
还缺乏in和. 因此,使用您的方法无法从页脚中删除 s 。removeSDT
XWPFHeaderFooter
XWPFDocument
StructuredDocumentTag
但是,如果需要完全清空所有现有的页眉和页脚,则可以使用XWPFHeaderFooter.setHeaderFooter简单地用新的空内容覆盖所有页眉和页脚内容。
例子:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordDoEmptyingHeaderFooter {
public static void main(String[] args) throws Exception {
String inFilePath = "./WordDocument.docx";
String outFilePath = "./WordDocumentNew.docx";
XWPFDocument document = new XWPFDocument(new FileInputStream(inFilePath));
for (XWPFHeader header : document.getHeaderList()) {
header.setHeaderFooter(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr.Factory.newInstance());
}
for (XWPFFooter footer : document.getFooterList()) {
footer.setHeaderFooter(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr.Factory.newInstance());
}
FileOutputStream out = new FileOutputStream(outFilePath);
document.write(out);
out.close();
document.close();
}
}
推荐阅读
- flutter - Flutter isSBSettingEnabled
- reactjs - 当我尝试“npm install gh-pages --save-dev”时出现错误。这是用于在 GitHub 上托管网站
- xml - Starshipit / DHL DEC 不从产品中导入“commodity_code”
- iframe - 关闭 iframe 弹出窗口 - 跨域
- ios - 无需按下操作按钮即可自动滑动 UITableViewCell 并触发操作
- r - ggplot 访问数据标签而不是数据值
- r - 使用带有基于部分匹配的列名的正则表达式将字符串列拆分为字符串矩阵
- java - Spring Data JPA:连接列上的 FindBy
- excel - vba 歧视的问题
- javascript - HTML/JS 输出表不显示水平边框