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 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 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
Pdf pdf1 = new Pdf();
//Assign a security instance to Pdf object
pdf1.Security = new 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
Section sec1 = pdf1.Sections.Add();
//Create a text paragraph
Text text1 = new 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 Pdf = New Pdf()
'Assign a security instance to Pdf object
pdf1.Security = New 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 Section = pdf1.Sections.Add()
'Create a text paragraph
Dim text1 As Text = New 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(...)
[JAVA]
//Instantiate Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Assign a security instance to Pdf object
pdf1.setSecurity( new Security() );
//Set the master password for the PDF document
pdf1.getSecurity().setMasterPass("master");
//Set the user password for the PDF document
pdf1.getSecurity().setUserPass("user");
//Add a section in the Pdf
Section sec1 = pdf1.getSections().add();
//Create a text paragraph
Text text1 = new Text(sec1,"this is text content");
//Set the top maring of text paragraph to 30
text1.getMargin().setTop(30);
//Add the text paragraph to the section
sec1.getParagraphs().add(text1);
//Save the Pdf
FileOutputStream fileOut = new FileOutputStream(new File(...));
pdf1.save(fileOut);
[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>