How to add OpenXml list paragraph with bullets to content control?

只谈情不闲聊 提交于 2019-12-13 09:31:18

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/31703771/how-to-add-openxml-list-paragraph-with-bullets-to-content-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!