17

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.

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

Is there are easiest way.

3 Answers 3

30

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);
}
3
  • 7
    Note that itextsharp requires a license for commercial use, unless your code is also released under the same license it uses. Prices only available upon application.
    – Spongeboy
    Jun 21, 2012 at 7:34
  • 2
    Further note that this answer was written in 2008, at which time iTextSharp was released under the LGPL. Upon the release of version 5.0.0 (Dec. 2009, SVN revision 108; license change is at revision 99), the license was changed to AGPL, requiring Application Service Providers to either release source code or purchase a commercial license. The previous version (4.1.6; LGPL) is forked here and still has the above functionality. May 28, 2013 at 21:27
  • 1
    It's a shame that this encrypt is too easy to break. codeproject.com/Articles/31493/PDF-Security-Remover
    – Diego
    May 30, 2015 at 16:56
1

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

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:

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.

2
  • is this library free or not? Aug 25, 2022 at 8:51
  • @AkalankaEkanayake The library can be free in certain cases but in general case you woul need to pay for the license.
    – Bobrovsky
    Aug 25, 2022 at 17:22
0

If anyone is looking for a IText7 reference.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.