Reputation: 1104
The following Java code has successfully removed all the contents in the headers and footers in a specific DOCX file, except one footer (it was a first page footer). Upon inspecting the DOCX, the naughty footer has the XML below. How would you remove its content?
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);
});
}
footer3.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>
Upvotes: 1
Views: 1499
Reputation: 61945
The w:sdt
in your footer is a StructuredDocumentTag
aka ContentControl. Apache POI
has only experimental class XWPFSDT for this. And while it provides removeParagraph
and removeTable
, it lacks removeSDT
until now in XWPFHeaderFooter
as well as in XWPFDocument
. So using your approach there is no way to remove the StructuredDocumentTag
s from the footer.
But if the need is to totally emptying all present headers and footers, then one could simply overwrite all header and footer content with new empty ones using XWPFHeaderFooter.setHeaderFooter.
Example:
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();
}
}
Upvotes: 2