首页 文章

如何将带有项目符号的OpenXml列表段添加到内容控件?

提问于
浏览
-2

我使用Word 2013 .docx作为在openxml代码中打开的模板,内容控件填写然后保存,下载到用户,该部分工作正常 .

在一个内容控件中,目标是添加项目符号列表 . 问题来自于尝试追加或替换从方法生成的新段落 . 这个方法工作正常,我发送变量,它返回一个带子弹的段落 . 它插入,但它破坏了doc这个词;

下面显示的是template.docx(上部)和得到的.docx(下部),只有下面的段落不同,在multiline = 1之后,空白区域是;其余的是相同的 . 如果我删除运行元素,保存,尝试,我可以打开单词doc打开并看到那里的格式化子弹列表,以便该部分“工作”,但是错误是什么?

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;
    }`

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>

和生成的文件:

<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>

运行的代码,单词doc有一个richtext内容控件*(它是用richtext cc设置的),名为custMilestones作为标记名,在contentcontrol中有一个空格,否则代码抛出一个对象引用而不是设置异常 .

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;
                   }

编辑7/30/15,结果xml制作奇怪的样式结果:

<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 回答

相关问题