简体   繁体   English

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

[英]PdfSharp, Updating Metadata in C# Error

I am using the PdfSharp reference library to attempt to add functionality to my program that adds metadata tags.我正在使用PdfSharp参考库来尝试向我的程序添加添加元数据标签的功能。 I am able to successfully add metadata tags to a document, but I am having an issue with updating the tags on existing custom properties.我能够成功地将元数据标签添加到文档,但我在更新现有自定义属性上的标签时遇到问题。 Whenever I attempt to use my method to update the custom properties, I receive the following exception:每当我尝试使用我的方法更新自定义属性时,都会收到以下异常:

"'System.Collections.Generic.KeyValuePair' does not contain a definition for 'Name'." “‘System.Collections.Generic.KeyValuePair’不包含‘Name’的定义。”

Could you guys tell me if I am coding the if statement in the foreach loop below to correctly loop through all of the custom elements in the PDF document to see if it exists and needs to be updated?你们能告诉我我是否在下面的 foreach 循环中编写 if 语句以正确循环遍历 PDF 文档中的所有自定义元素以查看它是否存在并且需要更新吗? Thanks.谢谢。

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();
          }
      }
}

I didn't try your code but a common issue when working with this library is that you need to add a slash before the name of the property for it to be found.我没有尝试你的代码,但使用这个库时的一个常见问题是你需要在属性名称前添加一个斜杠才能找到它。 The code below will make the trick.下面的代码将成功。

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();

Also the PDF file shouldn't be write protected.此外,PDF 文件不应被写保护。 Otherwise you need to use a tool for unlocking the file before calling PdfSharp.否则,您需要在调用 PdfSharp 之前使用解锁文件的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM