iTextSharp Password Protected PDF

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

The following question and answer on StackOverflow show how to generate a PDF that cannot be opened without the appropriate password.

Password protected PDF using C#

I would like to use this framework similarly, but slightly altered to allow my users to "open" the PDF without needing the password, but only allow them to EDIT the PDF if they have the password.

Is that possible with iTextSharp?

if this matters, I am working in C# 4.0 within a WF 4.0 custom activity.

回答1:

Yes, there are two passwords that you can pass to PdfEncryptor.Encrypt(), userPassword and ownerPassword. Just pass null to the userPassword and people will be able to open it without specify a password.

        string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);         string InputFile = Path.Combine(WorkingFolder, "Test.pdf");         string OutputFile = Path.Combine(WorkingFolder, "Test_enc.pdf");          using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))         {             using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))             {                 PdfReader reader = new PdfReader(input);                 PdfEncryptor.Encrypt(reader, output, true, null, "secret", PdfWriter.ALLOW_SCREENREADERS);             }         }


回答2:

Another implementation:

public static void Common_PassWordProtectPDF_Static_WithoutEmail(FileInfo[] filteredfiles, string strAgentName, string strAgentCode, string strpassword, string strEmailID, string sourcefolder, string strdestfolder, string strdestinationFileName) {     foreach (FileInfo file in filteredfiles)     {         //string sourcePdf = Convert.ToString(ConfigurationManager.AppSettings["SourceFolder"]) + "\\" + file.Name;         //string strdestPdf = Convert.ToString(ConfigurationManager.AppSettings["DestinationFolder"]) + file.Name;      string sourcePdf = sourcefolder + "\\" + file.Name;     string strdestPdf = strdestfolder + strdestinationFileName;      using (Stream input = new FileStream(sourcePdf, FileMode.Open, FileAccess.Read, FileShare.Read))     {         //sourcePdf  unsecured PDF file         //destPdf secured PDF file          using (Stream output = new FileStream(strdestPdf, FileMode.Create, FileAccess.Write, FileShare.None))         {             PdfReader pdfReader = new PdfReader(input);             X509Store store = new X509Store("My");             store.Open(OpenFlags.ReadOnly);              X509Certificate2 cert = new X509Certificate2();             RSACryptoServiceProvider csp = null;             AcroFields fields = pdfReader.AcroFields;              foreach (X509Certificate2 mCert in store.Certificates)             {                 //TODO's                 string strresult = mCert.GetName();                 bool str123 = false;                 if (strresult.Contains("Certificate name") == true)                 {                     csp = (RSACryptoServiceProvider)mCert.        
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!