|
| 1 | +/* |
| 2 | + * What this sample does: |
| 3 | + * - Decrypts a PDF, adds an image, then re-encrypts it. |
| 4 | + * - Routed from Program.cs as: `dotnet run -- decrypt-add-reencrypt <pdf> <image> [password]`. |
| 5 | + * |
| 6 | + * Setup (environment): |
| 7 | + * - Copy .env.example to .env |
| 8 | + * - Set PDFREST_API_KEY=your_api_key_here |
| 9 | + * - Optional: set PDFREST_URL to override the API region. For EU/GDPR compliance and proximity, use: |
| 10 | + * PDFREST_URL=https://eu-api.pdfrest.com |
| 11 | + * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work |
| 12 | + * |
| 13 | + * Usage: |
| 14 | + * dotnet run -- decrypt-add-reencrypt input.pdf image.png secret |
| 15 | + * |
| 16 | + * Output: |
| 17 | + * - Prints JSON responses for decrypt, add-image, and re-encrypt stages. |
| 18 | + */ |
| 19 | + |
1 | 20 | using Newtonsoft.Json.Linq; |
2 | 21 | using System.Text; |
3 | 22 |
|
4 | | -/* In this sample, we will show how to take an encrypted file and decrypt, modify |
5 | | -* and re-encrypt it to create an encryption-at-rest solution as discussed in |
6 | | -* https://pdfrest.com/solutions/create-secure-document-workflows-with-pdf-password-protection/ |
7 | | -* We will be running the document through /decrypted-pdf to open the document |
8 | | -* to modification, running the decrypted result through /pdf-with-added-image, |
9 | | -* and then sending the output with the new image through /encrypted-pdf to |
10 | | -* lock it up again. |
11 | | -*/ |
12 | | - |
13 | | -var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here |
14 | | - |
15 | | -using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) |
| 23 | +namespace Samples.ComplexFlowExamples |
16 | 24 | { |
17 | | - // Begin decryption |
18 | | - using var decryptRequest = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf"); |
19 | | - |
20 | | - decryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
21 | | - decryptRequest.Headers.Accept.Add(new("application/json")); |
22 | | - var decryptMultipartContent = new MultipartFormDataContent(); |
23 | | - |
24 | | - var byteArray = File.ReadAllBytes("/path/to/file.pdf"); |
25 | | - var byteAryContent = new ByteArrayContent(byteArray); |
26 | | - decryptMultipartContent.Add(byteAryContent, "file", "file.pdf"); |
27 | | - byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); |
28 | | - |
29 | | - var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password")); |
30 | | - decryptMultipartContent.Add(byteArrayOption, "current_open_password"); |
31 | | - |
32 | | - |
33 | | - decryptRequest.Content = decryptMultipartContent; |
34 | | - var decryptResponse = await httpClient.SendAsync(decryptRequest); |
35 | | - |
36 | | - var decryptResult = await decryptResponse.Content.ReadAsStringAsync(); |
37 | | - |
38 | | - Console.WriteLine("Decrypt response received."); |
39 | | - Console.WriteLine(decryptResult); |
40 | | - |
41 | | - dynamic decryptJson = JObject.Parse(decryptResult); |
42 | | - string decryptID = decryptJson.outputId; |
43 | | - |
44 | | - // Begin add image |
45 | | - using var addImageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-added-image"); |
46 | | - |
47 | | - addImageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
48 | | - addImageRequest.Headers.Accept.Add(new("application/json")); |
49 | | - var addImageMultipartContent = new MultipartFormDataContent(); |
50 | | - |
51 | | - var addImageId = new ByteArrayContent(Encoding.UTF8.GetBytes(decryptID)); |
52 | | - addImageMultipartContent.Add(addImageId, "id"); |
53 | | - |
54 | | - var addImageImage = File.ReadAllBytes("/path/to/file.png"); |
55 | | - var imageContent = new ByteArrayContent(addImageImage); |
56 | | - addImageMultipartContent.Add(imageContent, "image_file", "file_name.png"); |
57 | | - imageContent.Headers.TryAddWithoutValidation("Content-Type", "image/png"); |
58 | | - |
59 | | - var addImagePage = new ByteArrayContent(Encoding.UTF8.GetBytes("1")); |
60 | | - addImageMultipartContent.Add(addImagePage, "page"); |
61 | | - |
62 | | - var addImageX = new ByteArrayContent(Encoding.UTF8.GetBytes("0")); |
63 | | - addImageMultipartContent.Add(addImageX, "x"); |
64 | | - var addImageY = new ByteArrayContent(Encoding.UTF8.GetBytes("0")); |
65 | | - addImageMultipartContent.Add(addImageY, "y"); |
66 | | - |
67 | | - addImageRequest.Content = addImageMultipartContent; |
68 | | - var addImageResponse = await httpClient.SendAsync(addImageRequest); |
69 | | - |
70 | | - using var addImageResult = await addImageResponse.Content.ReadAsStringAsync(); |
71 | | - |
72 | | - Console.WriteLine("Add image response received."); |
73 | | - Console.WriteLine(addImageResult); |
74 | | - |
75 | | - dynamic addImageJson = JObject.Parse(addImageResult); |
76 | | - string addImageID = addImageJson.outputId; |
77 | | - |
78 | | - // Begin re-encryption |
79 | | - var encryptRequest = new HttpRequestMessage(HttpMethod.Post, "encrypted-pdf"); |
80 | | - |
81 | | - encryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
82 | | - encryptRequest.Headers.Accept.Add(new("application/json")); |
83 | | - var multipartContent = new MultipartFormDataContent(); |
84 | | - |
85 | | - var encryptID = new ByteArrayContent(Encoding.UTF8.GetBytes(addImageID)); |
86 | | - multipartContent.Add(encryptID, "id"); |
87 | | - |
88 | | - var encryptPassword = new ByteArrayContent(Encoding.UTF8.GetBytes("password")); |
89 | | - multipartContent.Add(encryptPassword, "new_open_password"); |
90 | | - |
91 | | - |
92 | | - encryptRequest.Content = multipartContent; |
93 | | - var response = await httpClient.SendAsync(encryptRequest); |
94 | | - |
95 | | - var apiResult = await response.Content.ReadAsStringAsync(); |
96 | | - |
97 | | - Console.WriteLine("Encrypt response received."); |
98 | | - Console.WriteLine(apiResult); |
| 25 | + public static class DecryptAddReencrypt |
| 26 | + { |
| 27 | + public static async Task Execute(string[] args) |
| 28 | + { |
| 29 | + if (args == null || args.Length < 2) |
| 30 | + { |
| 31 | + Console.Error.WriteLine("decrypt-add-reencrypt requires <pdf> <image> [password]"); |
| 32 | + Environment.Exit(1); |
| 33 | + return; |
| 34 | + } |
| 35 | + var pdfPath = args[0]; |
| 36 | + var imgPath = args[1]; |
| 37 | + var pwd = args.Length > 2 ? args[2] : "password"; |
| 38 | + if (!File.Exists(pdfPath) || !File.Exists(imgPath)) |
| 39 | + { |
| 40 | + Console.Error.WriteLine("One or more input files not found."); |
| 41 | + Environment.Exit(1); |
| 42 | + return; |
| 43 | + } |
| 44 | + var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY"); |
| 45 | + if (string.IsNullOrWhiteSpace(apiKey)) |
| 46 | + { |
| 47 | + Console.Error.WriteLine("Missing required environment variable: PDFREST_API_KEY"); |
| 48 | + Environment.Exit(1); |
| 49 | + return; |
| 50 | + } |
| 51 | + var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com"; |
| 52 | + |
| 53 | + using (var httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) }) |
| 54 | + { |
| 55 | + // Decrypt |
| 56 | + using var decryptRequest = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf"); |
| 57 | + decryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
| 58 | + decryptRequest.Headers.Accept.Add(new("application/json")); |
| 59 | + var decryptMultipartContent = new MultipartFormDataContent(); |
| 60 | + var byteArray = File.ReadAllBytes(pdfPath); |
| 61 | + var byteAryContent = new ByteArrayContent(byteArray); |
| 62 | + decryptMultipartContent.Add(byteAryContent, "file", Path.GetFileName(pdfPath)); |
| 63 | + byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream"); |
| 64 | + var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(pwd)); |
| 65 | + decryptMultipartContent.Add(byteArrayOption, "current_open_password"); |
| 66 | + decryptRequest.Content = decryptMultipartContent; |
| 67 | + var decryptResponse = await httpClient.SendAsync(decryptRequest); |
| 68 | + var decryptResult = await decryptResponse.Content.ReadAsStringAsync(); |
| 69 | + Console.WriteLine("Decrypt response received."); |
| 70 | + Console.WriteLine(decryptResult); |
| 71 | + dynamic decryptJson = JObject.Parse(decryptResult); |
| 72 | + string decryptID = decryptJson.outputId; |
| 73 | + |
| 74 | + // Add image |
| 75 | + using var addImageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-added-image"); |
| 76 | + addImageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
| 77 | + addImageRequest.Headers.Accept.Add(new("application/json")); |
| 78 | + var addImageMultipartContent = new MultipartFormDataContent(); |
| 79 | + var addImageId = new ByteArrayContent(Encoding.UTF8.GetBytes(decryptID)); |
| 80 | + addImageMultipartContent.Add(addImageId, "id"); |
| 81 | + var addImageImage = File.ReadAllBytes(imgPath); |
| 82 | + var imageContent = new ByteArrayContent(addImageImage); |
| 83 | + addImageMultipartContent.Add(imageContent, "image_file", Path.GetFileName(imgPath)); |
| 84 | + imageContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream"); |
| 85 | + var addImagePage = new ByteArrayContent(Encoding.UTF8.GetBytes("1")); |
| 86 | + addImageMultipartContent.Add(addImagePage, "page"); |
| 87 | + var addImageX = new ByteArrayContent(Encoding.UTF8.GetBytes("0")); |
| 88 | + addImageMultipartContent.Add(addImageX, "x"); |
| 89 | + var addImageY = new ByteArrayContent(Encoding.UTF8.GetBytes("0")); |
| 90 | + addImageMultipartContent.Add(addImageY, "y"); |
| 91 | + addImageRequest.Content = addImageMultipartContent; |
| 92 | + var addImageResponse = await httpClient.SendAsync(addImageRequest); |
| 93 | + var addImageResult = await addImageResponse.Content.ReadAsStringAsync(); |
| 94 | + Console.WriteLine("Add image response received."); |
| 95 | + Console.WriteLine(addImageResult); |
| 96 | + dynamic addImageJson = JObject.Parse(addImageResult); |
| 97 | + string addImageID = addImageJson.outputId; |
| 98 | + |
| 99 | + // Re-encrypt |
| 100 | + using var encryptRequest = new HttpRequestMessage(HttpMethod.Post, "encrypted-pdf"); |
| 101 | + encryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey); |
| 102 | + encryptRequest.Headers.Accept.Add(new("application/json")); |
| 103 | + var multipartContent = new MultipartFormDataContent(); |
| 104 | + var encryptID = new ByteArrayContent(Encoding.UTF8.GetBytes(addImageID)); |
| 105 | + multipartContent.Add(encryptID, "id"); |
| 106 | + var encryptPassword = new ByteArrayContent(Encoding.UTF8.GetBytes(pwd)); |
| 107 | + multipartContent.Add(encryptPassword, "new_open_password"); |
| 108 | + encryptRequest.Content = multipartContent; |
| 109 | + var response = await httpClient.SendAsync(encryptRequest); |
| 110 | + var apiResult = await response.Content.ReadAsStringAsync(); |
| 111 | + Console.WriteLine("Encrypt response received."); |
| 112 | + Console.WriteLine(apiResult); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
99 | 116 | } |
0 commit comments