2016-05-24 18 views
0

iTextSharpを使用してPDFからイメージを作成するのに成功しました。 PDFのページ数に等しい画像を作成しますが、生成された画像は、どの画像ビューアソフトウェアでもプレビューされません。それは画像が壊れていると言います。以下は私が作成したコードです。iTextSharpを使用して作成した表示イメージです。

try 
     { 
      PdfReader reader = null; 
      int currentPage = 1; 
      int pageCount = 0; 
      string destinationFolderPath = string.Format(@"{0}PageImages\{1}", BaseDataPath, Convert.ToString(documentId)); 

      if (!Directory.Exists(destinationFolderPath)) 
      { 
       Directory.CreateDirectory(destinationFolderPath); 
      } 


      reader = new PdfReader(filePath); 
      reader.RemoveUnusedObjects(); 
      pageCount = reader.NumberOfPages; 
      string ext = ".png"; 

      for (int i = 1; i <= pageCount; i++) 
      { 
       PdfReader reader1 = new PdfReader(filePath); 
       string destinationFilePath = string.Format(@"{0}/{1}{2}", destinationFolderPath, Convert.ToString(i), ext); 
       reader1.RemoveUnusedObjects(); 
       Document doc = new Document(reader1.GetPageSizeWithRotation(currentPage)); 
       PdfCopy pdfCpy = new PdfCopy(doc, new FileStream(destinationFilePath, FileMode.Create)); 
       doc.Open(); 
       for (int j = 1; j <= 1; j++) 
       { 
        PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage); 
        //pdfCpy.SetFullCompression(); 
        pdfCpy.AddPage(page); 
        currentPage += 1; 
       } 
       doc.Close(); 
       pdfCpy.Close(); 
       reader1.Close(); 
       reader.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 

誰かが間違っているとお考えですか? おかげ

答えて

0

あなたはPdfCopyを使用してPDFファイルを作成しているが、あなたはPNGファイルを作成しているかのように、あなたがそのPDFを格納するには:

string ext = ".png"; 
string destinationFilePath = 
    string.Format(@"{0}/{1}{2}", 
    destinationFolderPath, Convert.ToString(i), ext); 
PdfCopy pdfCpy = new PdfCopy(doc, 
    new FileStream(destinationFilePath, FileMode.Create)); 

あなたは、PDFビューアで.pngファイルを開くことができません。あなたのオペレーティングシステムは、作成しているファイルを画像のように開こうとしますが、その「画像」のバイトはPDFバイトになり、画像ビューアはそれを認識しません。

変更この行:これに

string ext = ".png"; 

string ext = ".pdf"; 

そして、あなたは、PDFビューアでファイルを開くことができます。

ところであなたのコードは厄介です。例えば。あなたは一度だけ、何かを実行するための外観を作成したい理由を私は理解していない:

for (int j = 1; j <= 1; j++) 

をも:それは再考、PNGへのPDFのページを変換するためにあなたの意図だ場合。 iTextSharpはPDFを画像に変換しません。

関連する問題