|
| 1 | +using Aspose.Pdf.Cloud.Sdk.Api; |
| 2 | +using Aspose.Pdf.Cloud.Sdk.Model; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace EncryptDecrypt |
| 7 | +{ |
| 8 | + public class ConfigParams |
| 9 | + { |
| 10 | + public string CrdentialPath { get; } = "..\\credentials.json"; |
| 11 | + public string LOCAL_FOLDER { get; } = "C:\\Samples"; |
| 12 | + public string REMOTE_TEMP_FOLDER { get; } = "TempPdfCloud"; |
| 13 | + public string PDF_DOCUMENT { get; } = "sample.pdf"; |
| 14 | + public string PDF_DOCUMENT_ENCRYPTED { get; } = "sample_encrypted.pdf"; |
| 15 | + |
| 16 | + public string PDF_OUTPUT { get; } = "output_sample.pdf"; |
| 17 | + } |
| 18 | + |
| 19 | + public class Credentials |
| 20 | + { |
| 21 | + public string Id { get; set; } |
| 22 | + public string Key { get; set; } |
| 23 | + } |
| 24 | + |
| 25 | + public class EncryptDecryptHelper |
| 26 | + { |
| 27 | + public PdfApi pdfApi { get; private set; } |
| 28 | + public ConfigParams config { get; private set; } |
| 29 | + |
| 30 | + public EncryptDecryptHelper() |
| 31 | + { |
| 32 | + config = new ConfigParams(); |
| 33 | + string jsCredText = File.ReadAllText(config.CrdentialPath); |
| 34 | + Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText); |
| 35 | + pdfApi = new PdfApi(cred.Key, cred.Id); |
| 36 | + } |
| 37 | + |
| 38 | + public async Task UploadFile(string fileName) |
| 39 | + { |
| 40 | + using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName))) |
| 41 | + { |
| 42 | + FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file); |
| 43 | + if (response == null) |
| 44 | + Console.WriteLine("UploadFile(): Unexpected error - no response!"); |
| 45 | + else if (response.Errors != null && response.Errors.Count > 0) |
| 46 | + foreach (var error in response.Errors) |
| 47 | + Console.WriteLine("UploadFile(): {0} -> {1}", [error.Code, error.Message]); |
| 48 | + else |
| 49 | + Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public async Task DownloadFile(string fileName, string outputName, string outputPrefix) |
| 54 | + { |
| 55 | + Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName)); |
| 56 | + using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + outputName)); |
| 57 | + stream.Position = 0; |
| 58 | + await stream.CopyToAsync(fileStream); |
| 59 | + Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + outputName); |
| 60 | + } |
| 61 | + |
| 62 | + public string ToBase64(string str) |
| 63 | + { |
| 64 | + var bytes = Encoding.UTF8.GetBytes(str); |
| 65 | + return Convert.ToBase64String(bytes); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments