OpenXML 创建自定义 属性 不起作用

OpenXML creating custom property doesnt work

我应该如何创建自定义 属性? 我试过这个,但它不起作用。 我需要添加一些东西吗 属性?

谢谢:)

MemoryStream ms = new MemoryStream();
WordStream.CopyTo(ms);
ms.Position = 0;

WordprocessingDocument word = WordprocessingDocument.Open(ms, true);
Properties CustomeProperties = word.CustomFilePropertiesPart.Properties;
Boolean Name1 = false;

foreach (CustomDocumentProperty customeProperty in CustomeProperties) 
{
      if (customeProperty.Name.Equals("Language"))
      {
          customeProperty.VTLPWSTR = new VTLPWSTR("english");
          Name1 = true;
      }
}

if (!Name1) {
    var newProp = new CustomDocumentProperty();
    newProp.Name = "Language";
    newProp.VTLPWSTR = new VTLPWSTR("english");
    newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
    CustomeProperties.Append(newProp);
}

CustomeProperties.Save();
word.Save();

word.Close();
ms.Position = 0; 

要使用 DocumentFormat.Openxml 将自定义 属性 添加到文字处理文档,请尝试以下操作:

Download/install NuGet 包DocumentFormat.Openxml

添加引用:WindowsBase

VS 2019:

  • 在 VS 菜单中,单击 项目
  • Select 添加参考...
  • 单击程序集
  • 勾选WindowsBase
  • 点击确定

添加以下using语句:

using System;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.CustomProperties;
using DocumentFormat.OpenXml.VariantTypes;

以下代码改编自 Set a custom property in a word processing document (Open XML SDK),其中指出:“...始终删除然后重新创建元素更简单。”

public void AddCustomPropertyToWordDocument(string filename, string propertyName, string propertyValue)
{
    if (String.IsNullOrEmpty(filename))
        throw new Exception("Filename not specified.");

    // Open Wordprocessing document. 
    using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
    {
        // Add a main document part.
        MainDocumentPart mainPart = doc.MainDocumentPart;

        //CustomFilePropertiesPart
        CustomFilePropertiesPart customProps = doc.CustomFilePropertiesPart;

        //add part if it doesn't exist
        if (customProps == null)
        {
            customProps = doc.AddCustomFilePropertiesPart();
            customProps.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties();
        }

        DocumentFormat.OpenXml.CustomProperties.Properties props = customProps.Properties;

        //check if property already exists
        var prop = props.Where(p => ((CustomDocumentProperty)p).Name.Value == propertyName).FirstOrDefault();

        //remove property, if it exists
        string currentPropValue;
        if (prop != null)
        {
            currentPropValue = prop.InnerText;
            prop.Remove();
        }

        //create custom property
        CustomDocumentProperty customProp = new CustomDocumentProperty();
        customProp.Name = propertyName;
        customProp.VTLPWSTR = new VTLPWSTR(propertyValue);
        customProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";

        // Append the new property, and 
        // fix up all the property ID values. 
        // The PropertyId value must start at 2.
        props.AppendChild(customProp);
        int pid = 2;
        foreach (CustomDocumentProperty item in props)
        {
            item.PropertyId = pid++; //increment
        }

        props.Save();

        // Save changes to the main document part. 
        doc.MainDocumentPart.Document.Save();
    }
}

用法:

using (OpenFileDialog ofd = new OpenFileDialog())
{
    ofd.Filter = "Word Document (*.docx)|*.docx";

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        AddCustomPropertyToWordDocument(ofd.FileName, "Language", "English");
    }
}