|
| 1 | +using Aspose.Pdf.Cloud.Sdk.Api; |
| 2 | +using Aspose.Pdf.Cloud.Sdk.Model; |
| 3 | +using Newtonsoft.Json; |
| 4 | + |
| 5 | +namespace ChangeLayout |
| 6 | +{ |
| 7 | + public class ConfigParams |
| 8 | + { |
| 9 | + public string CrdentialPath { get; } = "..\\credentials.json "; |
| 10 | + public string LOCAL_FOLDER { get; } = "C:\\Samples"; |
| 11 | + public string REMOTE_TEMP_FOLDER { get; } = "TempPdfCloud"; |
| 12 | + public string PDF_DOCUMENT { get; } = "sample.pdf"; |
| 13 | + public string PDF_OUTPUT { get; } = "output_sample.pdf"; |
| 14 | + |
| 15 | + public Rotation ROTATE_PAGES_ANGLE = Rotation.On90; |
| 16 | + public string ROTATE_PAGES { get; } = "1-3"; |
| 17 | + |
| 18 | + |
| 19 | + public string CROP_PAGE_TEMP_FILE { get; } = "sample_temp_file.png"; |
| 20 | + public string CROP_LOCAL_RESULT_DOCUMENT_NAME { get; } = "output_sample.pdf"; |
| 21 | + public int CROP_PAGE_NUMBER { get; } = 3; |
| 22 | + public int CROP_HEIGHT { get; } = 400; |
| 23 | + public int CROP_WIDTH { get; } = 300; |
| 24 | + public int CROP_LLX { get; } = 100; |
| 25 | + public int CROP_LLY { get; } = 200; |
| 26 | + |
| 27 | + public string RESIZE_PDF_HTML_FILE { get; } = "sample_temp_file.html"; |
| 28 | + public string RESIZE_RESULT_DOCUMENT_NAME { get; } = "output_sample.pdf"; |
| 29 | + public int RESIZE_PAGE_NUMBER { get; } = 2; |
| 30 | + public int RESIZE_NEW_PAGE_WIDTH { get; } = 1000; |
| 31 | + public int RESIZE_NEW_PAGE_HEIGHT { get; } = 500; |
| 32 | + |
| 33 | + public int CROP_PAGE_WIDTH { get; set; } = 0; |
| 34 | + public int CROP_PAGE_HEIGHT { get; set; } = 0; |
| 35 | + } |
| 36 | + |
| 37 | + public class Credentials |
| 38 | + { |
| 39 | + public string Id { get; set; } |
| 40 | + public string Key { get; set; } |
| 41 | + } |
| 42 | + |
| 43 | + public class ChangeLayoutHelper |
| 44 | + { |
| 45 | + public PdfApi pdfApi { get; private set; } |
| 46 | + public ConfigParams config { get; private set; } |
| 47 | + |
| 48 | + public ChangeLayoutHelper() |
| 49 | + { |
| 50 | + config = new ConfigParams(); |
| 51 | + string jsCredText = File.ReadAllText(config.CrdentialPath); |
| 52 | + Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText); |
| 53 | + pdfApi = new PdfApi(cred.Key, cred.Id); |
| 54 | + } |
| 55 | + |
| 56 | + public async Task UploadFile(string fileName) |
| 57 | + { |
| 58 | + using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName))) |
| 59 | + { |
| 60 | + FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file); |
| 61 | + if (response == null) |
| 62 | + Console.WriteLine("UploadFile(): Unexpected error - no response!"); |
| 63 | + else if (response.Errors != null && response.Errors.Count > 0) |
| 64 | + foreach (var error in response.Errors) |
| 65 | + Console.WriteLine("UploadFile(): {0} -> {1}", [ error.Code, error.Message]); |
| 66 | + else |
| 67 | + Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public async Task DownloadFile(string fileName, string outputPrefix) |
| 72 | + { |
| 73 | + Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName)); |
| 74 | + using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + fileName)); |
| 75 | + stream.Position = 0; |
| 76 | + await stream.CopyToAsync(fileStream); |
| 77 | + Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + fileName); |
| 78 | + } |
| 79 | + |
| 80 | + public async Task GetPageInfo(string document, int pageNumber) |
| 81 | + { |
| 82 | + DocumentPageResponse response = await pdfApi.GetPageAsync(document, pageNumber, folder: config.REMOTE_TEMP_FOLDER); |
| 83 | + if (response == null) |
| 84 | + Console.WriteLine("GetPageInfo(): Unexpected error - no response!"); |
| 85 | + else if (response.Code != 200) |
| 86 | + Console.WriteLine("GetPageInfo(): Code {0} -> Status '{1}'", [response.Code, response.Status]); |
| 87 | + else |
| 88 | + { |
| 89 | + Page page = response.Page; |
| 90 | + Rectangle rectangle = page.Rectangle; |
| 91 | + Console.WriteLine("GetPageInfo(): page {0} => id: '{1}', lLx: '{2}', lLY: '{3}', uRX: '{4}', uRY: '{5}'", |
| 92 | + [pageNumber, page.Id, rectangle.LLX, rectangle.LLY, rectangle.URX, rectangle.URY]); |
| 93 | + config.CROP_PAGE_HEIGHT = (int)(Math.Round(rectangle.URY ?? 0) - Math.Round(rectangle.LLY ?? 0)); |
| 94 | + config.CROP_PAGE_WIDTH = (int)(Math.Round(rectangle.URX ?? 0) - Math.Round(rectangle.LLX ?? 0)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public async Task<string> ExtractPdfPage(string document, int pageNumber, int width, int height) |
| 99 | + { |
| 100 | + string imageFile = document + ".png"; |
| 101 | + Stream stream = await pdfApi.GetPageConvertToPngAsync(document, pageNumber, width, height, config.REMOTE_TEMP_FOLDER); |
| 102 | + using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, imageFile)); |
| 103 | + stream.Position = 0; |
| 104 | + await stream.CopyToAsync(fileStream); |
| 105 | + |
| 106 | + UploadFile(imageFile); |
| 107 | + Console.WriteLine("ExtractPdfPage(): Page #{0} extracted as image {1}.", [pageNumber, imageFile]); |
| 108 | + |
| 109 | + return imageFile; |
| 110 | + } |
| 111 | + |
| 112 | + public async Task<DocumentResponse?> CreatePdfDocument(string newDocument, int width, int height) |
| 113 | + { |
| 114 | + DocumentProperties docProps = new DocumentProperties( |
| 115 | + List: new List<DocumentProperty>() { |
| 116 | + new DocumentProperty(Name: "prop1", Value: "Value1", BuiltIn: false) |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + DisplayProperties dispProps = new DisplayProperties() |
| 121 | + { |
| 122 | + CenterWindow = true, |
| 123 | + HideMenuBar = true, |
| 124 | + Direction = Direction.L2R, |
| 125 | + DisplayDocTitle = true, |
| 126 | + HideToolBar = true, |
| 127 | + HideWindowUI = true, |
| 128 | + NonFullScreenPageMode = PageMode.UseThumbs, |
| 129 | + PageLayout = PageLayout.TwoPageLeft, |
| 130 | + PageMode = PageMode.UseThumbs |
| 131 | + }; |
| 132 | + |
| 133 | + DefaultPageConfig pageConfig = new DefaultPageConfig(height, width); |
| 134 | + |
| 135 | + DocumentConfig document_config = new DocumentConfig( |
| 136 | + DocumentProperties: docProps, |
| 137 | + DisplayProperties: dispProps, |
| 138 | + DefaultPageConfig: pageConfig, |
| 139 | + PagesCount: 1 |
| 140 | + ); |
| 141 | + |
| 142 | + DocumentResponse response = await pdfApi.PostCreateDocumentAsync(newDocument, document_config, folder: config.REMOTE_TEMP_FOLDER); |
| 143 | + |
| 144 | + if (response != null && response.Code == 200) |
| 145 | + { |
| 146 | + Console.WriteLine("CreatePdfDocument(): Document '{0}' created.", newDocument); |
| 147 | + return response; |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + Console.WriteLine("CreatePdfDocument(): Unexpected error!!!"); |
| 152 | + return null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public async Task<AsposeResponse?> InsertPageAsImage(string document, string imageFileValue, int llx, int lly) |
| 157 | + { |
| 158 | + ImageStamp imageStamp = new ImageStamp( |
| 159 | + Background: true, |
| 160 | + HorizontalAlignment: HorizontalAlignment.None, |
| 161 | + VerticalAlignment: VerticalAlignment.None, |
| 162 | + Rotate: Rotation.None, |
| 163 | + RotateAngle: 0, |
| 164 | + Opacity: 1, |
| 165 | + XIndent: -llx, |
| 166 | + YIndent: -lly, |
| 167 | + Zoom: 1, |
| 168 | + FileName: Path.Combine(config.REMOTE_TEMP_FOLDER, imageFileValue) |
| 169 | + ); |
| 170 | + |
| 171 | + AsposeResponse response = await pdfApi.PostPageImageStampsAsync(document, 1, new List<ImageStamp>() { imageStamp}, folder: config.REMOTE_TEMP_FOLDER ); |
| 172 | + if (response != null && response.Code == 200) |
| 173 | + { |
| 174 | + Console.WriteLine("InsertPageAsImage(): Document '{0}' with stamp '{1}' created.", [document, imageFileValue]); |
| 175 | + return response; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + Console.WriteLine("CreatePdfDocument(): Unexpected error!!!"); |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments