//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.Security; namespaceDsPdfWeb.Demos{// This sample demonstrates the use of Security.EncryptHandler and Security.DecryptHandler.// Security.DecryptHandler allows you to examine the security attributes of an existing PDF.// Security.EncryptHandler allows you to specify security attributes when saving a PDF.// DsPdf supports Standard Security Handlers revisions 2, 3, 4, 5 and 6 as defined// in the PDF Spec 1.7 and 2.0. In this sample, we use StandardSecurityHandlerRev4.// // See SecurityHandlerRev6 for a Standard Security Handler revision 6 demo.publicclassSecurityHandlers {publicintCreatePDF(Stream stream) {// Sample passwords:conststringownerPassword = "I'm the owner";conststringuserPassword = "I'm a user"; // Step 1: Generate a document with some security attributes:var doc0 = newGcPdfDocument();var rc0 = Common.Util.AddNote("Demonstrating security:\n" +"In this PDF, we specify certain encryption options,\n" +"and set owner and user passwords.", doc0.NewPage()); // Create a Rev4 security handler:var ssh4 = newStandardSecurityHandlerRev4() {// Set some rev4 specific props:EncryptionAlgorithm = EncryptionAlgorithm.AES,EncryptStrings = true, };// StandardSecurityHandlerRev4 is derived from StandardSecurityHandlerRev3,// so we can do this to make sure we touch rev3-specific properties only// (the cast is for illustration only, you do not need it to set those props of course):if (ssh4 isStandardSecurityHandlerRev3 ssh3) { ssh3.EditingPermissions = EditingPermissions.AssembleDocument; ssh3.PrintingPermissions = PrintingPermissions.LowResolution; }// But StandardSecurityHandlerRev3 is NOT derived from StandardSecurityHandlerRev2,// because some properties have similar meanings but different syntax, so this:// if (ssh3 is StandardSecurityHandlerRev2 ssh2) { ... }// will NOT work. // Set some passwords: ssh4.OwnerPassword = ownerPassword; ssh4.UserPassword = userPassword; // Assign the handler we created to the document so that it is used when saving the PDF: doc0.Security.EncryptHandler = ssh4; // Save the PDF in a temp file, so that we can load it:var fn = Path.GetTempFileName(); doc0.Save(fn); // Step 2: Load the generated PDf and examine its security attributes:var doc = newGcPdfDocument();using (var fs = File.OpenRead(fn)) {// User password is needed to load the document: doc.Load(fs, userPassword); // At this point we can examine doc.Security.DecryptHandler if it exists,// but there is NO Security.EncryptHandler:if (doc.Security.EncryptHandler != null)thrownewException("This should not happen."); var dh = doc.Security.DecryptHandler;if (dh isStandardSecurityHandlerRev4 dh_ssh4) {// Make sure the loaded permissions are what we specified in Step 1:Common.Util.AddNote(string.Format("Security attributes that were in the loaded PDF's DecryptHandler:\n" +"EditingPermissions: {0}\n" +"PrintingPermissions: {1}", dh_ssh4.EditingPermissions, dh_ssh4.PrintingPermissions), doc.Pages[0],newRectangleF(72, rc0.Bottom + 36, 72 * 6, 72 * 2));// This won't work, sorry:var noway = dh_ssh4.OwnerPassword;if (noway != null)thrownewException("No way."); }elseif (dh isStandardSecurityHandlerRev3 dh_ssh3) {// If we didn't know that we have a Rev4 handler, we would add code here, }elseif (dh isStandardSecurityHandlerRev2 dh_ssh2) {// ... and here, }else {// ... and done something in this case too. } // Save the new PDF - but PLEASE NOTE that because we did not set// the Security.EncryptHandler, the newly saved document has no security! doc.Save(stream); }// Clean up the temp file:File.Delete(fn);// Done:return doc.Pages.Count; } }}