Skip to content

Commit

Permalink
Fix run text removal (#260)
Browse files Browse the repository at this point in the history
In 0d0a6ad, a bug was introduced where
paragraph and run text was removed
incorrectly.
This commit fixes this by correctly
setting the removal index and replacing
the run text array.
  • Loading branch information
AntonOellerer authored Oct 10, 2024
1 parent 0d0a6ad commit 5c67904
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 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.6'
version = '4.2.7'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;

public class WordUtilities {
Expand Down Expand Up @@ -67,14 +68,11 @@ public static void replaceText(XWPFParagraph paragraph, String newText) {
// When deleting a run from a paragraph, the collection keeping the runs shrinks to fit to the new size
// If we delete the runs with indices 1,2,3...,x, the second half of the delete operations fails silently
// To avoid this, we simply delete the first run x times.
IntStream.range(1, runs.size()).forEach(value -> paragraph.removeRun(0));
IntStream.range(1, runs.size()).forEach(value -> paragraph.removeRun(1));
}

private static void removeRunText(XWPFRun run) {
int sizeOfTextArray = run.getCTR().sizeOfTArray();
for (int i = 0; i < sizeOfTextArray; i++) {
run.setText("", i);
}
run.getCTR().setTArray(new CTText[]{});
}

private static void insertLines(String[] lines, XWPFRun run) {
Expand Down

0 comments on commit 5c67904

Please sign in to comment.