Skip to content

Commit

Permalink
Fix paragraph removal from header/footer
Browse files Browse the repository at this point in the history
The implementation of how paragraphs are
removed from headers/footers was faulty,
and is fixed in this commit
  • Loading branch information
AntonOellerer committed Sep 4, 2024
1 parent 6e54df9 commit b94ef94
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '4.2.2'
version = '4.2.3'

java {
toolchain {
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/com/docutools/jocument/impl/word/WordUtilities.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.docutools.jocument.impl.word;

import com.docutools.jocument.impl.ParsingUtils;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFHeaderFooter;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
Expand Down Expand Up @@ -130,34 +133,40 @@ public static IBodyElement copyBefore(IBodyElement element, IBodyElement destina
*/
public static void removeIfExists(IBodyElement element) {
IBody body = element.getBody();
CTDocument1 document = body.getXWPFDocument().getDocument();
XWPFDocument xwpfDocument = body.getXWPFDocument();
CTDocument1 document = xwpfDocument.getDocument();
if (element instanceof XWPFParagraph xwpfParagraph) {
try (XmlCursor xmlCursor = xwpfParagraph.getCTP().newCursor()) {
XmlObject object = getParentObject(xmlCursor);
if (object.equals(document.getBody())) {
findPositionInBody(element).ifPresent(pos -> body.getXWPFDocument().removeBodyElement(pos));
findPositionInBody(element).ifPresent(xwpfDocument::removeBodyElement);
} else if (object instanceof CTTc) {
removeElementFromTable(xwpfParagraph);
} else if (object instanceof CTHdrFtr ctHdrFtr) {
xmlCursor.toParent();
new XWPFFooter(body.getXWPFDocument(), ctHdrFtr).removeParagraph(xwpfParagraph);
removeObjectFromDocument(ctHdrFtr, xwpfDocument, xwpfHeaderFooter -> xwpfHeaderFooter.removeParagraph(xwpfParagraph));
}
} catch (IOException e) {
throw new ElementRemovalException(e);
}
} else if (element instanceof XWPFTable xwpfTable) {
try (XmlCursor xmlCursor = xwpfTable.getCTTbl().newCursor()) {
XmlObject object = getParentObject(xmlCursor);
if (object.equals(document.getBody())) {
findPositionInBody(element).ifPresent(pos -> body.getXWPFDocument().removeBodyElement(pos));
findPositionInBody(element).ifPresent(xwpfDocument::removeBodyElement);
} else if (object instanceof CTTc) {
removeElementFromTable(xwpfTable);
} else if (object instanceof CTHdrFtr ctHdrFtr) {
xmlCursor.toParent();
new XWPFFooter(body.getXWPFDocument(), ctHdrFtr).removeTable(xwpfTable);
removeObjectFromDocument(ctHdrFtr, xwpfDocument, xwpfHeaderFooter -> xwpfHeaderFooter.removeTable(xwpfTable));
}
} catch (IOException e) {
throw new ElementRemovalException(e);
}
}
}

private static void removeObjectFromDocument(CTHdrFtr ctHdrFtr, XWPFDocument xwpfDocument, Consumer<XWPFHeaderFooter> removalAction) {
List<XWPFHeaderFooter> headerFooterList = new LinkedList<>(xwpfDocument.getHeaderList());
headerFooterList.addAll(xwpfDocument.getFooterList());
for (XWPFHeaderFooter xwpfHeaderFooter : headerFooterList) {
if (xwpfHeaderFooter._getHdrFtr().equals(ctHdrFtr)) {
removalAction.accept(xwpfHeaderFooter);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ void pictureInHeader() throws InterruptedException, IOException {
assertThat(document.completed(), is(true));
xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document);
assertThat(xwpfDocument.getHeaderArray(0).getAllPictures(), hasSize(1));
assertThat(xwpfDocument.getHeaderArray(0).getParagraphs().stream()
.noneMatch(xwpfParagraph -> xwpfParagraph.getParagraphText().equals("{{profilePic}}")), is(true));
}

@Test
Expand Down

0 comments on commit b94ef94

Please sign in to comment.