【问题标题】:How to read header and footer child elements in sequence order using word interop C#如何使用word interop C#按顺序读取页眉和页脚子元素
【发布时间】:2019-12-19 19:24:36
【问题描述】:

我们正在使用表单,在当前项目中,我们正在使用 NPOI 对象,但由于单词元数据,npoi 没有返回第一页、偶数页和奇数页页眉/页脚。因此,我们正在尝试使用互操作来获得正确的结果,并且我们能够按照以下代码获得结果。但是我们再次面临按顺序读取页眉/页脚下的正文元素的挑战。

当前结果:
我能够阅读页眉/页脚的正文元素(表格和段落)。但不是按顺序排列的。我可以单独读取元素,例如“firstPageFooter .Paragraphs”和“firstPageFooter .Tables”

foreach (Section data in sections)
{
    var tempVal = string.Empty;
    HeadersFooters headersFooters = data.Footers;
    bool isDifferentFirstPageHeaderFooter = Convert.ToBoolean(data.PageSetup.DifferentFirstPageHeaderFooter);

    bool isOddAndEvenPagesHeaderFooter = Convert.ToBoolean(data.PageSetup.OddAndEvenPagesHeaderFooter);

    if (isDifferentFirstPageHeaderFooter)
        firstPageFooter = headersFooters[WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;

    Tables headerTable = firstPageFooter .Tables;

    StringBuilder tableBuilder = new StringBuilder();                    

    foreach (Table table in headerTable)
    {
        foreach (Row row in table.Rows)
        {
            foreach (Cell cell in row.Cells)
            {
                tableBuilder.Append(cell.Range.Text);
            }
        }
    }

    StringBuilder paraBuilder = new StringBuilder();

    var headerPara= firstPageFooter.Paragraphs;

    foreach (Paragraph paragraph in headerPara)
    {
        paraBuilder.Append(paragraph.Range.Text);
    }
}   // Added by edit!!!

预期结果:
如果页眉/页脚正文元素包含表格和段落。第一个元素是表格,第二个是段落。所以单词应该以相同的顺序返回正文元素。

有人可以帮帮我吗?

【问题讨论】:

  • 这不是很清楚。我认为我理解,但如果您可以使用edit 链接包含一个小例子来说明您的意思(来源和预期结果),这将有所帮助。根据我对问题的理解,如果您循环 Paragraphs 集合并 test 每个段落(例如,它是否在表格中),您应该可以从那里开始工作。跨度>
  • 感谢辛迪的更新。对不起,不完整的细节。我已经更新了我的问题,希望它会很清楚。

标签: c# ms-word office-interop


【解决方案1】:

可以循环文档Story(例如Header)的Paragraphs集合并测试它是否在表格中。如果是,则拿起桌子并处理它。然后继续执行表格下方的段落,直到完成Story

这是一个演示此方法的代码 sn-p,基于单个 Header。

Word.HeaderFooter hdr = doc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
Word.Range hdrRange = hdr.Range;
Word.Paragraphs paras = hdrRange.Paragraphs;
for (int counter = 1; counter <= paras.Count; counter++)
{
    Word.Paragraph para = paras[counter];
    if ((bool)para.Range.get_Information(Word.WdInformation.wdWithInTable))
    {
        //Get the table that belongs to the first paragraph in the table
        Word.Table tbl = para.Range.Tables[1];
        //Reset the counter so that it cycles to the paragraph after the table
        counter += tbl.Range.Paragraphs.Count - 1;
        Debug.Print("In table with " + (counter - 1).ToString() + " paragraphs");
        //Process the table
    }
    else
    {
        //Process the paragraph
        Debug.Print("Paragraph " + counter.ToString() + ": " + para.Range.Text);
    }
}

【讨论】:

  • 如果我没记错的话,如果表格在段落中,这种方法有效,但是如果两个元素一个接一个地放置怎么办?我们如何按顺序循环遍历 body 元素?我们有一个表格,在标题中一个接一个地包含表格和段落。例如:表格有两行两列,段落放在表格之后。
  • @LalithaRani:你在评论中问什么不清楚。我的回答完全基于内容中提供的信息。表格不能在段落中。什么可能是“一个接一个”横向?问题中的代码将表格和段落作为单独的实体处理。答案中的代码将垂直处理表格和段落。你真的尝试了吗?
  • 是的,这两个元素是水平放置的。我尝试了上面的代码,它返回了所有内容。但是我们正在寻找解决方案,比如对象应该像 npoi 对象一样返回子元素列表。例如:Npoi 对象返回标题的子元素列表“var firstPageHeader = policy.GetFirstPageHeader().BodyElements”它返回标题的正文元素列表
  • @LalithaRani 表格和段落不能水平相邻放置 - 除非它们位于 Shape 对象中(应用了文本换行格式)。由于互操作是基于布局的:使用互操作来获得事物在底层 Word Open XML 中出现的顺序几乎是不可能的:XML 的 UI 解释。你试过firstPageFooter.WordOpenXML然后解析了吗?听起来这可能是你需要的......
  • 我还没有尝试过这个 firstPageFooter.WordOpenXML。我对此没有任何想法。你能帮我解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多