Sample C# code for using Apryse SDK to read encrypted (password protected) documents, secure a document with encryption, or remove encryption. Learn more about our UWP SDK.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.Collections.Generic;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.IO;
11using Windows.Foundation;
12
13using pdftron.PDF;
14using pdftron.SDF;
15
16using PDFNetUniversalSamples.ViewModels;
17
18namespace PDFNetSamples
19{
20 public sealed class EncTestCS : Sample
21 {
22 public EncTestCS() :
23 base("Encryption Test", "Illustrates how to add password protection to a document and how to open it again.")
24 {
25 }
26
27 public override IAsyncAction RunAsync()
28 {
29 return Task.Run(new System.Action(async () =>
30 {
31 WriteLine("--------------------------------");
32 WriteLine("Starting Encryption Test...");
33 WriteLine("--------------------------------\n");
34
35 // Example 1: Securing a document with password protection and adjusting permissions
36 // on the document.
37 try
38 {
39 // Open the test file
40 WriteLine("-------------------------------------------------");
41 WriteLine("Securing an existing document...");
42
43 String input_file_path = Path.Combine(InputPath, "fish.pdf");
44 PDFDoc doc = new PDFDoc(input_file_path);
45
46 if (!doc.InitSecurityHandler())
47 {
48 WriteLine("Document authentication error...");
49 return;
50 }
51
52
53 SecurityHandler new_handler = new SecurityHandler();
54
55 string my_password = "test";
56 new_handler.ChangeUserPassword(my_password);
57
58 new_handler.SetPermission(SecurityHandlerPermission.e_print, true);
59 new_handler.SetPermission(SecurityHandlerPermission.e_extract_content, false);
60
61 doc.SetSecurityHandler(new_handler);
62
63 WriteLine(string.Format("\nTo open document use password: {0}\n", my_password));
64
65 String output_file_path = Path.Combine(OutputPath, "secured.pdf");
66 await doc.SaveAsync(output_file_path, 0);
67
68 WriteLine("Done. Result saved in " + output_file_path);
69 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
70
71 }
72 catch (Exception e)
73 {
74 WriteLine(GetExceptionMessage(e));
75 }
76
77
78 // Example 2: Reading password protected document without user feedback.
79 try
80 {
81 WriteLine("-------------------------------------------------");
82 WriteLine("Open the password protected document from the first example...");
83
84 String input_file_path = Path.Combine(OutputPath, "secured.pdf");
85 PDFDoc doc = new PDFDoc(input_file_path); // Open the encrypted document that we saved in the first example.
86
87 if (!doc.InitStdSecurityHandler("test"))
88 {
89 WriteLine("Document authentication error...");
90 WriteLine("The password is not valid.");
91 return;
92 }
93 else
94 {
95 WriteLine("The password is correct! Document can now be used for reading and editing");
96
97 // Remove the password security and save the changes to a new file.
98 doc.SetSecurityHandler(null);
99
100 String output_file_path = Path.Combine(OutputPath, "secured_nomore1.pdf");
101 await doc.SaveAsync(output_file_path, 0);
102
103 WriteLine("Done. Result saved in " + output_file_path);
104 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
105
106 WriteLine("Done. Result saved in secured_nomore1.pdf");
107
108
109 }
110 }
111 catch (Exception e)
112 {
113 WriteLine(GetExceptionMessage(e));
114 }
115
116 WriteLine("\n--------------------------------");
117 WriteLine("To see how to open a document with user feedback, see the PDFViewCtrlDemo and the Open and HandlePassword functions.");
118 WriteLine("--------------------------------\n");
119
120 WriteLine("\n--------------------------------");
121 WriteLine("Done Encryption Test.");
122 WriteLine("--------------------------------\n");
123 })).AsAsyncAction();
124 }
125 }
126}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales