前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dotnet OpenXML SDK 添加 CoreFilePropertiesPart 的方法

dotnet OpenXML SDK 添加 CoreFilePropertiesPart 的方法

作者头像
林德熙
发布2024-01-25 08:22:53
1030
发布2024-01-25 08:22:53
举报
文章被收录于专栏:林德熙的博客林德熙的博客

本文记录在 OpenXML SDK 2.15 版本下,为 PPTX 文件添加 CoreFilePropertiesPart 的方法,通过本文的方法可以正确且简单的添加 core.xml 文件到 PPTX 文件里

当前的 OpenXML SDK 存在已知问题,那就是默认情况下生成不包含 core.xml 文件,请参阅 https://github.com/dotnet/Open-XML-SDK/issues/1093

在 OpenXML SDK 里面提供了 AddCoreFilePropertiesPart 方法可以用来添加 CoreFilePropertiesPart 的内容。但是对其调用有要求,比如说在 PresentationDocument 的 PackageProperties 属性赋值之前进行调用,如以下代码,则会出现 System.Xml.XmlException:“Root element is missing.” 异常

代码语言:javascript
复制
        public void CreatePackage(string filePath)
        {
            using PresentationDocument package = PresentationDocument.Create(filePath, PresentationDocumentType.Presentation);

            var document = package;

            if (document.CoreFilePropertiesPart is null)
            {
                document.AddCoreFilePropertiesPart();
            }

            SetPackageProperties(package);
        }

        private void SetPackageProperties(OpenXmlPackage document)
        {
            document.PackageProperties.Creator = "dexi lin";
            document.PackageProperties.Title = "PowerPoint 演示文稿";
            document.PackageProperties.Revision = "1";
            document.PackageProperties.Created = System.Xml.XmlConvert.ToDateTime("2024-01-24T09:19:23Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind);
            document.PackageProperties.Modified = System.Xml.XmlConvert.ToDateTime("2024-01-24T09:19:34Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind);
            document.PackageProperties.LastModifiedBy = "dexi lin";
        }

以上代码进入到设置 PackageProperties.Creator 属性时,将会收到 System.Xml.XmlException:“Root element is missing.” 异常。这是因为预期的 CoreFilePropertiesPart 已经存在,但是里面没有任何内容

这时候网上的许多方法都是推荐采用如下或类似的代码写入 CoreFilePropertiesPart 的内容

代码语言:javascript
复制
            if (document.CoreFilePropertiesPart is null)
            {
                var coreFilePropertiesPart = document.AddCoreFilePropertiesPart();

                using (XmlTextWriter writer = new XmlTextWriter(coreFilePropertiesPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8))
                {
                    writer.WriteRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<coreProperties xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\"></coreProperties>");
                    writer.Flush();
                }
            }

            SetPackageProperties(package);

尽管以上代码能够正常工作,但代码不好看

如果你不小心抄错了 EXCEL 的代码,那你将会遇到 System.Xml.XmlException:“Unrecognized root element in Core Properties part. Line 2, position 2.” 错误哈

下面我推荐给大家一个简单的代码方法,那就是在 SetPackageProperties 完成之后再调用 AddCoreFilePropertiesPart 方法,也就是将 SetPackageProperties 方法先调用,代码如下

代码语言:javascript
复制
        public void CreatePackage(string filePath)
        {
            using PresentationDocument package = PresentationDocument.Create(filePath, PresentationDocumentType.Presentation);

            var document = package;

            SetPackageProperties(package);

            if (document.CoreFilePropertiesPart is null)
            {
                document.AddCoreFilePropertiesPart();
            }
        }

        private void SetPackageProperties(OpenXmlPackage document)
        {
            document.PackageProperties.Creator = "dexi lin";
            document.PackageProperties.Title = "PowerPoint 演示文稿";
            document.PackageProperties.Revision = "1";
            document.PackageProperties.Created = System.Xml.XmlConvert.ToDateTime("2024-01-24T09:19:23Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind);
            document.PackageProperties.Modified = System.Xml.XmlConvert.ToDateTime("2024-01-24T09:19:34Z", System.Xml.XmlDateTimeSerializationMode.RoundtripKind);
            document.PackageProperties.LastModifiedBy = "dexi lin";
        }

看起来十分简单,只是将 SetPackageProperties 和 AddCoreFilePropertiesPart 的调用顺序调换即可

如果 SetPackageProperties 等是生成的代码,不想修改顺序,可以在 AddCoreFilePropertiesPart 方法调用之前,随意对 PackageProperties 的属性进行赋值,如下面代码

代码语言:javascript
复制
        public void CreatePackage(string filePath)
        {
            using PresentationDocument package = PresentationDocument.Create(filePath, PresentationDocumentType.Presentation);

            var document = package;

            if (document.CoreFilePropertiesPart is null)
            {
                document.PackageProperties.Creator = "xxxxx"; // 随意
                document.AddCoreFilePropertiesPart();
            }

            CreateParts(package);
        }

通过以上的方法即可成功创建 core.xml 文件到 PPTX 文件里面。如果你使用本文的方法没有创建成功,那我推荐你使用下面的方法拉取本文的代码,跑一下代码试试

本文以上代码放在githubgitee 欢迎访问

可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

代码语言:javascript
复制
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin f4a8f9c5265f3e52f3b9f93bb6570c9e73dc41c4

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码

代码语言:javascript
复制
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin f4a8f9c5265f3e52f3b9f93bb6570c9e73dc41c4

获取代码之后,进入 WefejurkawFekejiyi 文件夹

更多关于 CoreFilePropertiesPart 请参阅 ECMA 376 文档的 15.2.12.1 章内容

更多关于 OpenXML 相关知识,请参阅 Office 使用 OpenXML SDK 解析文档博客目录

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档