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 Link Annotation with the required properties
5
+ // 5. Append new Link Annotation to the document using postPageLinkAnnotations() 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 { Stamp } from "../../../src/models/stamp.js" ;
14
+
15
+ const configParams = {
16
+ LOCAL_FOLDER : "C:\\Samples\\" ,
17
+ PDF_DOCUMENT_NAME : "sample.pdf" ,
18
+ IMAGE_STAMP_FILE : "sample.png" ,
19
+ LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
20
+ PAGE_NUMBER : 2 , // Your document page number...
21
+ IMAGE_STAMP_LLY : 800 ,
22
+ IMAGE_STAMP_WIDTH : 24 ,
23
+ IMAGE_STAMP_HEIGHT : 24 ,
24
+ } ;
25
+
26
+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
27
+
28
+ const pdfStamps = {
29
+ async uploadFile ( fileName ) {
30
+ const fileNamePath = path . join ( configParams . LOCAL_FOLDER , fileName ) ;
31
+ const pdfFileData = await fs . readFile ( fileNamePath ) ;
32
+ await pdfApi . uploadFile ( fileName , pdfFileData ) ;
33
+ } ,
34
+
35
+ async downloadResult ( ) {
36
+ const changedPdfData = await pdfApi . downloadFile ( configParams . PDF_DOCUMENT_NAME ) ;
37
+ const filePath = path . join ( configParams . LOCAL_FOLDER , configParams . LOCAL_RESULT_DOCUMENT_NAME ) ;
38
+ await fs . writeFile ( filePath , changedPdfData . body ) ;
39
+ console . log ( "Downloaded: " + filePath ) ;
40
+ } ,
41
+
42
+ async uploadDocument ( ) {
43
+ await this . uploadFile ( configParams . PDF_DOCUMENT_NAME ) ;
44
+ } ,
45
+
46
+ addStamps : async function ( ) {
47
+
48
+ const textStamp = new Stamp ( ) ;
49
+ textStamp . type = "Text" ;
50
+ textStamp . background = true ;
51
+ textStamp . horizontalAlignment = "Center" ;
52
+ textStamp . textAlignment = "Center" ;
53
+ textStamp . value = "NEW TEXT STAMP" ;
54
+
55
+ const imageStamp = new Stamp ( ) ;
56
+ imageStamp . type = "Image" ;
57
+ imageStamp . background = true ;
58
+ imageStamp . horizontalAlignment = "Center" ;
59
+ imageStamp . textAlignment = "Center" ;
60
+ imageStamp . value = "NEW IMAGE STAMP" ;
61
+ imageStamp . fileName = configParams . IMAGE_STAMP_FILE ;
62
+ imageStamp . yIndent = configParams . IMAGE_STAMP_LLY ;
63
+ imageStamp . width = configParams . IMAGE_STAMP_WIDTH ;
64
+ imageStamp . height = configParams . IMAGE_STAMP_HEIGHT ;
65
+
66
+ const addResult = await pdfApi . postDocumentTextStamps ( configParams . PDF_DOCUMENT_NAME , [ textStamp ] )
67
+ . then ( async ( ) => {
68
+ return await pdfApi . postDocumentImageStamps ( configParams . PDF_DOCUMENT_NAME , [ imageStamp ] ) ;
69
+ } ) ;
70
+
71
+ if ( addResult . body . code == 200 ) {
72
+ console . log ( "Text and image stamps added!" ) ;
73
+ return true ;
74
+ }
75
+ else
76
+ console . error ( "Unexpected error : can't get pages!!!" ) ;
77
+ } ,
78
+ }
79
+
80
+ async function main ( ) {
81
+ try {
82
+ await pdfStamps . uploadDocument ( ) ;
83
+ await pdfStamps . uploadFile ( configParams . IMAGE_STAMP_FILE ) ;
84
+ await pdfStamps . addStamps ( ) ;
85
+ await pdfStamps . downloadResult ( ) ;
86
+ } catch ( error ) {
87
+ console . error ( "Error:" , error . message ) ;
88
+ }
89
+ }
90
+
91
+ main ( ) ;
0 commit comments