【问题标题】:How to get FootnoteRefrence Id in OpenXML using C#如何使用 C# 在 OpenXML 中获取 FootnoteRefrence Id
【发布时间】:2013-05-08 10:14:40
【问题描述】:

我有一个像这样的 OOXML 文档的段落元素。 现在我想使用 c# 以编程方式从此文本中获取 FootNoteRefrence id。

来自 document.xml 的文本

<w:p>
  <w:r>
    <w:rPr>
      <w:rStyle w:val="FootnoteReference" />
    </w:rPr>
    <w:footnoteReference w:id="2" />
  </w:r>
</w:p>

C#代码

private BodyPara writePara(BodyPara bPara2, OpenXmlElement pTag)
    {
        Footnotes fn = null;
        foreach (var run in pTag.Descendants<Run>())
        {
            if (run.HasChildren)
            {

                foreach (var runProp in run.Descendants<RunProperties>())
                {
                    foreach (var runStyle in runProp.Descendants<RunStyle>())
                    {

                        if (runStyle.Val != null)
                        {
                            string runSty = runStyle.Val.Value;
                            if (runSty == "FootnoteReference")
                            {
                                if (fn != null)
                                {
                                    bPara2.FootNotes.Add(fn);

                                }
                                fn = new Footnotes();

                            }
                            else if (runSty == "CommentReference")
                            {


                            }
                            else
                            {
                                if (fn != null)
                                {
                                    fn.FootText = fn.FootText + run.InnerText;
                                }
                            }

                        }
                    }
                    //FootnotesPart footnotesPart = wordDoc.MainDocumentPart.FootnotesPart;
                    //if (footnotesPart != null)
                    //{
                    //  IEnumerable<Footnote> footnotes = footnotesPart.Footnotes.Elements<Footnote>();
                    // ...
                    //}
                    if (runProp.NextSibling() != null)
                    {
                        OpenXmlElement fr = runProp.NextSibling();
                        foreach (var fnref in fr)
                        {
                            if (fnref != null)
                            {
                                // fn.FootnoteID = fnref.Id.Value.ToString();
                            }
                        }
                    }
                    foreach (var shd in runProp.Descendants<Shading>())
                    {
                        if (shd.Fill != null)
                        {
                            string shdvalue = shd.Fill.Value;
                        }
                    }
                }
            }
        }

        return bPara2;
    }

我正在使用它来获取每个脚注的脚注参考 ID。 在这个循环中,我无法获得 FootNoteReference 类型的运行的后代及其值。 请帮我解决这个问题。 谢谢。

【问题讨论】:

    标签: openxml wordml footnotes


    【解决方案1】:

    对不起,我在参数中做错了,我没有在参数列表中使用Paragraph pTag,而是使用OpenXmlElement pTag。现在我将它从通用更改为特定。它现在有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多