PdfSharp,在 C# 错误中更新元数据
PdfSharp, Updating Metadata in C# Error
我正在使用 PdfSharp
参考库尝试向我的程序添加添加元数据标签的功能。我能够成功地将元数据标签添加到文档,但我在更新现有自定义属性上的标签时遇到问题。每当我尝试使用我的方法更新自定义属性时,我都会收到以下异常:
"'System.Collections.Generic.KeyValuePair' does not contain a definition for '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();
}
}
}
我没有尝试您的代码,但使用此库时的一个常见问题是您需要在 属性 的名称前添加斜杠才能找到它。下面的代码可以解决问题。
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();
此外,PDF 文件不应被写保护。否则您需要在调用 PdfSharp 之前使用解锁文件的工具。
我正在使用 PdfSharp
参考库尝试向我的程序添加添加元数据标签的功能。我能够成功地将元数据标签添加到文档,但我在更新现有自定义属性上的标签时遇到问题。每当我尝试使用我的方法更新自定义属性时,我都会收到以下异常:
"'System.Collections.Generic.KeyValuePair' does not contain a definition for '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();
}
}
}
我没有尝试您的代码,但使用此库时的一个常见问题是您需要在 属性 的名称前添加斜杠才能找到它。下面的代码可以解决问题。
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();
此外,PDF 文件不应被写保护。否则您需要在调用 PdfSharp 之前使用解锁文件的工具。