Skip to content

Commit

Permalink
Remove last word page if it is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed Jan 15, 2025
1 parent 2746511 commit 8eec3e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
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 = '8.1.0'
version = '8.2.0'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.apache.logging.log4j.Logger;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class WordDocumentImpl extends DocumentImpl {
private static final Logger logger = LogManager.getLogger();
Expand All @@ -35,6 +37,8 @@ protected Path generate() throws IOException {
logger.debug("Retrieved all body elements, starting WordGenerator");
WordGenerator.apply(resolver, bodyElements, options);

cleanLastEmptyPage(document);

document.enforceUpdateFields();

try (OutputStream os = Files.newOutputStream(file)) {
Expand All @@ -46,4 +50,32 @@ protected Path generate() throws IOException {
return file;
}

private void cleanLastEmptyPage(XWPFDocument document) {
List<IBodyElement> elements = document.getBodyElements();
int elementsToRemove = 0;
for (int i = elements.size() - 1; i >= 0; i--) {
IBodyElement element = elements.get(i);
if (element instanceof XWPFParagraph xwpfParagraph && isEmpty(xwpfParagraph)) {
elementsToRemove++;
} else {
break;
}
}
for (; elementsToRemove > 0; elementsToRemove--) {
document.removeBodyElement(elements.size() - 1);
}
}

private boolean isEmpty(XWPFParagraph xwpfParagraph) {
return xwpfParagraph.isPageBreak() || (xwpfParagraph.getText().trim().isEmpty() && !hasPictures(xwpfParagraph));
}

private boolean hasPictures(XWPFParagraph paragraph) {
for (XWPFRun xwpfRun : paragraph.getRuns()) {
if (!xwpfRun.getEmbeddedPictures().isEmpty()) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,21 @@ void preservesLinebreaksInStrings() throws InterruptedException, IOException {
assertThat(xwpfRun.getText(1), equalTo(" Socci Mignon "));
assertThat(xwpfRun.getText(2), equalTo(" Ellworths"));
}

@Test
void deletesEmptyPage() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/word/EmptyTemplate.docx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.LINEBREAK_NAME_PERSON);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(60000L); // 1 minute

// Assert
assertThat(document.completed(), is(true));
xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document);
assertThat(xwpfDocument.getBodyElements().size(), equalTo(0));
}
}
Binary file not shown.

0 comments on commit 8eec3e8

Please sign in to comment.