C#:iTextSharp,如何编辑 pdf 文档的标题属性?

标签 c# pdf itextsharp

我在我的 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/

相关文章:

itextsharp - 垂直对齐在复合模式下不起作用

itextsharp - 将 AcroField 文本大小设置为自动

c# - 在 CaSTLe Windsor 中的其他类之间共享类的实例

c# - 无法让 ASP.NET Core Web API 与 IIS 一起工作

c# - CheckedListBox 和带有索引的选中项目列表

c# - 用于处理 MySQL 的带有队列的线程

python - Django 和 Reportlab 问题

c# - 使用 itextsharp 从 PDF 中提取图像

pdf - BibTeX 引用中的 LaTeX 数学不适用于 pandoc

javascript - 提供下载 pdf 或在浏览器中打开的选项