1
+ // 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2
+ // 2. Create an object to connect to the Pdf.Cloud API
3
+ // 3. Upload your document file
4
+ // 4. Create a new XmpMetadataProperty with Your key and null as value for delete XmpMetadataProperty
5
+ // 5. Delete XMpMetadataProperty in the document using postXmpMetadata() function
6
+ // 6. Perform some action after successful addition
7
+ // All values of variables starting with "YOUR_****" should be replaced by real user values
8
+
9
+ import credentials from "../../../../Credentials/credentials.json" with { type : "json" } ;
10
+ import fs from 'node:fs/promises' ;
11
+ import path from 'node:path' ;
12
+ import { PdfApi } from "../../../src/api/api.js" ;
13
+ import { XmpMetadata } from "../../../src/models/xmpMetadata.js" ;
14
+ import { XmpMetadataProperty } from "../../../src/models/xmpMetadataProperty.js" ;
15
+
16
+ const configParams = {
17
+ LOCAL_FOLDER : "C:\\Samples\\" ,
18
+ PDF_DOCUMENT_NAME : "sample.pdf" ,
19
+ LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
20
+ NEW_LINK_ACTION : "https://reference.aspose.cloud/pdf/#/" ,
21
+ PAGE_NUMBER : 2 , // Your document page number...
22
+ LINK_POS_LLX : 244.914 ,
23
+ LINK_POS_LLY : 488.622 ,
24
+ LINK_POS_URX : 284.776 ,
25
+ LINK_POS_URY : 498.588 ,
26
+ } ;
27
+
28
+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
29
+
30
+ const pdfMetadatas = {
31
+ async uploadDocument ( ) {
32
+ const pdfFilePath = path . join ( configParams . LOCAL_FOLDER , configParams . PDF_DOCUMENT_NAME ) ;
33
+ const pdfFileData = await fs . readFile ( pdfFilePath ) ;
34
+ await pdfApi . uploadFile ( configParams . PDF_DOCUMENT_NAME , pdfFileData ) ;
35
+ } ,
36
+
37
+ async downloadResult ( ) {
38
+ const changedPdfData = await pdfApi . downloadFile ( configParams . PDF_DOCUMENT_NAME ) ;
39
+ const filePath = path . join ( configParams . LOCAL_FOLDER , configParams . LOCAL_RESULT_DOCUMENT_NAME ) ;
40
+ await fs . writeFile ( filePath , changedPdfData . body ) ;
41
+ console . log ( "Downloaded: " + filePath ) ;
42
+ } ,
43
+
44
+ async getMetadata ( ) {
45
+ const responseMetadata = await pdfApi . getXmpMetadataJson ( configParams . PDF_DOCUMENT_NAME ) ;
46
+
47
+ if ( responseMetadata . response . status == 200 )
48
+ {
49
+ const props = responseMetadata . body . properties ;
50
+ for ( var i = 0 ; i < props . length ; i ++ )
51
+ {
52
+ console . log ( props [ i ] ) ;
53
+ }
54
+ }
55
+ } ,
56
+
57
+ async deleteMetadata ( ) {
58
+
59
+ const prop = new XmpMetadataProperty ( ) ;
60
+ prop . key = 'dc:creator' ;
61
+ prop . value = null ;
62
+ prop . namespaceUri = 'http://purl.org/dc/elements/1.1/' ;
63
+
64
+ const metadata = new XmpMetadata ( ) ;
65
+ metadata . properties = [ prop ] ;
66
+
67
+ const response = await pdfApi . postXmpMetadata ( configParams . PDF_DOCUMENT_NAME , metadata ) ;
68
+
69
+ if ( response . body . code == 200 ) {
70
+ console . log ( "Dlete metadata '" + prop . key + "' successful!" ) ;
71
+ return true ;
72
+ }
73
+ } ,
74
+
75
+ }
76
+
77
+ async function main ( ) {
78
+ try {
79
+ await pdfMetadatas . uploadDocument ( ) ;
80
+ await pdfMetadatas . getMetadata ( ) ;
81
+ await pdfMetadatas . deleteMetadata ( ) ;
82
+ await pdfMetadatas . getMetadata ( ) ;
83
+ await pdfMetadatas . downloadResult ( ) ;
84
+ } catch ( error ) {
85
+ console . error ( "Error:" , error . message ) ;
86
+ }
87
+ }
88
+
89
+ main ( ) ;
0 commit comments