Free Support Forum - aspose.com

About Encrypting pdf File

I am creating pdf on fly and adding header/footer , Stamp. At last encrypting (with password) the file.

But when I try to open it, it is not password protected

Is that not supported or I am doing it wrong way?

My sample code :


Document PdfDocument;

PdfFileSecurity fileSecurity;

PdfFileStamp fileStamp

PdfDocument = new Document();

PdfDocument.Pages.Add(); // Adding empty page.

// Adding Header

TextStamp textStamp = new TextStamp(“Test”);

textStamp.TopMargin = (double)10;

textStamp.VerticalAlignment = VerticalAlignment.Top;

page.AddStamp(textStamp);

fileStamp.AddStamp(stamp);

//Adding Stamp

fileStamp = new PdfFileStamp(PdfDocument);

Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();

string stampText = “My Stamp”;

stamp.BindLogo(new FormattedText(stampText, forColors, backColors, Aspose.Pdf.Facades.FontStyle.Helvetica, EncodingType.Winansi, true, size));

// Encrypting with password

DocumentPrivilege privilege = DocumentPrivilege.ForbidAll

string userpassword = “abcd”;

string masterpassword= “”;

fileSecurity = new PdfFileSecurity(PdfDocument);

fileSecurity.EncryptFile(userpassword, masterpassword, privilege, KeySize.x256, Algorithm.AES);

//Saving

PdfDocument.Save(@“C:\Users\ankur.patel\Desktop\Encrypt.pdf”);


//Disposing

if (fileSecurity != null){ fileSecurity.Dispose();}

if (fileStamp != null){ fileStamp.Dispose();}

if (PdfDocument != null){ PdfDocument.Dispose(); }

Regards,

Ankur


Hi Ankur,


Thanks for using our products.

The reason password protection is not being applied to PDF file is because PdfFileSecurity object accepts an existing PDF file as an argument and in your code snippet, PdfDocument object is being saved after applying security restrictions. In case you need to create a PDF file containing Header, watermark and open password protection, you may simply try using Aspose.Pdf.Generator namespace. Please try using the following code snippet. For your reference, I have also attached the resultant PDF file generated over my end.

In the event of any further query, please feel free to contact. We are sorry for this inconvenience.

[C#]

Aspose.Pdf.Generator.Pdf
pdf1 = new Pdf();<o:p></o:p>

//Create a Section object by calling Add method of Sections collection of Pdf class

Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

//Instantiate HeaderFooter object and pass the Section reference in which

//the header or footer is to be added

Aspose.Pdf.Generator.HeaderFooter hf1 = new Aspose.Pdf.Generator.HeaderFooter(sec1);

//Set the header of odd pages of the PDF document

sec1.OddHeader = hf1;

//Set the header of even pages of the PDF document

sec1.EvenHeader = hf1;

//Instantiate a Text paragraph that will store the content to show as header

Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(hf1, "header");

//Add the text object to the Paragraphs collection of HeaderFooter object to

//display header on the pages of PDF document

hf1.Paragraphs.Add(text);

//text watermark

Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text("Text Watermark");

Aspose.Pdf.Generator.FloatingBox watermark1 = new Aspose.Pdf.Generator.FloatingBox(108, 80);

watermark1.Left = 50;

watermark1.Top = 500;

watermark1.Paragraphs.Add(text1);

// add watermark to watermarks collection of PDf file

pdf1.Watermarks.Add(watermark1);

//Assign a security instance to Pdf object

pdf1.Security = new Aspose.Pdf.Generator.Security();

//Set the master password for the PDF document

pdf1.Security.MasterPassword = "master";

//Set the user password for the PDF document

pdf1.Security.UserPassword = "user";

// save the PDF document

pdf1.Save(“c:/pdftest/ProtectedPDFfile.pdf”);