1+ import credentials from "../../../Credentials/credentials.json" with { type : "json" } ;
2+ import fs from 'node:fs/promises' ;
3+ import path from 'node:path' ;
4+ import { PdfApi } from "../../src/api/api.js" ;
5+ import { Signature } from "../../src/models/signature.js" ;
6+ import { SignatureType } from "../../src/models/signatureType.js" ;
7+ import { SignatureField } from "../../src/models/signatureField.js" ;
8+
9+ const configParams = {
10+ LOCAL_FOLDER : "C:\\Samples\\" ,
11+ PDF_DOCUMENT_NAME : "sample.pdf" ,
12+ LOCAL_RESULT_DOCUMENT_NAME : "output_sample.pdf" ,
13+ LOCAL_SIGNATURE_PATH : "C:\\Samples\\Signatures\\3" ,
14+ SIGNATURE_PFX : "signature.pfx" ,
15+ SIGNATURE_FORM_FIELD : 'Signature_1' ,
16+ SIGNATURE_PASSWORD : 'Password' ,
17+ SIGNATURE_CONTACT : 'Contact' ,
18+ SIGNATURE_LOCATION : 'Location' ,
19+ SIGNATURE_AUTHORITY : 'Issuer' ,
20+ SIGNATURE_DATE : '04/19/2025 12:15:00.000 PM' ,
21+ SIGNATURE_RECT : { lLx : 100 , lLy : 100 , uRx : 500 , uRy : 500 }
22+ } ;
23+
24+ const pdfApi = new PdfApi ( credentials . id , credentials . key ) ;
25+
26+ const pdfSignatures = {
27+ async uploadFile ( folder , fileName ) {
28+ const fileNamePath = path . join ( folder , fileName ) ;
29+ const pdfFileData = await fs . readFile ( fileNamePath ) ;
30+ await pdfApi . uploadFile ( fileName , pdfFileData ) ;
31+ console . log ( "File '" + fileName + "' successfully uploaded!" ) ;
32+ } ,
33+
34+ async uploadDocument ( ) {
35+ await this . uploadFile ( configParams . LOCAL_FOLDER , configParams . PDF_DOCUMENT_NAME ) ;
36+ } ,
37+
38+ async downloadResult ( ) {
39+ const changedPdfData = await pdfApi . downloadFile ( configParams . PDF_DOCUMENT_NAME ) ;
40+ const filePath = path . join ( configParams . LOCAL_FOLDER , configParams . LOCAL_RESULT_DOCUMENT_NAME ) ;
41+ await fs . writeFile ( filePath , changedPdfData . body ) ;
42+ console . log ( "Downloaded: " + filePath ) ;
43+ } ,
44+
45+ async addSignature ( ) {
46+ if ( pdfApi )
47+ {
48+
49+ const signature = new Signature ( ) ;
50+ signature . authority = configParams . SIGNATURE_AUTHORITY ;
51+ signature . contact = configParams . SIGNATURE_CONTACT ;
52+ signature . date = configParams . SIGNATURE_DATE ;
53+ signature . formFieldName = configParams . SIGNATURE_FORM_FIELD ;
54+ signature . location = configParams . SIGNATURE_LOCATION ;
55+ signature . password = configParams . SIGNATURE_PASSWORD ;
56+ signature . rectangle = configParams . SIGNATURE_RECT ;
57+ signature . signaturePath = configParams . SIGNATURE_PFX ;
58+ signature . signatureType = SignatureType . PKCS7 ;
59+ signature . visible = true ;
60+
61+ const field = new SignatureField ( ) ;
62+ field . pageIndex = 1 ;
63+ field . signature = signature ;
64+ field . partialName = 'sign1' ;
65+ field . rect = configParams . SIGNATURE_RECT ;
66+
67+ const response = await pdfApi . postSignatureField ( configParams . PDF_DOCUMENT_NAME , field ) ;
68+
69+ if ( response . body . code == 200 )
70+ console . log ( "addSignature(): Signature '" + configParams . SIGNATURE_CONTACT + "' successfully added to the document." ) ;
71+ else
72+ console . error ( "addSignature(): Failed to add signature to the document. Response code: " + response . body . code ) ;
73+ }
74+ } ,
75+ }
76+
77+ async function main ( ) {
78+ try {
79+ await pdfSignatures . uploadFile ( configParams . LOCAL_SIGNATURE_PATH , configParams . SIGNATURE_PFX ) ;
80+ await pdfSignatures . uploadDocument ( ) ;
81+ await pdfSignatures . addSignature ( ) ;
82+ await pdfSignatures . downloadResult ( ) ;
83+ } catch ( error ) {
84+ console . error ( "Error:" , error . message ) ;
85+ }
86+ }
87+
88+ main ( ) ;
0 commit comments