2012-01-23 2 views
0

noob to C# 여기서 iTextSharp 예제를 사용합니다. 기존 exe로 제목, 설명 및 키워드를 기존 PDF로 변경했습니다. MS 비주얼 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(); 
      } 
     } 
    } 
} 

답변

2

변경 아래 라인 :

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

대신

Hashtable newInfo = pdfReader.Info; 

의 두 오류를 수정해야합니다.

해시 테이블에서 일반 사전으로 캐스팅하려고했기 때문에 해시 테이블에 implicit 형식 변환을 사용할 수 없기 때문입니다. 해시 테이블과 사전의 차이점을 보려면 here을보십시오.

+0

당신의 의견이 정확합니다. C#으로 새 라이브러리를 발견 할 때 메소드를 호출하고 속성에 액세스 할 때'var' 키워드를 사용하는 것이 더 쉬울 때가 있습니다. 'var newInfo = pdfReader.Info;'확실히'var'을 악용 할 수 있지만, 이런 것들은 매우 유용 할 수 있습니다. –

+0

크리스, 예,하지만 혼란을 피하기 위해 명확한 설명을 위해 전체 정의를 사용하고 싶습니다. –

+0

Ahh thankyou 얘들 아, 내 인생에 대한 내가 볼 수 없습니다 : pdfReader.Info는 사전으로 정의되었습니다. pdfReader.Info에 대한 문서는 어디에 있습니까? – ChrisAdmin

1

나는

Hashtable newInfo = pdfReader.Info; 

이 일 발생이 줄을 가정 E 오류, 그리고 그 다음이 하나

pdfStamper.MoreInfo = newInfo; 

pdfStamper.MoreInfo 그래서 당신이해야 할 모든

에 의해

Hashtable newInfo = pdfReader.Info; 

교체되는 입력 System.Collections.Generic.Dictionary이 될 것 같다

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

유형이 일치해야합니다. 나는 이것을 테스트 할 수 없기 때문에 정확한 라인을 찾았는지 모르지만, 이와 같은 것은 작동 할 것이다.

관련 문제