ここからnoob to C#ここで、iTextSharpの例を使用しています。既存のPDFにタイトル、説明、キーワードを変更するための基本的なexeを作成しました。 MSのVisual C#の2010を使用して、私はC#に、すべてこの 'ジェネリック' な変化を理解していないので、私はこのエラーを取得しています:iTextSharp HashTableなどを使用したプロパティ
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();
}
}
}
}
私はこのライン
Hashtable newInfo = pdfReader.Info;
が目を投げたと
あなたの名前は間違いありませんので、私はそれをアップ票します。 C#で新しいライブラリを発見したときにメソッドを呼び出してプロパティにアクセスするときに 'var'キーワードを使用するほうが簡単なことを追加したかっただけです。 'var newInfo = pdfReader.Info;'あなたは間違いなく 'var'を悪用することができますが、このようなことは非常に役に立ちます。 –
クリス、ええ、わかりやすくするために、私はむしろ混乱を避けるために完全な定義を使用したいと思います。 –
Ahh thankyouみんな、私の人生のために私はどこに見えなかった:pdfReader.Infoは辞書として定義されていました、どこにpdfReader.Infoのドキュメントがありますか? – ChrisAdmin