【问题标题】:Finding a graph part within a rich text control with OpenXML SDK使用 OpenXML SDK 在富文本控件中查找图形部件
【发布时间】:2013-05-29 10:14:43
【问题描述】:

各位开发者您好,

作为 OpenXML SDK 的新手,我不知道如何检索已放入富文本控件(具有特定标签名称)的图形部分。

目前,我使用 mainDocumentPart.ChartParts 集合检索图形部分。但是 ChartPart 对象似乎不知道它在文档中的位置:chartPart.GetParentParts() 只包含 mainDocumentPart。

我的文档中有多个图表,如何区分它们? 我已经将我的图表放在富文本控件中,所以我认为我可以像那样访问它们,但我不知道如何做到这一点。检索富文本控件有效,但如何在其中找到图形?

        foreach (SdtProperties sdtProp in mainDocumentPart.Document.Body.Descendants<SdtProperties>())
        {
            Tag tag = sdtProp.GetFirstChild<Tag>();

            if (tag != null && tag.Val != null)
            {
                if (tag.Val == "containerX")
                {
                    SdtProperties sdtPropTestResults = sdtProp;

                    // How to retrieve the graph part??
                    // sdtPropTestResults.Descendants<ChartPart> does not seem to work
                }
            }
        }

非常感谢您的帮助。

【问题讨论】:

    标签: c# openxml-sdk


    【解决方案1】:

    自己找到了解决方案。我现在不使用父容器。相反,我给图表空间一个“Alt Title”。现在我的代码查找具有给定标题的 docProperties 的绘图。

    这里是:

    // Find our graphs by looping all drawings in the document and comparing their "alt title" property
    foreach (Drawing drawing in mainDocumentPart.Document.Body.Descendants<Drawing>())
    {
        DocProperties docProperties = drawing.Descendants<DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties>().FirstOrDefault();
    
        if (docProperties != null && docProperties.Title != null)
        {
            if (docProperties.Title.Value == AltTitleChartBlack || docProperties.Title.Value == AltTitleChartRed)
            {
                LineChartData lineChartData = null;
                switch (docProperties.Title.Value)
                {
                    case AltTitleChartBlack:
                        lineChartData = this.chartDataBlack;
                        break;
                    case AltTitleChartRed:
                        lineChartData = this.chartDataRed;
                        break;
                }
    
                ChartReference chartRef = drawing.Descendants<ChartReference>().FirstOrDefault();
                if (chartRef != null && chartRef.Id != null)
                {
                    ChartPart chartPart = (ChartPart)mainDocumentPart.GetPartById(chartRef.Id);
                    if (chartPart != null)
                    {
                        Chart chart = chartPart.ChartSpace.Elements<Chart>().FirstOrDefault();
                        if (chart != null)
                        {
    
                            LineChart lineChart = chart.Descendants<LineChart>().FirstOrDefault();
    
                            if (lineChart != null)
                            {
                                LineChartEx chartEx = new LineChartEx(chartPart, lineChartData);
                                chartEx.Refresh();
                                chartPart.ChartSpace.Save();
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多