iTextSharp Password Protected PDF

前端 未结 2 1037
别那么骄傲
别那么骄傲 2020-11-30 05:17

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

2条回答
  •  抹茶落季
    2020-11-30 05:40

    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);
                }
            }
    

提交回复
热议问题