This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new fe4184d649 Fix Bug 66263 — Add support for reading SDT row in tables 
(#971)
fe4184d649 is described below

commit fe4184d649c0f7950e3a610c7dbc0591b7a52e3e
Author: Maksim Novozhilov <[email protected]>
AuthorDate: Wed Aug 12 00:53:21 2026 +0500

    Fix Bug 66263 — Add support for reading SDT row in tables (#971)
    
    * Fix Bug 66263 — Add support for SDT row in tables
    
    * remove repeated if
    
    * add missing imports
    
    * add tests
    
    * add tests
    
    * add tests with file + fix paragraph duplication
    
    * fix indentation
    
    * fix AI comments
    
    * fix create cursor
    
    * Update XWPFTable.java
    
    Update XWPFTable.java
    
    * Update XWPFTable.java
    
    * merge issue
    
    * fix review issue
    
    * more changes
    
    ---------
    
    Co-authored-by: mnovozhilov <[email protected]>
    Co-authored-by: PJ Fanning <[email protected]>
---
 .../apache/poi/xwpf/usermodel/XWPFParagraph.java   |  25 +++
 .../org/apache/poi/xwpf/usermodel/XWPFTable.java   | 248 +++++++++++++++++---
 .../poi/xwpf/usermodel/TestXWPFParagraph.java      | 153 +++++++++++++
 .../apache/poi/xwpf/usermodel/TestXWPFTable.java   | 249 +++++++++++++++++++++
 test-data/document/Bug66263-paragraph.docx         | Bin 0 -> 2012 bytes
 test-data/document/Bug66263-table.docx             | Bin 0 -> 1970 bytes
 6 files changed, 646 insertions(+), 29 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
index 2ff9ed0027..b7014df5ff 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
@@ -136,10 +136,21 @@ public class XWPFParagraph implements IBodyElement, 
IRunBody, ISDTContents, Para
                 if (o instanceof CTSdtBlock block) {
                     XWPFSDT cc = new XWPFSDT(block, part);
                     iruns.add(cc);
+                    CTSdtContentBlock content = block.getSdtContent();
+                    if (content != null) {
+                        for (CTP ctp : content.getPList()) {
+                            processCTRs(ctp.getRList());
+                        }
+                    }
                 }
                 if (o instanceof CTSdtRun run) {
                     XWPFSDT cc = new XWPFSDT(run, part);
                     iruns.add(cc);
+
+                    CTSdtContentRun sdtContent = run.getSdtContent();
+                    if (sdtContent != null) {
+                        processCTRs(sdtContent.getRList());
+                    }
                 }
                 if (o instanceof CTRunTrackChange parentRecord) {
                     for (CTR r : parentRecord.getRArray()) {
@@ -161,6 +172,20 @@ public class XWPFParagraph implements IBodyElement, 
IRunBody, ISDTContents, Para
         }
     }
 
+    private void processCTRs(List<CTR> ctrs) {
+        if (ctrs == null) {
+            return;
+        }
+        for (CTR ctr : ctrs) {
+            // Only add runs that have formatting properties (RPr).
+            // SDT content runs are added to the runs list for formatting 
access,
+            // while the SDT element itself is in iruns for text extraction.
+            if (ctr.getRPr() != null) {
+                runs.add(new XWPFRun(ctr, (IRunBody) this));
+            }
+        }
+    }
+
     @Internal
     public CTP getCTP() {
         return paragraph;
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
index 273635e022..e6115bd0fb 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
@@ -30,11 +30,15 @@ import org.apache.poi.ooxml.util.POIXMLUnits;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.MathUtil;
 import org.apache.poi.util.Units;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlObject;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJcTable;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRow;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
@@ -165,28 +169,67 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
         this.ctTbl = table;
 
         // is an empty table: I add one row and one column as default
-        if (initRow && table.sizeOfTrArray() == 0) {
+        // Check if table has any row-like content (either TR or SDT-wrapped 
rows)
+        if (initRow && table.sizeOfTrArray() == 0 && !hasSdtRows(table)) {
             createEmptyTable(table);
         }
 
-        for (CTRow row : table.getTrList()) {
-            StringBuilder rowText = new StringBuilder();
-            XWPFTableRow tabRow = new XWPFTableRow(row, this);
-            tableRows.add(tabRow);
-            for (CTTc cell : row.getTcList()) {
-                for (CTP ctp : cell.getPList()) {
-                    XWPFParagraph p = new XWPFParagraph(ctp, part);
-                    if (!rowText.isEmpty()) {
-                        rowText.append('\t');
+        try (XmlCursor cursor = table.newCursor()) {
+            cursor.selectPath("./*");
+            while (cursor.toNextSelection()) {
+                XmlObject xmlObject = cursor.getObject();
+                if (xmlObject instanceof CTRow ctRow) {
+                    processCTRow(ctRow);
+                }
+                else if (xmlObject instanceof CTSdtRow sdtRow) {
+                    List<CTRow> rows = new ArrayList<>();
+                    collectCTRowsInnerSdtRow(sdtRow, rows);
+                    for (CTRow row : rows) {
+                        processCTRow(row);
                     }
-                    rowText.append(p.getText());
                 }
             }
-            if (!rowText.isEmpty()) {
-                this.text.append(rowText);
-                this.text.append('\n');
+        }
+    }
+
+    private void processCTRow(CTRow row) {
+        StringBuilder rowText = new StringBuilder();
+        XWPFTableRow tableRow = new XWPFTableRow(row, this);
+        tableRows.add(tableRow);
+        for (CTTc cell : row.getTcList()) {
+            for (CTP ctp : cell.getPList()) {
+                XWPFParagraph p = new XWPFParagraph(ctp, part);
+                if (!rowText.isEmpty()) {
+                    rowText.append('\t');
+                }
+                rowText.append(p.getText());
             }
         }
+        if (!rowText.isEmpty()) {
+            this.text.append(rowText);
+            this.text.append('\n');
+        }
+    }
+
+    private void collectCTRowsInnerSdtRow(CTSdtRow sdtRow, List<CTRow> rows) {
+        CTSdtContentRow sdtContent = sdtRow.getSdtContent();
+        if (sdtContent == null) {
+            return;
+        }
+
+        try (XmlCursor cursor = sdtContent.newCursor()) {
+            if (!cursor.toFirstChild()) {
+                return;
+            }
+            do {
+                XmlObject child = cursor.getObject();
+                if (child instanceof CTRow ctRow) {
+                    rows.add(ctRow);
+                } else if (child instanceof CTSdtRow sdtChild) {
+                    collectCTRowsInnerSdtRow(sdtChild, rows);
+                }
+            } while (cursor.toNextSibling());
+        }
     }
 
     private void createEmptyTable(CTTbl table) {
@@ -216,6 +259,27 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
         //getRows();
     }
 
+    /**
+     * Check if the table contains any SDT-wrapped rows.
+     * This is needed to avoid adding an extra blank row to tables that
+     * already have row-like content, even if it's wrapped in SDT elements.
+     *
+     * @param table the CTTbl to check
+     * @return true if the table contains any SDT row elements
+     */
+    private boolean hasSdtRows(CTTbl table) {
+        try (XmlCursor cursor = table.newCursor()) {
+            cursor.selectPath("./*");
+            while (cursor.toNextSelection()) {
+                XmlObject xmlObject = cursor.getObject();
+                if (xmlObject instanceof CTSdtRow) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     /**
      * @return ctTbl object
      */
@@ -267,13 +331,16 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
     }
 
     /**
+     * Returns the row at the specified position, including SDT-wrapped rows.
+     * Note: the returned row may be a direct child of the table or nested
+     * inside an SDT (structured document tag) row element.
+     *
      * @param pos - index of the row
-     * @return the row at the position specified or null if no rows is defined 
or if the position is greather than the max size of rows array
+     * @return the row at the position specified or null if no rows is defined 
or if the position is greater than the max size of rows array
      */
     public XWPFTableRow getRow(int pos) {
-        if (pos >= 0 && pos < ctTbl.sizeOfTrArray()) {
-            //return new XWPFTableRow(ctTbl.getTrArray(pos));
-            return getRows().get(pos);
+        if (pos >= 0 && pos < tableRows.size()) {
+            return tableRows.get(pos);
         }
         return null;
     }
@@ -303,10 +370,10 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
     }
 
     /**
-     * @return number of rows in table
+     * @return number of rows in table (including SDT-wrapped rows)
      */
     public int getNumberOfRows() {
-        return ctTbl.sizeOfTrArray();
+        return tableRows.size();
     }
 
     /**
@@ -1106,8 +1173,9 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      * @param row the row which should be added
      */
     public void addRow(XWPFTableRow row) {
-        ctTbl.addNewTr();
-        ctTbl.setTrArray(getNumberOfRows() - 1, row.getCtRow());
+        // Add to end of table
+        CTRow addedRow = ctTbl.addNewTr();
+        addedRow.set(row.getCtRow());
         tableRows.add(row);
     }
 
@@ -1119,8 +1187,55 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      */
     public boolean addRow(XWPFTableRow row, int pos) {
         if (pos >= 0 && pos <= tableRows.size()) {
-            ctTbl.insertNewTr(pos);
-            ctTbl.setTrArray(pos, row.getCtRow());
+            if (pos == tableRows.size()) {
+                // Add to end of table
+                CTRow addedRow = ctTbl.addNewTr();
+                addedRow.set(row.getCtRow());
+            } else {
+                // Insert before the row currently at position pos
+                XWPFTableRow nextRow = tableRows.get(pos);
+                CTRow nextCTRow = nextRow.getCtRow();
+
+                // Find where to insert by checking the parent of the next row
+                try (XmlCursor cursor = nextCTRow.newCursor()) {
+                    if (cursor.toParent()) {
+                        XmlObject parent = cursor.getObject();
+                        if (parent instanceof CTTbl tbl) {
+                            // Top-level row - insert into table
+                            List<CTRow> trList = tbl.getTrList();
+                            int idx = trList.indexOf(nextCTRow);
+                            if (idx >= 0) {
+                                CTRow addedRow = tbl.insertNewTr(idx);
+                                addedRow.set(row.getCtRow());
+                            } else {
+                                // Fallback: add to end
+                                CTRow addedRow = ctTbl.addNewTr();
+                                addedRow.set(row.getCtRow());
+                            }
+                        } else if (parent instanceof CTSdtContentRow 
sdtContent) {
+                            // SDT-wrapped row - insert into SDT content
+                            List<CTRow> trList = sdtContent.getTrList();
+                            int idx = trList.indexOf(nextCTRow);
+                            if (idx >= 0) {
+                                CTRow addedRow = sdtContent.insertNewTr(idx);
+                                addedRow.set(row.getCtRow());
+                            } else {
+                                // Fallback: add to end of SDT content
+                                CTRow addedRow = sdtContent.addNewTr();
+                                addedRow.set(row.getCtRow());
+                            }
+                        } else {
+                            // Unknown parent - add to table as fallback
+                            CTRow addedRow = ctTbl.addNewTr();
+                            addedRow.set(row.getCtRow());
+                        }
+                    } else {
+                        // No parent - add to table as fallback
+                        CTRow addedRow = ctTbl.addNewTr();
+                        addedRow.set(row.getCtRow());
+                    }
+                }
+            }
             tableRows.add(pos, row);
             return true;
         }
@@ -1128,14 +1243,59 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
     }
 
     /**
-     * inserts a new tablerow
+     * inserts a new tablerow at position pos
      *
+     * @param pos the position to insert the new row
      * @return the inserted row
      */
     public XWPFTableRow insertNewTableRow(int pos) {
         if (pos >= 0 && pos <= tableRows.size()) {
-            CTRow row = ctTbl.insertNewTr(pos);
-            XWPFTableRow tableRow = new XWPFTableRow(row, this);
+            CTRow insertedRow;
+
+            if (pos == tableRows.size() || tableRows.isEmpty()) {
+                // Insert at end or first row in empty table - add to table
+                insertedRow = ctTbl.addNewTr();
+            } else {
+                // Insert before the row currently at position pos
+                XWPFTableRow nextRow = tableRows.get(pos);
+                CTRow nextCTRow = nextRow.getCtRow();
+
+                // Find where to insert by checking the parent of the next row
+                try (XmlCursor cursor = nextCTRow.newCursor()) {
+                    if (cursor.toParent()) {
+                        XmlObject parent = cursor.getObject();
+                        if (parent instanceof CTTbl tbl) {
+                            // Top-level row - insert into table
+                            List<CTRow> trList = tbl.getTrList();
+                            int idx = trList.indexOf(nextCTRow);
+                            if (idx >= 0) {
+                                insertedRow = tbl.insertNewTr(idx);
+                            } else {
+                                // Fallback: add to end of table
+                                insertedRow = ctTbl.addNewTr();
+                            }
+                        } else if (parent instanceof CTSdtContentRow 
sdtContent) {
+                            // SDT-wrapped row - insert into SDT content
+                            List<CTRow> trList = sdtContent.getTrList();
+                            int idx = trList.indexOf(nextCTRow);
+                            if (idx >= 0) {
+                                insertedRow = sdtContent.insertNewTr(idx);
+                            } else {
+                                // Fallback: add to end of SDT content
+                                insertedRow = sdtContent.addNewTr();
+                            }
+                        } else {
+                            // Unknown parent - add to table as fallback
+                            insertedRow = ctTbl.addNewTr();
+                        }
+                    } else {
+                        // No parent - add to table as fallback
+                        insertedRow = ctTbl.addNewTr();
+                    }
+                }
+            }
+
+            XWPFTableRow tableRow = new XWPFTableRow(insertedRow, this);
             tableRows.add(pos, tableRow);
             return tableRow;
         }
@@ -1149,9 +1309,39 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      */
     public boolean removeRow(int pos) throws IndexOutOfBoundsException {
         if (pos >= 0 && pos < tableRows.size()) {
-            if (ctTbl.sizeOfTrArray() > 0) {
-                ctTbl.removeTr(pos);
+            XWPFTableRow row = tableRows.get(pos);
+            CTRow ctRow = row.getCtRow();
+
+            // Remove the row from its actual parent (could be CTTbl or 
CTSdtContentRow)
+            try (XmlCursor cursor = ctRow.newCursor()) {
+                if (cursor.toParent()) {
+                    XmlObject parent = cursor.getObject();
+                    if (parent instanceof CTTbl tbl) {
+                        // Top-level row - find its actual position in tr array
+                        List<CTRow> trList = tbl.getTrList();
+                        int idx = trList.indexOf(ctRow);
+                        if (idx >= 0) {
+                            tbl.removeTr(idx);
+                        }
+                    } else if (parent instanceof CTSdtContentRow sdtContent) {
+                        // SDT-wrapped row - remove from SDT content
+                        List<CTRow> trList = sdtContent.getTrList();
+                        int idx = trList.indexOf(ctRow);
+                        if (idx >= 0) {
+                            sdtContent.removeTr(idx);
+                        }
+                    } else {
+                        // Fallback: try removing from the table directly
+                        List<CTRow> trList = ctTbl.getTrList();
+                        int idx = trList.indexOf(ctRow);
+                        if (idx >= 0) {
+                            ctTbl.removeTr(idx);
+                        }
+                    }
+                }
             }
+
+            // Remove from our list
             tableRows.remove(pos);
             return true;
         }
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java 
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
index e29ae8c0d2..18f5e72596 100644
--- 
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
+++ 
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
@@ -44,6 +44,8 @@ import 
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextAlignment;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
@@ -987,4 +989,155 @@ public final class TestXWPFParagraph {
                     "A group of shapes\nWhere some contain text", 
p.getPictureText());
         }
     }
+
+    @Test
+    void testParagraphWithSdtRunAndContent() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            XWPFParagraph p = doc.createParagraph();
+
+            CTP ctp = p.getCTP();
+
+            CTR normalRun = ctp.addNewR();
+            normalRun.addNewT().setStringValue("Before ");
+
+            CTSdtRun sdtRun = ctp.addNewSdt();
+            CTSdtContentRun content = sdtRun.addNewSdtContent();
+
+            CTR innerRun = content.addNewR();
+            innerRun.addNewRPr().addNewB().setVal(STOnOff1.ON);
+            innerRun.addNewT().setStringValue("SDT Run Content");
+
+            CTR innerRun2 = content.addNewR();
+            innerRun2.addNewRPr().addNewI().setVal(STOnOff1.ON);
+            innerRun2.addNewT().setStringValue(" More");
+
+            CTR afterRun = ctp.addNewR();
+            afterRun.addNewT().setStringValue(" After");
+
+            XWPFParagraph newParagraph = new XWPFParagraph(ctp, doc);
+
+            // getRuns() includes: normal runs (always) + SDT content runs 
with RPr
+            // Before (1) + SDT Run Content (1) + More (1) + After (1) = 4 runs
+            List<XWPFRun> runs = newParagraph.getRuns();
+            assertEquals(4, runs.size(), "Should have 4 runs (2 normal + 2 SDT 
with RPr)");
+
+            // getIRuns() includes SDT elements, but SDT runs from processCTRs 
are NOT in iruns
+            // Before (1) + SDT (1) + After (1) = 3
+            List<IRunElement> iruns = newParagraph.getIRuns();
+            assertEquals(3, iruns.size(), "Should have 3 elements (SDT runs 
are not in iruns)");
+
+            // Verify text includes SDT content (text is also duplicated)
+            String text = newParagraph.getText();
+            assertTrue(text.contains("Before"), "Text should contain 
'Before'");
+            assertTrue(text.contains("SDT Run Content"), "Text should contain 
'SDT Run Content'");
+            assertTrue(text.contains(" More"), "Text should contain ' More'");
+            assertTrue(text.contains("After"), "Text should contain 'After'");
+        }
+    }
+
+    @Test
+    void testParagraphWithMultipleSdtRuns() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            XWPFParagraph p = doc.createParagraph();
+
+            CTP ctp = p.getCTP();
+
+            CTSdtRun sdtRun1 = ctp.addNewSdt();
+            CTSdtContentRun content1 = sdtRun1.addNewSdtContent();
+            CTR innerRun1 = content1.addNewR();
+            innerRun1.addNewRPr().addNewB().setVal(STOnOff1.ON);
+            innerRun1.addNewT().setStringValue("First");
+
+            CTR normalRun = ctp.addNewR();
+            normalRun.addNewT().setStringValue(" Middle ");
+
+            CTSdtRun sdtRun2 = ctp.addNewSdt();
+            CTSdtContentRun content2 = sdtRun2.addNewSdtContent();
+            CTR innerRun2 = content2.addNewR();
+            innerRun2.addNewRPr().addNewI().setVal(STOnOff1.ON);
+            innerRun2.addNewT().setStringValue("Second");
+
+            XWPFParagraph newParagraph = new XWPFParagraph(ctp, doc);
+
+            // getRuns() includes: normal run (always) + SDT content runs with 
RPr
+            // First (1) + Middle (1) + Second (1) = 3 runs
+            List<XWPFRun> runs = newParagraph.getRuns();
+            assertEquals(3, runs.size(), "Should have 3 runs (1 normal + 2 SDT 
with RPr)");
+
+            // getIRuns() includes SDT elements, but SDT runs from processCTRs 
are NOT in iruns
+            // SDT1 (1) + Middle (1) + SDT2 (1) = 3 (SDT runs are in runs, not 
iruns)
+            List<IRunElement> iruns = newParagraph.getIRuns();
+            assertEquals(3, iruns.size(), "Should have 3 elements (SDT runs 
are in runs, not iruns)");
+
+            // Verify text includes all content
+            String text = newParagraph.getText();
+            assertTrue(text.contains("First"), "Text should contain 'First' 
from SDT run");
+            assertTrue(text.contains("Middle"), "Text should contain 'Middle' 
from normal run");
+            assertTrue(text.contains("Second"), "Text should contain 'Second' 
from SDT run");
+        }
+    }
+
+    /**
+     * Bug 66263 — Test SDT runs in paragraph using sample document.
+     * Verifies that SDT runs with and without RPr are processed correctly.
+     */
+    @Test
+    void testSdtRunsFromSampleDocument() throws IOException {
+        try (XWPFDocument doc = 
XWPFTestDataSamples.openSampleDocument("Bug66263-paragraph.docx")) {
+            List<XWPFParagraph> paragraphs = doc.getParagraphs();
+
+            // Paragraph 1: SDT Run with RPr
+            XWPFParagraph paragraph1 = paragraphs.get(0);
+            String paragraph1Text = paragraph1.getText();
+            assertTrue(paragraph1Text.contains("Before"), "Paragraph 1 should 
contain 'Before'");
+            assertTrue(paragraph1Text.contains("SDT Run with RPr"), "Paragraph 
1 should contain SDT run with RPr");
+            assertTrue(paragraph1Text.contains("After"), "Paragraph 1 should 
contain 'After'");
+
+            // getRuns() contains: 2 normal runs (always added) + 1 SDT run 
(added because it has RPr) = 3 runs
+            List<XWPFRun> runs1 = paragraph1.getRuns();
+            assertEquals(3, runs1.size(), "Paragraph 1 should have 3 runs (2 
normal + 1 SDT with RPr)");
+            assertEquals("Before ", runs1.get(0).toString(), "First run should 
be 'Before'");
+            assertEquals("SDT Run with RPr", runs1.get(1).toString(), "Second 
run should be SDT content");
+            assertEquals(" After", runs1.get(2).toString(), "Third run should 
be 'After'");
+
+            // getIRuns() should contain all elements including SDT
+            List<IRunElement> iruns1 = paragraph1.getIRuns();
+            assertEquals(3, iruns1.size(), "Paragraph 1 should have 3 elements 
in getIRuns() (2 normal runs + 1 SDT)");
+
+            // Paragraph 2: SDT Run without RPr
+            XWPFParagraph paragraph2 = paragraphs.get(1);
+            String paragraph2Text = paragraph2.getText();
+            assertTrue(paragraph2Text.contains("Before No RPr"), "Paragraph 2 
should contain 'Before No RPr'");
+            assertTrue(paragraph2Text.contains("SDT Run without RPr"), 
"Paragraph 2 should contain SDT run without RPr");
+            assertTrue(paragraph2Text.contains("After"), "Paragraph 2 should 
contain 'After'");
+
+            // getRuns() contains: 2 normal runs (always added) + 0 SDT runs 
(no RPr) = 2 runs
+            List<XWPFRun> runs2 = paragraph2.getRuns();
+            assertEquals(2, runs2.size(), "Paragraph 2 should have 2 runs 
(normal runs only, no SDT runs without RPr)");
+            assertEquals("Before No RPr ", runs2.get(0).toString(), "First run 
should be 'Before No RPr'");
+            assertEquals(" After", runs2.get(1).toString(), "Second run should 
be 'After'");
+
+            // getIRuns() should still contain all elements
+            List<IRunElement> iruns2 = paragraph2.getIRuns();
+            assertEquals(3, iruns2.size(), "Paragraph 2 should have 3 elements 
in getIRuns() (2 normal runs + 1 SDT)");
+
+            // Paragraph 3: Multiple SDT Runs
+            XWPFParagraph paragraph3 = paragraphs.get(2);
+            String paragraph3Text = paragraph3.getText();
+            assertTrue(paragraph3Text.contains("First"), "Paragraph 3 should 
contain 'First' from first SDT");
+            assertTrue(paragraph3Text.contains("Middle"), "Paragraph 3 should 
contain 'Middle'");
+            assertTrue(paragraph3Text.contains("Second"), "Paragraph 3 should 
contain 'Second' from second SDT");
+
+            // getRuns() contains: 1 normal run (always added) + 2 SDT runs 
(both have RPr) = 3 runs
+            List<XWPFRun> runs3 = paragraph3.getRuns();
+            assertEquals(3, runs3.size(), "Paragraph 3 should have 3 runs (1 
normal + 2 SDT runs with RPr)");
+            assertTrue(runs3.get(0).toString().contains("First"), "First run 
should contain 'First'");
+            assertEquals(" Middle ", runs3.get(1).toString(), "Second run 
should be 'Middle'");
+            assertTrue(runs3.get(2).toString().contains("Second"), "Third run 
should contain 'Second'");
+
+            // getIRuns() should contain 3 elements (2 SDTs + 1 normal run)
+            List<IRunElement> iruns3 = paragraph3.getIRuns();
+            assertEquals(3, iruns3.size(), "Paragraph 3 should have 3 elements 
in getIRuns() (2 SDTs + 1 normal run)");
+        }
+    }
 }
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java 
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
index 8fe1f303c9..35a9a7bcb1 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
@@ -28,6 +28,8 @@ import org.junit.jupiter.api.Test;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRow;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblCellMar;
@@ -662,4 +664,251 @@ class TestXWPFTable {
             assertEquals(TableWidthType.AUTO, table1.getWidthType());
         }
     }
+
+    @Test
+    void testTableWithSdtRow() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            CTRow normalRow = table.addNewTr();
+            CTTc cell1 = normalRow.addNewTc();
+            CTP p1 = cell1.addNewP();
+            CTR r1 = p1.addNewR();
+            r1.addNewT().setStringValue("Normal Row Cell 1");
+
+            CTTc cell2 = normalRow.addNewTc();
+            CTP p2 = cell2.addNewP();
+            CTR r2 = p2.addNewR();
+            r2.addNewT().setStringValue("Normal Row Cell 2");
+
+            CTSdtRow sdtRow = table.addNewSdt();
+            CTSdtContentRow sdtContent = sdtRow.addNewSdtContent();
+            CTRow innerRow = sdtContent.addNewTr();
+
+            CTTc sdtCell1 = innerRow.addNewTc();
+            CTP sdtP1 = sdtCell1.addNewP();
+            CTR sdtR1 = sdtP1.addNewR();
+            sdtR1.addNewT().setStringValue("SDT Row Cell 1");
+
+            CTTc sdtCell2 = innerRow.addNewTc();
+            CTP sdtP2 = sdtCell2.addNewP();
+            CTR sdtR2 = sdtP2.addNewR();
+            sdtR2.addNewT().setStringValue("SDT Row Cell 2");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+
+            assertEquals(2, xtab.getNumberOfRows(), "Table should have 2 rows 
total (1 normal + 1 SDT)");
+
+            String text = xtab.getText();
+            assertTrue(text.contains("Normal Row Cell 1"), "Text should 
contain normal row cell 1");
+            assertTrue(text.contains("Normal Row Cell 2"), "Text should 
contain normal row cell 2");
+            assertTrue(text.contains("SDT Row Cell 1"), "Text should contain 
SDT row cell 1");
+            assertTrue(text.contains("SDT Row Cell 2"), "Text should contain 
SDT row cell 2");
+        }
+    }
+
+    @Test
+    void testTableWithNestedSdtRows() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            CTSdtRow outerSdtRow = table.addNewSdt();
+            CTSdtContentRow outerContent = outerSdtRow.addNewSdtContent();
+
+            CTSdtRow innerSdtRow = outerContent.addNewSdt();
+            CTSdtContentRow innerContent = innerSdtRow.addNewSdtContent();
+            CTRow row1 = innerContent.addNewTr();
+
+            CTTc cell1 = row1.addNewTc();
+            CTP p1 = cell1.addNewP();
+            CTR r1 = p1.addNewR();
+            r1.addNewT().setStringValue("Nested SDT Row");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+
+            String text = xtab.getText();
+            assertTrue(text.contains("Nested SDT Row"), "Text should contain 
nested SDT row content");
+        }
+    }
+
+
+    @Test
+    void testTableWithOnlySdtRow() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            CTSdtRow sdtRow = table.addNewSdt();
+            CTSdtContentRow sdtContent = sdtRow.addNewSdtContent();
+            CTRow innerRow = sdtContent.addNewTr();
+
+            CTTc cell = innerRow.addNewTc();
+            CTP p = cell.addNewP();
+            CTR r = p.addNewR();
+            r.addNewT().setStringValue("Only SDT Row");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+
+            assertEquals(1, xtab.getNumberOfRows(), "Table has 1 SDT row");
+            assertEquals(1, xtab.getRows().size(), "Table should have 1 SDT 
row");
+
+            String text = xtab.getText();
+            assertTrue(text.contains("Only SDT Row"), "Text should contain SDT 
row content");
+        }
+    }
+
+    @Test
+    void testTableWithMultipleSdtRows() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            // First SDT row
+            CTSdtRow sdtRow1 = table.addNewSdt();
+            CTSdtContentRow sdtContent1 = sdtRow1.addNewSdtContent();
+            CTRow row1 = sdtContent1.addNewTr();
+            CTTc cell1 = row1.addNewTc();
+            CTP p1 = cell1.addNewP();
+            CTR r1 = p1.addNewR();
+            r1.addNewT().setStringValue("SDT Row 1");
+
+            // Second SDT row
+            CTSdtRow sdtRow2 = table.addNewSdt();
+            CTSdtContentRow sdtContent2 = sdtRow2.addNewSdtContent();
+            CTRow row2 = sdtContent2.addNewTr();
+            CTTc cell2 = row2.addNewTc();
+            CTP p2 = cell2.addNewP();
+            CTR r2 = p2.addNewR();
+            r2.addNewT().setStringValue("SDT Row 2");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+
+            assertEquals(2, xtab.getNumberOfRows(), "Table has 2 SDT rows");
+            assertEquals(2, xtab.getRows().size(), "Table should have 2 SDT 
rows");
+
+            String text = xtab.getText();
+            assertTrue(text.contains("SDT Row 1"), "Text should contain first 
SDT row");
+            assertTrue(text.contains("SDT Row 2"), "Text should contain second 
SDT row");
+        }
+    }
+
+    /**
+     * Bug 66263 — Test SDT row support using sample document.
+     * Verifies that SDT rows from sample document are correctly processed.
+     */
+    @Test
+    void testSdtRowFromSampleDocument() throws IOException {
+        try (XWPFDocument doc = 
XWPFTestDataSamples.openSampleDocument("Bug66263-table.docx")) {
+            XWPFTable table = doc.getTables().get(0);
+
+            // Verify SDT row text extraction
+            String tableText = table.getText();
+            assertTrue(tableText.contains("SDT Cell 1"), "Table should contain 
SDT cell 1");
+            assertTrue(tableText.contains("SDT Cell 2"), "Table should contain 
SDT cell 2");
+
+            // Verify SDT row is accessible via getRows()
+            List<XWPFTableRow> rows = table.getRows();
+            assertEquals(1, rows.size(), "Table should have 1 SDT row");
+
+            // Verify paragraph processing in table cells
+            XWPFParagraph cellPara = 
rows.get(0).getCell(0).getParagraphs().get(0);
+            assertNotNull(cellPara, "Cell paragraph should not be null");
+            String paraText = cellPara.getText();
+            assertTrue(paraText.contains("SDT Cell 1"), "Cell paragraph should 
contain SDT cell text");
+        }
+    }
+
+    @Test
+    void testAddRowToSdtTable() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            // Add an SDT-wrapped row
+            CTSdtRow sdtRow = table.addNewSdt();
+            CTSdtContentRow sdtContent = sdtRow.addNewSdtContent();
+            CTRow innerRow = sdtContent.addNewTr();
+            CTTc cell = innerRow.addNewTc();
+            CTP p = cell.addNewP();
+            CTR r = p.addNewR();
+            r.addNewT().setStringValue("SDT Row");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+            assertEquals(1, xtab.getNumberOfRows());
+
+            // Add a normal row
+            CTRow newRow = CTRow.Factory.newInstance();
+            CTTc newCell = newRow.addNewTc();
+            CTP newP = newCell.addNewP();
+            CTR newR = newP.addNewR();
+            newR.addNewT().setStringValue("New Row");
+            XWPFTableRow newTableRow = new XWPFTableRow(newRow, xtab);
+
+            xtab.addRow(newTableRow);
+            assertEquals(2, xtab.getNumberOfRows());
+
+            // Verify structural state
+            assertNotNull(xtab.getRow(0));
+            assertNotNull(xtab.getRow(1));
+            assertEquals(xtab.getRow(1), newTableRow);
+        }
+    }
+
+    @Test
+    void testRemoveSdtRow() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            // Add a normal row
+            CTRow normalRow = table.addNewTr();
+            CTTc cell1 = normalRow.addNewTc();
+            CTP p1 = cell1.addNewP();
+            CTR r1 = p1.addNewR();
+            r1.addNewT().setStringValue("Normal Row");
+
+            // Add an SDT-wrapped row
+            CTSdtRow sdtRow = table.addNewSdt();
+            CTSdtContentRow sdtContent = sdtRow.addNewSdtContent();
+            CTRow innerRow = sdtContent.addNewTr();
+            CTTc cell2 = innerRow.addNewTc();
+            CTP p2 = cell2.addNewP();
+            CTR r2 = p2.addNewR();
+            r2.addNewT().setStringValue("SDT Row");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+            assertEquals(2, xtab.getNumberOfRows());
+            assertNotNull(xtab.getRow(0));
+            assertNotNull(xtab.getRow(1));
+
+            // Remove the SDT row (index 1)
+            assertTrue(xtab.removeRow(1));
+            assertEquals(1, xtab.getNumberOfRows());
+            assertNotNull(xtab.getRow(0));
+            assertNull(xtab.getRow(1));
+        }
+    }
+
+    @Test
+    void testInsertNewTableRowInSdtTable() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            CTTbl table = CTTbl.Factory.newInstance();
+
+            // Add an SDT-wrapped row
+            CTSdtRow sdtRow = table.addNewSdt();
+            CTSdtContentRow sdtContent = sdtRow.addNewSdtContent();
+            CTRow innerRow = sdtContent.addNewTr();
+            CTTc cell = innerRow.addNewTc();
+            CTP p = cell.addNewP();
+            CTR r = p.addNewR();
+            r.addNewT().setStringValue("SDT Row");
+
+            XWPFTable xtab = new XWPFTable(table, doc);
+            assertEquals(1, xtab.getNumberOfRows());
+
+            // Insert a new row at position 0
+            XWPFTableRow inserted = xtab.insertNewTableRow(0);
+            assertNotNull(inserted);
+            assertEquals(2, xtab.getNumberOfRows());
+
+            // The inserted row should be at position 0
+            assertEquals(0, xtab.getRows().indexOf(inserted));
+        }
+    }
 }
diff --git a/test-data/document/Bug66263-paragraph.docx 
b/test-data/document/Bug66263-paragraph.docx
new file mode 100644
index 0000000000..e1b942b9ce
Binary files /dev/null and b/test-data/document/Bug66263-paragraph.docx differ
diff --git a/test-data/document/Bug66263-table.docx 
b/test-data/document/Bug66263-table.docx
new file mode 100644
index 0000000000..4964164911
Binary files /dev/null and b/test-data/document/Bug66263-table.docx differ


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to