1+ using Aspose . Pdf . Cloud . Sdk . Api ;
2+ using Aspose . Pdf . Cloud . Sdk . Model ;
3+ using Newtonsoft . Json ;
4+
5+ namespace Bookmarks
6+ {
7+ public class ConfigParams
8+ {
9+ public string CrdentialPath { get ; } = "c:\\ Projects\\ ASPOSE\\ Pdf.Cloud\\ Credentials\\ 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 string BOOKMARK_TITLE { get ; } = "NEW Bookmark Title XYZ" ;
16+ public string BOOKMARK_PATH { get ; } = "/1" ;
17+ }
18+
19+ public class Credentials
20+ {
21+ public string Id { get ; set ; }
22+ public string Key { get ; set ; }
23+ }
24+
25+ public class BookmarksHelper
26+ {
27+ public PdfApi pdfApi { get ; private set ; }
28+ public ConfigParams config { get ; private set ; }
29+
30+ public BookmarksHelper ( )
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+ }
0 commit comments