如何将带有项目符号的OpenXml列表段落添加到内容控件中? [英] How to add OpenXml list paragraph with bullets to content control?

查看:129
本文介绍了如何将带有项目符号的OpenXml列表段落添加到内容控件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Word 2013 .docx作为模板,该模板在openxml代码中打开,然后填充内容控件,然后将其保存,下载给用户,并且该部分可以正常工作.

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.

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

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;

下面显示的是template.docx(上部)和result.docx(下部),只有下一行中的段落是不同的,在multiline = 1之后,空白区域是;其余的是相同的.如果我删除运行元素,保存,然后尝试,我可以打开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;
    }`

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一词有一个名为custMilestones的富文本内容控件*(它是通过富文本cc设置的)作为标记名,并且contentcontrol中有一个空格,否则该代码抛出未设置异常的对象引用

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

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

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>

推荐答案

似乎您将段落插入了<w:sdtPr>标签而不是<w:sdtContent>标签内.

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/zh-CN/library/documentformat.openxml.wordprocessing.sdtblock(v=office.14).aspx 指示将段落放在何处

还有,在段落中有一个段落,我不认为这样的话

And more, you have a Paragraph inside a Paragraph, I don't think Word like that

edit:标记sdtPr和sdtContent没有出现

edit : tags sdtPr and sdtContent didn't appears

这篇关于如何将带有项目符号的OpenXml列表段落添加到内容控件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆