Browse Source

Fix when bodyElements contain sdt, the inserted element is in the wrong position

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1906225 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_4
Bida Fan 1 year ago
parent
commit
98d51e4139

+ 1
- 1
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java View File

@@ -842,7 +842,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
cursor.toCursor(newParaPos);
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
if (o instanceof CTP || o instanceof CTTbl || o instanceof CTSdtBlock) {
i++;
}
}

+ 16
- 0
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java View File

@@ -464,6 +464,22 @@ public final class TestXWPFDocument {
}
}

@Test
void testInsertNewParagraphWithSdt() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
doc.createTOC();
doc.createParagraph();
try (XWPFDocument docx = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
XWPFParagraph paragraph = docx.getParagraphArray(0);
XmlCursor xmlCursor = paragraph.getCTP().newCursor();
XWPFParagraph insertNewParagraph = docx.insertNewParagraph(xmlCursor);

assertEquals(insertNewParagraph, docx.getParagraphs().get(0));
assertEquals(insertNewParagraph, docx.getBodyElements().get(1));
}
}
}

@Test
@Disabled("XWPF should be able to write to a new Stream when opened Read-Only")
void testWriteFromReadOnlyOPC() throws Exception {

Loading…
Cancel
Save