Using OpenXML what I'm trying to do is insert a table into a Word document. This works fine, but now I need the table to break across two Word columns (as in the newspaper type columns in Word, not as in the column of the table itself). So in Word itself I took my table, added a section break before and a section break after and then set it to two columns and it breaks my table into two and distributes it across the columns. Looking at the layout in the Open XML productivity tool I see it added this before the table:
<w:p w:rsidR="00E634EE" w:rsidRDefault="00E634EE" w14:paraId="0D41C6E2" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:sectPr w:rsidR="00E634EE" w:rsidSect="00C511F2">
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="864" w:right="864" w:bottom="1440" w:left="864" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:pPr>
</w:p>
And after my table it added this:
<w:p w:rsidR="00E634EE" w:rsidP="00E634EE" w:rsidRDefault="00E634EE" w14:paraId="15329047" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:rPr>
<w:noProof />
</w:rPr>
<w:sectPr w:rsidR="00E634EE" w:rsidSect="00E634EE">
<w:type w:val="continuous" />
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="864" w:right="864" w:bottom="1440" w:left="864" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" w:num="2" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:pPr>
</w:p>
(interestingly the 2 columns are defined in the section break after the table and not before it).
So I edited my code to add section breaks before and after my table and it does produce columns...but my table doesn't break across the columns. Instead it's all stuffed in the left-most column.
So how, and where (if possible) can I indicate how and where to start putting table rows in the second column rather than the first?
These are the breaks that end up in my file:
<w:p w:rsidR="00EA3E62" w:rsidRDefault="003B0473" w14:paraId="6A8C371A" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:sectPr w:rsidR="00EA3E62">
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" />
<w:docGrid w:linePitch="360" />
</w:sectPr>
</w:pPr>
</w:p>
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
...
</w:tbl>
<w:p w:rsidR="00EA3E62" w:rsidRDefault="003B0473" w14:paraId="784DE666" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:sectPr w:rsidR="00EA3E62">
<w:type w:val="continuous" />
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" />
<w:cols w:space="720" w:num="2" />
</w:sectPr>
</w:pPr>
</w:p>
Added with (and running through Word Automation Services):
// Insert section breaks
var end = new Paragraph(
new ParagraphProperties(
new SectionProperties(
new SectionType() { Val = SectionMarkValues.Continuous },
new Columns() { Space = "720", ColumnCount = 2, Separator = true })
)
);
parent.InsertAfterSelf(end);
parent.InsertAfterSelf(t);
parent.InsertAfterSelf(new Paragraph(
new ParagraphProperties(
new SectionProperties(
new SectionType() { Val = SectionMarkValues.Continuous })
)
)
);
Where parent is the element I'm adding my table after and t is my Table element.
After some experimentation, I believe I have now figured this out. The trick is, to add another section break.
The issue, I believe, is that without another section break following the table, the next break is at the end of the page, so Word will lay everything out in the first column until it reaches the end of the page. If there is enough space on the page, then the entire table will end up in one column (and look really lopsided). So the trick is to add another (empty) paragraph right after to force Word to compress the table by breaking it across columns.
Related
I'm trying to build a docx document with HTML content in the page headers. This is what my document.xml body looks like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document ...xmlns declarations.. mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="43132D41" w14:textId="3A8DC205" w:rsidR="00940BB3" w:rsidRDefault="00772215">
<w:r>
<w:t>Hello</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00940BB3">
<w:headerReference w:type="even" r:id="rId7"/>
<w:headerReference w:type="default" r:id="rId8"/>
<w:footerReference w:type="even" r:id="rId9"/>
<w:footerReference w:type="default" r:id="rId10"/>
<w:headerReference w:type="first" r:id="rId11"/>
<w:footerReference w:type="first" r:id="rId12"/>
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
rId8 header refers to header2.xml which looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:hdr ...lots of xmlns... mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:p w14:paraId="775C4615" w14:textId="387F538F" w:rsidR="00772215" w:rsidRDefault="00772215">
<w:pPr>
<w:pStyle w:val="Header"/>
</w:pPr>
<w:r>
<w:t>The Header</w:t>
</w:r>
</w:p>
<w:p w14:paraId="746D41E6" w14:textId="77777777" w:rsidR="00772215" w:rsidRDefault="00772215">
<w:pPr>
<w:pStyle w:val="Header"/>
</w:pPr>
</w:p>
<w:altChunk r:id="htmlchunk"/>
</w:hdr>
And htmlchunk refers to a simple html file:
<!DOCTYPE html>
<html>
<body><b>Real HTML</b></body>
</html>
This is how these references look in the document.rels.xml:
<Relationship Id="htmlchunk" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk" Target="htmlchunk.html"/>
<Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header2.xml"/>
When trying to open the resulting document in MS Word, it tries to repair the document and then shows the header "The Header" portion without the html contents.
When trying to validate the document with OOXMLValidator, it shows this error:
[{"Description":"The relationship 'htmlchunk' referenced by attribute 'http://schemas.openxmlformats.org/officeDocument/2006/relationships:id' does not exist.","Path":{"NamespacesDefinitions":["xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\""],"Namespaces":{},"XPath":"/w:hdr[1]/w:altChunk[1]","PartUri":"/word/header2.xml"},"Id":"Sem_InvalidRelationshipId","ErrorType":"Semantic"}]
How can I solve this problem? How can the header xml file refer to other chunk?
To reference the part identified by"htmlchunk", that part must be added to the header part (header2.xml in your example) rather than the main document part (document.xml). This means that the Relationship element would not be found in document.xml.rels but in header2.xml.rels.
The docs recommend the following way to add a content control:
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w:body>
<w:p/>
<w:sdt>
<w:sdtPr>
<w:alias w:val="MyContentControlTitle"/>
<w:id w:val="1382295294"/>
<w15:appearance w15:val="hidden"/>
<w:showingPlcHdr/>
</w:sdtPr>
<w:sdtContent>
<w:p>
<w:r>
<w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>
But if one looks closely, the content control is added inside a paragraph (<w:p>):
<w:sdtContent>
<w:p>
<w:r>
<w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
</w:r>
</w:p>
</w:sdtContent>
Is there a way I can prevent adding it inside the paragraph and make it inline? Just removing the <w:p> tag doesn't work somehow. Also, I am familiar with the Word API for adding content controls. My scenario is such that I have to produce and OOXML and then insert in into the document.
If you remove the <w:p> and </w:p> from inside the <w:sdtContent> element, then you have to wrap the <w:sdt> element in a <w:p> element, like this, otherwise Word will not recognize the content:
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w:body>
<w:p/>
<w:p>
<w:sdt>
<w:sdtPr>
<w:alias w:val="MyContentControlTitle"/>
<w:id w:val="1382295294"/>
<w15:appearance w15:val="hidden"/>
<w:showingPlcHdr/>
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:t>[This text is inside a content control that has its container hidden. You can bind to a content control to add or interact with content at a specified location in the document.]</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>
Viewed in Word, in "your" encoding style, the paragraph mark appears inside the content control. With the above encoding, it appears outside the content control, and that means that it is possible to insert content between the end of the control and the end of paragraph.
I have a generated word document (docx) and want to change the page number afterwards, in a way that it starts at number X. The page number is included in the footer of the word document. Where in the XML files do I need to apply this change?
I have changed a document to start at number 4 and looked at the XML code. The element <w:pgNumType w:start="4"/> was added. But if I only add this part manually to the XML, the document is broken.
document.xml content before adding "start with number 4":
<w:sectPr w:rsidR="00C17A11" w:rsidRPr="00CD0C83" w:rsidSect="00A86D42">
<w:headerReference r:id="rId8" w:type="even"/>
<w:headerReference r:id="rId9" w:type="default"/>
<w:footerReference r:id="rId10" w:type="even"/>
<w:footerReference r:id="rId11" w:type="default"/>
<w:headerReference r:id="rId12" w:type="first"/>
<w:footerReference r:id="rId13" w:type="first"/>
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar w:bottom="851" w:footer="567" w:gutter="0" w:header="851" w:left="1304" w:right="567" w:top="828"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
And after adding "start with number 4":
<w:sectPr w:rsidR="00C17A11" w:rsidRPr="00CD0C83" w:rsidSect="00CA253E">
<w:headerReference w:type="default" r:id="rId8"/>
<w:footerReference w:type="default" r:id="rId9"/>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="828" w:right="567" w:bottom="851" w:left="1304" w:header="851" w:footer="567" w:gutter="0"/>
<w:pgNumType w:start="4"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
As Josch mentioned you are doing everything right. There is nothing more to be added to the document.
I have created an empty document with 4 pages in Word and then unpacked the contents of my .wordx file.
Here is my document.xml file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se wp14">
<w:body>
<w:p w:rsidR="00ED7F18" w:rsidRDefault="00ED7F18">
<w:r>
<w:br w:type="page"/>
</w:r>
</w:p>
<w:p w:rsidR="00ED7F18" w:rsidRDefault="00ED7F18">
<w:r>
<w:lastRenderedPageBreak/>
<w:br w:type="page"/>
</w:r>
</w:p>
<w:p w:rsidR="00ED7F18" w:rsidRDefault="00ED7F18">
<w:r>
<w:lastRenderedPageBreak/>
<w:br w:type="page"/>
</w:r>
</w:p>
<w:p w:rsidR="0070592A" w:rsidRDefault="00ED7F18"/>
<w:sectPr w:rsidR="0070592A" w:rsidSect="00ED7F18">
<w:headerReference w:type="even" r:id="rId6"/>
<w:headerReference w:type="default" r:id="rId7"/>
<w:footerReference w:type="even" r:id="rId8"/>
<w:footerReference w:type="default" r:id="rId9"/>
<w:headerReference w:type="first" r:id="rId10"/>
<w:footerReference w:type="first" r:id="rId11"/>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0"/>
<w:pgNumType w:start="4"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
Changing the following line
<w:pgNumType w:start="4"/>
to
<w:pgNumType w:start="5"/>
and then zipping back all the contents does the thing.
You are probably zipping back the contents together with the folder they are in.
Please make sure that the zip file has exactly the following folders/files.
_rels
docProps
word
[Content_Types].xml
How can I create a Toc(Table of Content) for a document in Word using office.js? I have look through the help doc on github, but find nothing about this. Please help me. Thank you very much.
Table of Contents is currently not a supported object of the Word JS API.
Make sure to add this idea to our user voice channel
https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback/category/163566-add-in-word
I am experiment TOC in Word JS API[preview]
till now i had created some sort of TOC look in Word JS API with help of XML[insertOoxml]
Word.run(async function (context) {
const currentDoc = context.document;
const body = currentDoc.body;
const paragraphs = currentDoc.body.paragraphs.load('items');
const headers = [];
const uniqueStr = new Date().getTime();
let insertion = '';
await context.sync();
for (let i = 0; i < paragraphs.items.length; i++) {
// we can use outlineLevel 1,2,3
if (
paragraphs.items[i].styleBuiltIn === 'Heading1' ||
paragraphs.items[i].styleBuiltIn === 'Heading2' ||
paragraphs.items[i].styleBuiltIn === 'Heading3'
) {
headers.push(paragraphs.items[i]);
}
}
const xValues = headers.map(function (o) {
return o.outlineLevel;
});
const xMax = Math.max.apply(null, xValues);
const xMin = Math.min.apply(null, xValues);
console.log(headers);
headers.map(function (item, i) {
console.log(item.outlineLevel);
const bookmarkName = `_Toc${item.outlineLevel + item._Id + uniqueStr}`;
insertion += `<w:p w:rsidR='00000000' w:rsidRDefault='0043114D'>
<w:pPr>
<w:pStyle w:val='TOC${item.outlineLevel}'/>
<w:tabs>
<w:tab w:val='right' w:leader='dot' w:pos='9016'/>
<w:tab w:val="left" w:pos="9016" w:leader='dot'/>
</w:tabs>
<w:rPr>
<w:rFonts w:eastAsiaTheme='minorEastAsia' w:cs='Latha'/>
<w:noProof/>
</w:rPr>
</w:pPr>
${
i === 0
? `<w:r>
<w:fldChar w:fldCharType='begin'/>
</w:r>
<w:r>
<w:rPr>
<w:noProof />
<w:webHidden />
</w:rPr>
<w:instrText xml:space='preserve'> TOC \\o "${xMin}-${xMax}" \\h \\z \\u </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType='separate'/>
</w:r>`
: `<w:r><w:rPr>
<w:noProof />
<w:webHidden />
</w:rPr></w:r>`
}
<w:p w:rsidR='00000000' w:rsidRDefault='0043114D'>
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText xml:space="preserve"> PAGEREF ${bookmarkName} \\h </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:noProof/>
</w:rPr>
<w:t>${item.text.toString()}</w:t>
</w:r>
<w:r>
<w:tab />
</w:r>
<w:r>
<w:rPr>
<w:noProof />
<w:webHidden />
<w:spacing w:line='50' w:lineRule='auto'/>
</w:rPr>
<w:fldChar w:fldCharType='separate' />
</w:r>
<w:r>
<w:rPr>
<w:noProof />
<w:webHidden />
</w:rPr>
<w:t xml:space="preserve"> 1 </w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
</w:p>`;
const insertRange: Word.Range = item.getRange();
insertRange.insertBookmark(bookmarkName);
});
body.insertOoxml(
`<pkg:package xmlns:pkg='http://schemas.microsoft.com/office/2006/xmlPackage'>
<pkg:part pkg:name='/_rels/.rels' pkg:contentType='application/vnd.openxmlformats-package.relationships+xml' pkg:padding='512'>
<pkg:xmlData>
<Relationships xmlns='http://schemas.openxmlformats.org/package/2006/relationships'>
<Relationship Id='rId1' Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' Target='word/document.xml'/></Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name='/word/document.xml' pkg:contentType='application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'>
<pkg:xmlData>
<w:document xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' >
<w:body>
<w:sdt>
<w:sdtPr>
<w:id w:val='${uniqueStr}' />
<w:docPartObj>
<w:docPartGallery w:val='Table of Contents' />
<w:docPartUnique />
</w:docPartObj>
</w:sdtPr>
<w:sdtEndPr>
<w:rPr>
<w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi" />
<w:b />
<w:bCs />
<w:noProof />
<w:color w:val="auto" />
<w:sz w:val="22" />
<w:szCs w:val="22" />
</w:rPr>
</w:sdtEndPr>
<w:sdtContent>
<w:p w:rsidR="00095C65" w:rsidRDefault="00095C65">
<w:pPr>
<w:pStyle w:val='TOCHeading'/>
<w:b />
<w:color w:val="2E74B5" w:themeColor="accent1" w:themeShade="BF" />
<w:sz w:val="30" />
<w:szCs w:val="30" />
</w:pPr>
<w:r>
<w:t>Contents</w:t>
</w:r>
</w:p>
${insertion}
</w:sdtContent>
</w:sdt>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>`,
Word.InsertLocation.start
);
await context.sync();
console.log('xml added at the start of the document body.');
}).catch(OfficeAddinFacade.errorHandler);
now i want page number in dynamic way where Header[1,2,3] is present
someone can help me this
Im using a Word 2013 .docx as a template thats opened in the openxml code, the content controls are filled in then its saved, downloaded to the user and that part works fine.
In one content control the goal is to add a bulleted list. The problem comes from trying to append or replace the new paragraph, which is generated from a method. This method works fine, i send it the variable and it returns a paragraph with a bullet. It inserts, but it corrupts the word doc;
Shown below are the template.docx (upper) and resulting.docx (lower) only the paragraph in the lower one is different, after multiline=1 where the blank area is; the rest is identical. if I remove run elements, save, try, I could get the word doc to open and see the formatted bullet list there so that part "works", but what is the error?
public static Paragraph GenerateParagraph(string bulletText)
{
var element =
new Paragraph(
new ParagraphProperties(
new ParagraphStyleId() { Val = "ListParagraph" },
new NumberingProperties(
new NumberingLevelReference() { Val = 1 },
new NumberingId() { Val = 2 })),
new Run(
new Text(bulletText)) //Text you want to insert with bullet
) { RsidParagraphAddition = "00031711", RsidParagraphProperties = "00031711", RsidRunAdditionDefault = "00031711" };
return element;
}`
the word doc:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custMilestones"/>
<w:id w:val="-1610118874"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
<w:multiLine w:val="1"/>
* in the file compare this original file was different, in this area with missing lines
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidRPr="002C1BAC">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
<w:r w:rsidR="00DD453F">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
</w:sdtContent>
and the generated file:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custMilestones"/>
<w:id w:val="-1610118874"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
<w:multiLine w:val="1"/>
<w:p>
<w:p w:rsidR="00031711" w:rsidP="00031711" w:rsidRDefault="00031711">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="1"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>
Summary of outlines including the details of customer products and part numbers.
</w:t>
</w:r>
</w:p>
</w:p>
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidRPr="002C1BAC">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
<w:r w:rsidR="00DD453F">
<w:rPr>
<w:b w:val="0"/>
<w:color w:val="000000" w:themeColor="text1"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve">
</w:t>
</w:r>
</w:sdtContent>
And the code that runs, the word doc has a richtext content control* (it was setup with the richtext cc) called custMilestones as the tagname and there is one space in the contentcontrol otherwise the code throws an object reference not set exception.
byte[] byteArray = System.IO.File.ReadAllBytes(fileName);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(mem, true))
{
MainDocumentPart mainDoc = wordDoc.MainDocumentPart;
Body bodyDoc = mainDoc.Document.Body;
foreach (var cc in wordDoc.ContentControls())
{
SdtProperties props = cc.Elements<SdtProperties>().FirstOrDefault();
Tag tag = props.Elements<Tag>().FirstOrDefault();
dat1 = tag.Val.ToString();
switch (dat1)
{
case "custMilestones":
props.RemoveAllChildren<Paragraph>();
props.AppendChild<Paragraph>(new Paragraph(GenerateParagraph(custmilestone1)));
break;
default:
break;
}
edit 7/30/15 with resulting xml making oddly styled results:
<w:sdt>
<w:sdtPr>
<w:rPr>
<w:rStyle w:val="Style1"/>
<w:b w:val="0"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tag w:val="custmiles"/>
<w:id w:val="568603642"/>
<w:placeholder>
<w:docPart w:val="D9E50C82EA8C4C45A237822288EB36B5"/>
</w:placeholder>
<w:showingPlcHdr/>
<w15:appearance w15:val="hidden"/>
</w:sdtPr>
<w:sdtEndPr>
<w:rPr>
<w:rStyle w:val="DefaultParagraphFont"/>
<w:sz w:val="36"/>
</w:rPr>
</w:sdtEndPr>
<w:sdtContent>
<w:r w:rsidRPr="00B87AB0" w:rsidR="00E73294">
<w:rPr>
<w:b w:val="0"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t xml:space="preserve"/>
</w:r>
</w:sdtContent>
</w:sdt>
<w:r w:rsidRPr="00B87AB0" w:rsidR="00C06C61">
<w:rPr>
<w:b w:val="0"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:tab/>
<w:sdt>
<w:sdtPr>
<w:id w:val="-206335861"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1081868574"/>
</w:placeholder>
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00816E2A" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence1 for the software to make lists with numbers
</w:t>
</w:r>
</w:p>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00816E2A" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence2 for the software to make lists with numbers
</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:p w:rsidRDefault="00816E2A" w:rsidR="00546790" w:rsidP="00816E2A">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
<w:ind w:leftChars="0"/>
</w:pPr>
<w:r>
<w:t>
this is test sentence3 for the software to make lists with numbers </w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>
</w:r>
It looks like you insert your Paragraph inside the <w:sdtPr> Tag instead of the <w:sdtContent> tag.
<w:sdtPr>
...
<w:p>
<w:p w:rsidR="00031711" w:rsidP="00031711" w:rsidRDefault="00031711">
...
</w:p>
</w:p>
</w:sdtPr>
https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sdtblock(v=office.14).aspx indicate where to put the Paragraph
And more, you have a Paragraph inside a Paragraph, I don't think Word like that
edit : tags sdtPr and sdtContent didn't appears