| Security has always been the most important issue in every field either it's about the protection of a network or a PDF document. Documents are made secure for many possible reasons like the writer of the document may like to keep the content of the document safe and doesn't want to allow others to change it etc. |
Aspose.Pdf for .NET has taken much care of such security aspects by providing such features to developers that can be useful for them to protect their PDF documents. Aspose.Pdf for .NET provides Security class that contains all certain properties and methods that allow developers to apply several security measures regarding PDF documents.
One of these security measures is to password protect the PDF document during encryption. Security class offers two properties: MasterPassword and UserPassword that can be used to set master or user password for the PDF document during encryption. Both properties take the password in string format.
Code Snippet
C#
//Instantiate Pdf instance by calling its empty constructor Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //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"; //Add a section in the Pdf Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add(); //Create a text paragraph Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text(sec1,"this is text content"); //Set the top maring of text paragraph to 30 text1.Margin.Top = 30; //Add the text paragraph to the section sec1.Paragraphs.Add(text1); //Save the Pdf pdf1.Save(...);
VB.NET
'Instantiate Pdf instance by calling its empty constructor Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf() '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" 'Add a section in the Pdf Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() 'Create a text paragraph Dim text1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(sec1, "this is text content") 'Set the top maring of text paragraph to 30 text1.Margin.Top = 30 'Add the text paragraph to the section sec1.Paragraphs.Add(text1) 'Save the Pdf pdf1.Save(...)
XML
<?xml version="1.0" encoding="utf-8" ?> <Pdf xmlns="Aspose.Pdf" UserPassword="user" MasterPassword="master"> <Section> <Text MarginTop="30"> <Segment>this is text content</Segment> </Text> </Section> </Pdf>
