使用库.docx设置docx属性 [英] Set docx properties using library .docx

查看:184
本文介绍了使用库.docx设置docx属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为使用.net的docx库创建的文件设置诸如标题,作者,主题之类的属性?

因此,如果您想更新title属性(明确命名为 dc:title ),则只需添加一个新的Core属性(通过 AddCoreProperty()方法)匹配相同的名称,然后保存文件以保留更改:

 //加载您的文档var wordFile = DocX.Load(@"your-docx-file-path");//更新元数据wordFile.AddCoreProperty("dc:title",示例标题");wordFile.Save(); 

执行完此操作后,您应该能够重新打开文件,并看到您所做的更改:

如您所见,根据上面的示例代码, dc:title 属性现在设置为示例标题".

How to set properties like title, author, subject for a file created with docx library for .net ?

docx

解决方案

The DocX project that you provided appears to be able to easily access the metadata properties that you are referring to and can do so quite easily by using the CoreProperties property as seen below :

// Load your Document
var wordFile = Novacode.DocX.Load(@"your-docx-file-path");
// Access Metadata properties
var props = wordFile.CoreProperties;

The issue here is that this collection of properties is read only, so you won't be able to easily change them. However, you may be able to take a look at what the values look like and attempt to add one manually :

So if you wanted to update the title property (clearly named dc:title), you would simply need to add a new Core Property (via the AddCoreProperty() method) that matched that same name and then save the file to persist the changes :

// Load your Document
var wordFile = DocX.Load(@"your-docx-file-path");
// Update Metadata
wordFile.AddCoreProperty("dc:title", "Example Title");
wordFile.Save();

After doing this, you should be able to re-open the file and see that your changes reflected :

As you can see the dc:title property is now set to "Example Title" as per the example code above.

这篇关于使用库.docx设置docx属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆