Password protected PDF using C#

后端 未结 3 1545
陌清茗
陌清茗 2020-12-03 03:40

I am creating a pdf document using C# code in my process. I need to protect the docuemnt with some standard password like \"123456\" or some account number. I need to do th

3条回答
  •  借酒劲吻你
    2020-12-03 04:01

    I am creating a pdf document using C# code in my process

    Are you using some library to create this document? The pdf specification (8.6MB) is quite big and all tasks involving pdf manipulation could be difficult without using a third party library. Password protecting and encrypting your pdf files with the free and open source itextsharp library is quite easy:

    using (Stream input = new FileStream("test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
    using (Stream output = new FileStream("test_encrypted.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, "secret", "secret", PdfWriter.ALLOW_PRINTING);
    }
    

提交回复
热议问题