我在我的 C# 项目中使用 iTextSharp 库来阅读和编辑 pdf 文档。 现在我想更改某个 pdf 文档的标题。 我对这个问题进行了很多搜索,但对我来说没有任何用处。 我发现的最好的是:
PdfReader pdfReader = new PdfReader(filePath);
using (FileStream fileStream = new FileStream(newFilePath,
FileMode.Create,
FileAccess.Write))
{
string title = pdfReader.Info["Title"] as string;
Trace.WriteLine("Existing title: " + title);
PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
// The info property returns a copy of the internal HashTable
Hashtable newInfo = pdfReader.Info;
newInfo["Title"] = "New title";
pdfStamper.MoreInfo = newInfo;
pdfReader.Close();
pdfStamper.Close();
}
但是 Visual Studio 说 System.Collection.Hashtable
无法隐式转换为 System.Collections.Generic.IDictionary<string,string>
.已有一个转化。
希望有人能帮助我。或者有另一个使用 iTextSharp 的解决方案来编辑标题。
最佳答案
你需要改变这个:
Hashtable newInfo = pdfReader.Info;
对此:
Dictionary<string, string> newInfo = pdfReader.Info;
因为正如错误所说,pdfReader.Info
返回对 IDictionary<string, string>
的引用, 不是 Hashtable
.
注意如果要修改Info
,无需创建额外的局部变量:
var title = "Title";
if (pdfReader.Info.ContainsKey(title))
{
pdfReader.Info[title] = "NewTitle";
}
关于C#:iTextSharp,如何编辑 pdf 文档的标题属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734059/