使用 C# 受密码保护的 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:51:36  来源:igfitidea点击:

Password protected PDF using C#

c#pdf-generation

提问by balaweblog

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 this without any reference dlls like pdf writer.

我正在我的过程中使用 C# 代码创建一个 pdf 文档。我需要使用一些标准密码(如“123456”或一些帐号)来保护文档。我需要在没有任何参考 dll 的情况下执行此操作,例如 pdf writer。

I am generating the PDF file using SQL Reporting services reports.

我正在使用 SQL Reporting Services 报告生成 PDF 文件。

Is there are easiest way.

有没有最简单的方法。

采纳答案by Darin Dimitrov

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

我正在我的过程中使用 C# 代码创建一个 pdf 文档

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 itextsharplibrary is quite easy:

您是否使用某个库来创建此文档?在PDF规范(8.6MB)是相当大的,涉及操作PDF的所有任务,可以在不使用第三方库是困难的。使用免费和开源的itextsharp库密码保护和加密您的 pdf 文件非常简单:

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

回答by Bobrovsky

It would be very difficult to do this without using a PDF library. Basically, you'll need to develop such library yourselves.

如果不使用 PDF 库,就很难做到这一点。基本上,您需要自己开发这样的库。

With help of a PDF library everything is much simpler. Here is a sample that shows how a document can easily be protected using Docotic.Pdf library:

在 PDF 库的帮助下,一切都变得简单多了。这是一个示例,展示了如何使用Docotic.Pdf 库轻松保护文档:

public static void protectWithPassword(string input, string output)
{
    using (PdfDocument doc = new PdfDocument(input))
    {
        // set owner password (a password required to change permissions)
        doc.OwnerPassword = "pass";

        // set empty user password (this will allow anyone to
        // view document without need to enter password)
        doc.UserPassword = "";

        // setup encryption algorithm
        doc.Encryption = PdfEncryptionAlgorithm.Aes128Bit;

        // [optionally] setup permissions
        doc.Permissions.CopyContents = false;
        doc.Permissions.ExtractContents = false;

        doc.Save(output);
    }
}

Disclaimer: I work for the vendor of the library.

免责声明:我为图书馆的供应商工作。

回答by horseman1210

If anyone is looking for a IText7 reference.

如果有人正在寻找 IText7 参考。

    private string password = "@d45235fewf";
    private const string pdfFile = @"C:\Temp\Old.pdf";
    private const string pdfFileOut = @"C:\Temp\New.pdf";

public void DecryptPdf()
{
        //Set reader properties and password
        ReaderProperties rp = new ReaderProperties();
        rp.SetPassword(new System.Text.UTF8Encoding().GetBytes(password));

        //Read the PDF and write to new pdf
        using (PdfReader reader = new PdfReader(pdfFile, rp))
        {
            reader.SetUnethicalReading(true);
            PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdfFileOut));
            pdf.GetFirstPage(); // Get at the very least the first page
        }               
}