使用 HashTable 或其他东西的 iTextSharp 属性?

发布于 2024-12-28 08:22:14 字数 2532 浏览 3 评论 0原文

这里是 C# 菜鸟,使用周围的 iTextSharp 示例,所以我制作了一个基本的 exe 来更改现有 PDF 的标题、描述和关键字。使用 MS Visual C# 2010,我不理解对 C# 的所有“通用”更改,因此我收到此错误:

Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,string>' to 'System.Collections.Hashtable'

以及

Cannot implicitly convert type 'System.Collections.Hashtable' to 'System.Collections.Generic.IDictionary<string,string>'. An explicit conversion exists (are you missing a cast?)

代码:

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if ((args == null) || (args.Length < 3))
            {
                Console.WriteLine("args: PDFProp [fileName] [outputPath] [Title] [Description] [Keywords]");
                Console.WriteLine();
                Console.Write("<Continue>");
                Console.ReadLine();
                return;
            }

            string filePath = args[0];
            string newFilePath = args[1];
            string title = args[2];
            string desc = "";
            string keywords = "";
            if (args.Length > 3)
                desc = args[3];
            if (args.Length > 4)
                keywords = args[4];

            Console.Write(filePath + "->" + newFilePath + " title: " + title + " description: " + desc + " keywords: " + keywords);
            Console.WriteLine();
            Console.ReadLine();

            PdfReader pdfReader = new PdfReader(filePath);
            using (FileStream fileStream = new FileStream(newFilePath, FileMode.Create, FileAccess.Write))
            {
                //   string title = pdfReader.Info["Title"] as string;           

                PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);

                // The info property returns a copy of the internal HashTable
                Hashtable newInfo = pdfReader.Info;             // error 1 

                newInfo["Title"] = title;

                if (args.Length > 3)
                    newInfo["Description"] = desc;
                if (args.Length > 4)
                    newInfo["Keywords"] = keywords;

                pdfStamper.MoreInfo = newInfo;                  // error 2

                pdfReader.Close();
                pdfStamper.Close();
            }
        }
    }
}

noob to C# here, using iTextSharp examples from around SO I've made a basic exe to change title, description and keywords to an existing PDF. Using MS Visual C# 2010, I don't understand all this 'Generic' change to C#, so I'm getting this error:

Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,string>' to 'System.Collections.Hashtable'

and

Cannot implicitly convert type 'System.Collections.Hashtable' to 'System.Collections.Generic.IDictionary<string,string>'. An explicit conversion exists (are you missing a cast?)

The Code:

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if ((args == null) || (args.Length < 3))
            {
                Console.WriteLine("args: PDFProp [fileName] [outputPath] [Title] [Description] [Keywords]");
                Console.WriteLine();
                Console.Write("<Continue>");
                Console.ReadLine();
                return;
            }

            string filePath = args[0];
            string newFilePath = args[1];
            string title = args[2];
            string desc = "";
            string keywords = "";
            if (args.Length > 3)
                desc = args[3];
            if (args.Length > 4)
                keywords = args[4];

            Console.Write(filePath + "->" + newFilePath + " title: " + title + " description: " + desc + " keywords: " + keywords);
            Console.WriteLine();
            Console.ReadLine();

            PdfReader pdfReader = new PdfReader(filePath);
            using (FileStream fileStream = new FileStream(newFilePath, FileMode.Create, FileAccess.Write))
            {
                //   string title = pdfReader.Info["Title"] as string;           

                PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);

                // The info property returns a copy of the internal HashTable
                Hashtable newInfo = pdfReader.Info;             // error 1 

                newInfo["Title"] = title;

                if (args.Length > 3)
                    newInfo["Description"] = desc;
                if (args.Length > 4)
                    newInfo["Keywords"] = keywords;

                pdfStamper.MoreInfo = newInfo;                  // error 2

                pdfReader.Close();
                pdfStamper.Close();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀お生予夺 2025-01-04 08:22:14

更改以下行:

Dictionary<string,string> newInfo = pdfReader.Info;             

而不是

Hashtable newInfo = pdfReader.Info;

应该修复这两个错误。

发生这种情况的原因是因为您尝试从哈希表转换为通用字典,并且哈希表没有 隐式类型转换可用于此。请查看此处了解两者之间的区别哈希表和字典。

Change the below line:

Dictionary<string,string> newInfo = pdfReader.Info;             

instead of

Hashtable newInfo = pdfReader.Info;

Should fix both errors.

The reason why this is happening is because you are trying to cast from a Hashtable to a Generic Dictionary and the hashtable does not have an implicit type conversion available for this. look here to see the difference between a hashtable and a Dictionary.

不知所踪 2025-01-04 08:22:14

我想这一行

Hashtable newInfo = pdfReader.Info; 

会引发错误,然后这一行也会引发错误:

pdfStamper.MoreInfo = newInfo; 

pdfStamper.MoreInfo 似乎是 System.Collections.Generic.Dictionary 类型,因此您所要做的就是替换

Hashtable newInfo = pdfReader.Info;

System.Collections.Generic.Dictionary<string,string> newInfo = pdfReader.Info;

类型必须匹配。我无法测试这个,所以我不知道我是否找到了正确的行,但这样的东西会起作用。

I suppose this line

Hashtable newInfo = pdfReader.Info; 

throws the error, and then also this one:

pdfStamper.MoreInfo = newInfo; 

pdfStamper.MoreInfo seems to be of type System.Collections.Generic.Dictionary, so all you have to do is replace

Hashtable newInfo = pdfReader.Info;

by

System.Collections.Generic.Dictionary<string,string> newInfo = pdfReader.Info;

The types must match. I can't test this, so I don't know if I've found the correct lines, but something like this will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文