使用 HashTable 或其他东西的 iTextSharp 属性?
这里是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改以下行:
而不是
应该修复这两个错误。
发生这种情况的原因是因为您尝试从哈希表转换为通用字典,并且哈希表没有 隐式类型转换可用于此。请查看此处了解两者之间的区别哈希表和字典。
Change the below line:
instead of
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.
我想这一行
会引发错误,然后这一行也会引发错误:
pdfStamper.MoreInfo 似乎是 System.Collections.Generic.Dictionary 类型,因此您所要做的就是替换
为
类型必须匹配。我无法测试这个,所以我不知道我是否找到了正确的行,但这样的东西会起作用。
I suppose this line
throws the error, and then also this one:
pdfStamper.MoreInfo seems to be of type System.Collections.Generic.Dictionary, so all you have to do is replace
by
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.