Access PDF security settings

Security
7/17/2015

This code sample shows how you can view the security settings of a PDF document.

C# code sample

1 //read document with security settings 2 using (FileStream inFile = new FileStream( @"..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read )) 3 { 4 // open the source document 5 Document document = new Document( inFile ); 6 7 // change fields here... (you can also set Widget.Persistency = WidgetPersistency.Flatten if you want to flatten the document) 8 9 // preserve existing security settings 10 // all settings like: Print, CopyExtract, Strong and passwords are preserved automatically 11 12 // display settings, note that the passwords will not be available. 13 PasswordSecurity passwordSecurity = document.Security as PasswordSecurity; 14 15 Console.WriteLine( "Assembly: {0}", passwordSecurity.Assembly ); 16 Console.WriteLine( "AuthorCommentsFormFields: {0}", passwordSecurity.AuthorCommentsFormFields ); 17 Console.WriteLine( "Change: {0}", passwordSecurity.Change ); 18 Console.WriteLine( "CopyExtract: {0}", passwordSecurity.CopyExtract ); 19 Console.WriteLine( "ExtractAccessibilityContents: {0}", passwordSecurity.ExtractAccessibilityContents ); 20 Console.WriteLine( "FormFillSign: {0}", passwordSecurity.FormFillSign ); 21 Console.WriteLine( "Print: {0}", passwordSecurity.Print ); 22 Console.WriteLine( "PrintHighQuality: {0}", passwordSecurity.PrintHighQuality ); 23 Console.WriteLine("EncryptionLevel: {0}", passwordSecurity.EncryptionLevel); 24 25 //include to leave the command-prompt open: Console.Read(); 26 27 // write the modified document to disk 28 using (FileStream outFile = new FileStream( @"..\..\preservesecuritysettings.pdf", FileMode.Create, FileAccess.Write )) 29 { 30 document.Write( outFile ); 31 } 32 }

VB.NET code sample

1 'read document with security settings 2 Using inFile As New FileStream("..\..\..\inputDocuments\PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read) 3 ' open the source document 4 Dim document As New Document(inFile) 5 6 ' change fields here... (you can also set Widget.Persistency = WidgetPersistency.Flatten if you want to flatten the document) 7 8 ' preserve existing security settings 9 ' all settings like: Print, CopyExtract, Strong and passwords are preserved automatically 10 11 ' display settings, note that the passwords will not be available. 12 Dim passwordSecurity As PasswordSecurity = TryCast(document.Security, PasswordSecurity) 13 14 Console.WriteLine("Assembly: {0}", passwordSecurity.Assembly) 15 Console.WriteLine("AuthorCommentsFormFields: {0}", passwordSecurity.AuthorCommentsFormFields) 16 Console.WriteLine("Change: {0}", passwordSecurity.Change) 17 Console.WriteLine("CopyExtract: {0}", passwordSecurity.CopyExtract) 18 Console.WriteLine("ExtractAccessibilityContents: {0}", passwordSecurity.ExtractAccessibilityContents) 19 Console.WriteLine("FormFillSign: {0}", passwordSecurity.FormFillSign) 20 Console.WriteLine("Print: {0}", passwordSecurity.Print) 21 Console.WriteLine("PrintHighQuality: {0}", passwordSecurity.PrintHighQuality) 22 Console.WriteLine("EncryptionLevel: {0}", passwordSecurity.EncryptionLevel) 23 24 'include to leave the command-prompt open: Console.Read(); 25 26 ' write the modified document to disk 27 Using outFile As New FileStream("..\..\preservesecuritysettings.pdf", FileMode.Create, FileAccess.Write) 28 document.Write(outFile) 29 End Using 30 End Using