在C#中访问Power Point文件的文档属性

问题描述 投票:1回答:2

我需要访问Power Point属性,例如Author,Organization ..

我该怎么做?

编辑:

这是我正在尝试的:

static void TestProperties(Presentation presentation) // Microsoft.Office.Interop.PowerPoint.Presentation;
            {
                Microsoft.Office.Core.DocumentProperties properties;
                properties = (Microsoft.Office.Core.DocumentProperties)presentation.BuiltInDocumentProperties;

                Microsoft.Office.Core.DocumentProperty prop;

            }

这给了我ClassCastException:

无法将类型'System .__ ComObject'的COM对象转换为接口类型'Microsoft.Office.Core.DocumentProperties'

我有一个文件选择器对话框,在其中选择演示文稿,然后将其传递给TestProperties方法。

c# properties document powerpoint
2个回答
0
投票

这样的东西对您有用吗?更改属性[“”]以适合您的情况

Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
    Globals.ThisWorkbook.BuiltinDocumentProperties; 
Microsoft.Office.Core.DocumentProperty prop;
prop = properties["Revision Number"]; 

0
投票

我使用OLE自动化(Microsoft.Office.Interop.PowerPoint)对PowerPoint文档具有有效的C#代码,用于读/写文档属性>]

using System;
using Microsoft.Office.Interop.PowerPoint;
using System.Reflection;

class PowerPointConverterDocumentPropertiesReaderWriter
{
    static void Main()
    {
        Application pptApplication = new Application();
        Presentation presentation = pptApplication.Presentations.Open("presentation.pptx");

        object docProperties = presentation.BuiltInDocumentProperties;
        string title = GetDocumentProperty(docProperties, "Title").ToString();
        string subject = GetDocumentProperty(docProperties, "Subject").ToString();
        string author = GetDocumentProperty(docProperties, "Author").ToString();
        string category = GetDocumentProperty(docProperties, "Category").ToString();
        string keywords = GetDocumentProperty(docProperties, "Keywords").ToString();

        SetDocumentProperty(docProperties, "Title", "new title");
        SetDocumentProperty(docProperties, "Subject", "new subject");
        SetDocumentProperty(docProperties, "Author", "new author");
        SetDocumentProperty(docProperties, "Category", "new category");
        SetDocumentProperty(docProperties, "Keywords", "new keywords");
    }

    static object GetDocumentProperty(object docProperties, string propName)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        object propValue = prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.GetProperty,
            null, prop, new object[] { });
        return propValue;
    }

    static void SetDocumentProperty(object docProperties, string propName, object propValue)
    {
        object prop = docProperties.GetType().InvokeMember(
            "Item", BindingFlags.Default | BindingFlags.GetProperty,
            null, docProperties, new object[] { propName });
        prop.GetType().InvokeMember(
            "Value", BindingFlags.Default | BindingFlags.SetProperty,
            null, prop, new object[] { propValue });
    }
}

经过测试,可以与Office 365的Microsoft PowerPoint(版本16.0)一起正常使用。

感谢https://wditot.wordpress.com/2012/05/10/extracting-metadata-from-ms-office-docs-programmatically

© www.soinside.com 2019 - 2024. All rights reserved.