PdfSharp,正在更新C#中的元数据错误

PdfSharp,正在更新C#中的元数据错误,c#,metadata,pdfsharp,C#,Metadata,Pdfsharp,我正在使用PdfSharp参考库尝试向我的程序添加添加元数据标记的功能。我能够成功地将元数据标记添加到文档中,但在更新现有自定义属性上的标记时遇到问题。每当我尝试使用我的方法更新自定义属性时,我都会收到以下异常: “'System.Collections.Generic.KeyValuePair'不包含'Name'的定义。” 你们能告诉我我是否在下面的foreach循环中对if语句进行编码,以正确循环PDF文档中的所有自定义元素,查看它是否存在并且是否需要更新吗?谢谢 public void A

我正在使用
PdfSharp
参考库尝试向我的程序添加添加元数据标记的功能。我能够成功地将元数据标记添加到文档中,但在更新现有自定义属性上的标记时遇到问题。每当我尝试使用我的方法更新自定义属性时,我都会收到以下异常:

“'System.Collections.Generic.KeyValuePair'不包含'Name'的定义。”

你们能告诉我我是否在下面的foreach循环中对if语句进行编码,以正确循环PDF文档中的所有自定义元素,查看它是否存在并且是否需要更新吗?谢谢

public void AddMetaDataPDF(string property, string propertyValue, string    
                                                                   path)
{
    PdfDocument document = PdfReader.Open(path);
    bool propertyFound = false;

    try {
           dynamic properties = document.Info.Elements;
           foreach(dynamic p in properties)
           {
               //Check to see if the property exists. If it does, update   
                   value.
               if(string.Equals(p.Name, property,  
                StringComparison.InvariantCultureIgnoreCase))
               {
                   document.Info.Elements.SetValue("/" + property, new   
                           PdfString(propertyValue));
               }
           }
           // the property doesn't exist so add it
           if(!propertyFound)
           {
               document.Info.Elements.Add(new KeyValuePair<String, PdfItem>   
                   ("/"+ property, new PdfString(propertyValue)));
           }
      }

      catch (Exception ex)
      {
          MessageBox.Show(path + "\n" + ex.Message);
          document.Close();

      }
      finally
      {
          if(document != null)
          {
              document.Save(path);
              document.Close();
          }
      }
}
public void AddMetaDataPDF(字符串属性,字符串属性值,字符串
(路径)
{
PdfDocument document=PdfReader.Open(路径);
bool propertyFound=false;
试一试{
动态属性=document.Info.Elements;
foreach(属性中的动态p)
{
//检查属性是否存在。如果存在,请更新
价值
if(string.Equals)(p.Name,property,
StringComparison.InvariantCultureInogoreCase)
{
document.Info.Elements.SetValue(“/”+属性,新
PdfString(propertyValue));
}
}
//该属性不存在,请添加它
如果(!propertyFound)
{
document.Info.Elements.Add(新的KeyValuePair
(“/”+属性,新的PDF字符串(propertyValue));
}
}
捕获(例外情况除外)
{
MessageBox.Show(路径+“\n”+ex.Message);
document.Close();
}
最后
{
如果(文档!=null)
{
文件保存(路径);
document.Close();
}
}
}

我没有尝试您的代码,但使用此库时的一个常见问题是,需要在属性名称前添加斜杠才能找到它。下面的代码将实现这个技巧

PdfDocument document = PdfReader.Open(path);
var properties = document.Info.Elements;
if (properties.ContainsKey("/" + propertyName))
{
    properties.SetValue("/" + propertyName, new PdfString(propertyValue));
}
else
{
    properties.Add(new KeyValuePair<String, PdfItem>("/" + propertyName, new PdfString(propertyValue)));
}
document.Save(path);
document.Close();
PdfDocument document=PdfReader.Open(路径);
var属性=document.Info.Elements;
if(properties.ContainsKey(“/”+propertyName))
{
properties.SetValue(“/”+propertyName,新的PdfString(propertyValue));
}
其他的
{
添加(新的KeyValuePair(“/”+propertyName,新的PdfString(propertyValue));
}
文件保存(路径);
document.Close();

此外,PDF文件不应进行写保护。否则,在调用PdfSharp之前,您需要使用解锁文件的工具。

非常感谢,先生。成功了。你们在这个论坛上真是太棒了,很有帮助。