2012-06-05 83 views
1

Adob​​e IFilter不提供提供密碼以打開受密碼保護的PDF文件的機制,因此它不能用於打開受密碼保護的文件。如何以編程方式打開受密碼保護的PDF文件?

我想知道,是否有一種相對直接的方式來以編程方式檢索PDF文件中的實際加密數據,使用標準加密API對其進行解密,然後用解密的數據構建新的PDF文件?

回答

2

要打開一個密碼保護的PDF,你將需要開發至少一個PDF解析器,解密和發電機。不過,我不會推薦這麼做。這遠遠不是一件容易完成的任務。

在PDF庫的幫助下,一切都非常簡單。您可能需要爲該任務嘗試Docotic.Pdf library(免責聲明:我爲圖書館的供應商工作)。

這是給你的任務的例子:

public static void unprotectPdf(string input, string output) 
{ 
    bool passwordProtected = PdfDocument.IsPasswordProtected(input); 
    if (passwordProtected) 
    { 
     string password = null; // retrieve the password somehow 

     using (PdfDocument doc = new PdfDocument(input, password)) 
     { 
         // clear both passwords in order 
      // to produce unprotected document 
         doc.OwnerPassword = ""; 
         doc.UserPassword = ""; 

         doc.Save(output); 
     } 
    } 
    else 
    { 
     // no decryption is required 
     File.Copy(input, output, true); 
    } 
} 

Docotic.Pdf也可以extract text (formatted or not) from PDFs。它可能對索引有用(我想這是你所要做的,因爲你提到了Adobe IFilter)

2

如果使用SpirePDF那麼你可以得到頁的圖像進行這樣的ecrypted PDF的:

using System; 
using System.Drawing; 
using Spire.Pdf; 
namespace PDFDecrypt 
{ 
    class Decrypt 
    { 
     static void Main(string[] args) 
     { 
      //Create Document 
      String encryptedPdf = @"D:\work\My Documents\Encryption.pdf"; 
      PdfDocument doc = new PdfDocument(encryptedPdf, "123456"); 

      //Extract Image 
      Image image = doc.Pages[0].ImagesInfo[0].Image; 

      doc.Close(); 

      //Save 
      image.Save("EmployeeInfo.png", System.Drawing.Imaging.ImageFormat.Png); 

      //Launch 
      System.Diagnostics.Process.Start("EmployeeInfo.png"); 
     } 
    } 
} 
相關問題