从Windows窗体应用程序中的Word文档的属性中获取标题

时间:2017-10-25 16:13:59

标签: c# .net ms-word

我想从Windows窗体应用程序中的属性中获取Word文档的标题,并将其存储在字符串中。如图所示。如何使用C#进行此操作?

As show in the picture

2 个答案:

答案 0 :(得分:1)

您可以获得这样的标题,您可能需要为包装内容添加一个额外的ddl文件。右键单击References并在Framework下添加对WindowsBase的引用以使用打包。

Package file = Package.Open(@"C:\Users\pamjl\Desktop\Code\New Microsoft Word Document.docx", FileMode.Open, FileAccess.Read);
String yourTitle = file.PackageProperties.Title;

答案 1 :(得分:1)

将DocumentFormat.OpenXml nuget包添加到项目中,并添加对WindowsBase的引用。

使用此导入:

using DocumentFormat.OpenXml.Packaging;

这将检索标题:

var filePath = ""; // path to your .docx file
using (var document = WordprocessingDocument.Open(filePath, true))
{
    var title = document.PackageProperties.Title;
}