views:

2397

answers:

2

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.

+4  A: 

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);
}
Darin Dimitrov
A: 

Hi, i m using PDFStanper to add pdf pages to a new created pdf, but if some pdf are password protected, pdfstamper is giving error "PdfReader not opened with owner password", will u pls help me on this

Anil