【问题标题】:Can I add a CustomXmlPart to my presentation without storing the XML for it in a file?我可以将 CustomXmlPart 添加到我的演示文稿而不将其 XML 存储在文件中吗?
【发布时间】:2018-08-09 03:27:55
【问题描述】:

TLDR; 请确认第二个代码片段是创建 CustomXmlPart 的公认方法,或者告诉我另一种不那么繁琐的方法。


所以我尝试将一些应用程序数据嵌入到我正在使用 OpenXmlSDK 修改的 .pptx 中的某些元素中。

为了简要说明,我需要在加载到演示文稿中的每个图像中嵌入一个图表代码。这样可以重新上传演示文稿并再次生成图表,然后使用最新数据进行替换。

最初我在 OpenXmlElement 本身上使用 Extended Attributes:

//OpenXmlDrawing = DocumentFormat.OpenXml.Drawing

// there's only one image per slide for now, so I just grab the blip which contains the image
OpenXmlDrawing.Blip blip = slidePart.Slide.Descendants<OpenXmlDrawing.Blip>().FirstOrDefault(); 

//then apply the attribute
blip.SetAttribute(new OpenXmlAttribute("customAttribute", null, customAttributeValue));

问题在于,在 PowerPoint 2013 中编辑 .pptx 时,它会去除所有扩展属性。

所以。 现在我已经在多个地方阅读到解决方案是使用 CustomXmlPart。 所以我试图找到如何做到这一点.. 看起来它需要我为每个 CustomXmlPart 提供一个单独的文件才能输入到部件中。例如/

var customXmlPart = slidePart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
    customXmlPart.FeedData(stream);
}

^ 并且需要为每个 CustomXmlPart 使用不同的文件重复此操作。这意味着我可能只需要一个包含骨架自定义 XML 部件的模板文件,然后在将其输入到自定义 xml 部件之前为每张幻灯片动态填充其内容。

仅仅添加一个小自定义属性似乎需要做很多工作。但是我还没有找到任何替代方法。

任何人都可以确认这确实是我应该这样做的方式,或者指出我另一个方向吗?非常感谢。

【问题讨论】:

    标签: c# .net powerpoint openxml openxml-sdk


    【解决方案1】:

    答案是肯定的! :)

    public class CustomXMLPropertyClass
    {
        public string PropertyName { get; set; }
        public string PropertyValue { get; set; }
    }
    
    private static void AddCustomXmlPartCustomPropertyToSlidePart(string propertyName, string propertyValue, SlidePart part)
    {
        var customXmlPart = part.AddCustomXmlPart(CustomXmlPartType.CustomXml);
    
        var customProperty = new CustomXMLPropertyClass{ PropertyName = propertyName, PropertyValue = propertyValue };
    
    
        var serializer = new System.Xml.Serialization.XmlSerializer(customProperty.GetType());
        var stream = new MemoryStream();
    
        serializer.Serialize(stream, customProperty);
    
        var customXml = System.Text.Encoding.UTF8.GetString(stream.ToArray());
    
        using ( var streamWriter = new StreamWriter(customXmlPart.GetStream()))
        {
            streamWriter.Write(customXml);
            streamWriter.Flush();
        }
    }
    

    然后将其取出:

    private static string GetCustomXmlPropertyFromCustomXmlPart(CustomXmlPart customXmlPart)
    {
        var customXmlProperty = new CustomXMLPropertyClass();
        string xml = "";
    
        using (var stream = customXmlPart.GetStream())
        {
            var streamReader = new StreamReader(stream);
            xml = streamReader.ReadToEnd();
        }
    
        using (TextReader reader = new StringReader(xml))
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(customXmlProperty));
            customXmlProperty = (CustomXMLPropertyClass)serializer.Deserialize(reader);
        }
    
        var customPropertyValue = customXmlProperty.PropertyValue;
    
        return customPropertyValue;
    }
    

    【讨论】:

    • 注意: 使用此方法,PowerPoint 2013 仍会删除更改,因此我正在努力让它们在编辑演示文稿时持续存在。
    • 这是我打开的另一个问题,试图解决自定义数据被删除的问题。 stackoverflow.com/questions/50879524/…
    【解决方案2】:

    你也可以试试custom properties。自定义 XML 文件适用于复杂的对象,听起来您只需要存储简单的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多