Set docx properties using library .docx

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-19 15:48:21

问题


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

docx


回答1:


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.



来源:https://stackoverflow.com/questions/36405511/set-docx-properties-using-library-docx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!