element with a table in WordprocessingML I use the OpenXML SDK to programmatically replace some elements

OpenXML: replace the <sdt / "> element with a table in WordprocessingML

I use the OpenXML SDK to programmatically replace some elements <w:sdt/>with pieces of OpenXML markup (WordProcessingML).

For example, I have a paragraph with this content:

<w:p>
  <w:run><w:text> Text before </w:text></w:run>
  <w:sdt><w:sdtPr> ...</w:sdtPr><w:sdtContent>...</w:sdtContent></w:sdt>
  <w:run><w:text> Text after </w:text></w:run>
</w:p>

And a table with a structure like this:

<w:tbl>
  <w:tblPr>...</w:tblPr>
  <w:tblGrid> ... gridCol elements ...</w:tblGrid>
  <w:tr>
    <w:trPr>...</w:trPr>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
  </w:tr>
 </w:tbl>

Basically, I want to replace the item <w:sdt/>in the paragraph with table markup. The problem is that I cannot just replace it, because it will create an invalid document (a table with paragraphs inside another paragraph element is not allowed).

As a result, I want to get the following:

<w:p>
  <w:run><w:text> Text before </w:text></w:run>
</w:p>
<w:tbl>
  <w:tblPr>...</w:tblPr>
  <w:tblGrid> ... gridCol elements ...</w:tblGrid>
  <w:tr>
    <w:trPr>...</w:trPr>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
    <w:tc>
   <w:tcPr>...</w:tcPr>
   <w:p> Cell contents </w:p>
    </w:tc>
  </w:tr>
</w:tbl>
<w:p>
  <w:run><w:text> Text after </w:text></w:run>
</w:p>

I was thinking of creating a method that created a new paragraph with elements before the element <w:sdt/>, and another with elements after the sdt element, but it seems that such a method would be error prone.

, , , (, , Word, )?

.

+3
1

, Content Control, . , , Word , . , , , Word , / , Word, , . , .

, (, ), , , . Eric White, XML SDK Word. , / , , .

+3

Source: https://habr.com/ru/post/1754720/


All Articles