Skip to content

Commit b5dfb2d

Browse files
authored
Merge pull request #130 from aspose-pdf-cloud/develop
update to 25.5
2 parents 7187a61 + 4a4e9e9 commit b5dfb2d

21 files changed

+1273
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ XLS, XLSX, PPTX, DOC, DOCX, MobiXML, JPEG, EMF, PNG, BMP, GIF, TIFF, Text
3030
## Read PDF Formats
3131
MHT, PCL, PS, XSLFO, MD
3232

33-
## Enhancements in Version 25.4
34-
- Add method for adding Stamp per page in batch.
33+
## Enhancements in Version 25.5
34+
- Add a method for comparing pdf files.
3535
- A new version of Aspose.PDF Cloud was prepared using the latest version of Aspose.PDF for .NET.
3636

3737
## Installation
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
import { OptimizeOptions } from "../../src/models/optimizeOptions.js";
6+
7+
const configParams = {
8+
LOCAL_FOLDER: "C:\\Samples\\",
9+
PDF_DOCUMENT_NAME: "sample.pdf",
10+
TEMP_FOLDER : 'TempPdfCloud',
11+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
12+
};
13+
14+
const pdfApi = new PdfApi(credentials.id, credentials.key);
15+
16+
const PdfCompress = {
17+
async uploadDocument (fileName, localFolder) {
18+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
19+
const fileData = await fs.readFile(fileNamePath);
20+
const storagePath = path.join(configParams.TEMP_FOLDER, configParams.PDF_DOCUMENT_NAME);
21+
await pdfApi.uploadFile(storagePath, fileData)
22+
.then(() => console.log("File: '" + configParams.PDF_DOCUMENT_NAME +"' successfully uploaded."));
23+
},
24+
25+
async downloadResult() {
26+
const fileName = path.join( configParams.TEMP_FOLDER, configParams.PDF_DOCUMENT_NAME);
27+
const changedPdfData = await pdfApi.downloadFile(fileName);
28+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
29+
await fs.writeFile(filePath, changedPdfData.body);
30+
console.log("Downloaded: " + filePath);
31+
},
32+
33+
async compressPdfDocument() {
34+
if ( pdfApi ) {
35+
36+
const optimizeOptions = new OptimizeOptions();
37+
optimizeOptions.allowReusePageContent = true;
38+
optimizeOptions.compressImages = true;
39+
optimizeOptions.imageQuality = 100;
40+
optimizeOptions.linkDuplcateStreams = true;
41+
optimizeOptions.removeUnusedObjects = true;
42+
optimizeOptions.removeUnusedStreams = true;
43+
optimizeOptions.unembedFonts = true;
44+
45+
const response = await pdfApi.postOptimizeDocument(configParams.PDF_DOCUMENT_NAME, optimizeOptions, null, configParams.TEMP_FOLDER);
46+
if (response.body.code != 200)
47+
console.error("compressPdfDocument(): Failed to compress the PDF document!");
48+
else
49+
console.log("compressPdfDocument(): Successfully copressed the PDF document '" + configParams.PDF_DOCUMENT_NAME + "' !");
50+
}
51+
},
52+
};
53+
54+
async function main() {
55+
try {
56+
await PdfCompress.uploadDocument();
57+
await PdfCompress.compressPdfDocument();
58+
await PdfCompress.downloadResult();
59+
60+
} catch (error) {
61+
console.error("Error:", error.message);
62+
}
63+
}
64+
65+
main();

UsesCases/CreateDocument/createPdf.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
import { DocumentConfig } from "../../src/models/documentConfig.js"
6+
import { DocumentProperties } from "../../src/models/documentProperties.js"
7+
import { DocumentProperty } from "../../src/models/documentProperty.js"
8+
import { DisplayProperties } from "../../src/models/displayProperties.js"
9+
import { DefaultPageConfig } from "../../src/models/defaultPageConfig.js"
10+
import { Direction } from "../../src/models/direction.js";
11+
import { PageMode } from "../../src/models/pageMode.js";
12+
import { PageLayout } from "../../src/models/pageLayout.js";
13+
14+
const configParams = {
15+
LOCAL_FOLDER: "C:\\Samples\\",
16+
TEMP_FOLDER : 'TempPdfCloud',
17+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
18+
PAGE_WIDTH: 590,
19+
PAGE_HEIGHT: 894,
20+
PAGES_COUNT: 5,
21+
};
22+
23+
const pdfApi = new PdfApi(credentials.id, credentials.key);
24+
25+
const PdfPageChanges = {
26+
async downloadResult() {
27+
const fileName = path.join( configParams.TEMP_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
28+
const changedPdfData = await pdfApi.downloadFile(fileName);
29+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
30+
await fs.writeFile(filePath, changedPdfData.body);
31+
console.log("Downloaded: " + filePath);
32+
},
33+
34+
async createPdfDocument() {
35+
const pdfConfig = new DocumentConfig();
36+
pdfConfig.pagesCount = configParams.PAGES_COUNT;
37+
38+
pdfConfig.displayProperties = new DisplayProperties();
39+
pdfConfig.displayProperties.centerWindow = true;
40+
pdfConfig.displayProperties.hideMenuBar = true;
41+
pdfConfig.displayProperties.direction = Direction.L2R;
42+
pdfConfig.displayProperties.displayDocTitle = true;
43+
pdfConfig.displayProperties.hideToolBar = true;
44+
pdfConfig.displayProperties.hideWindowUI = true;
45+
pdfConfig.displayProperties.nonFullScreenPageMode = PageMode.UseThumbs;
46+
pdfConfig.displayProperties.pageLayout = PageLayout.TwoPageLeft;
47+
pdfConfig.displayProperties.pageMode = PageMode.UseThumbs;
48+
49+
pdfConfig.documentProperties = new DocumentProperties();
50+
const docProperty = new DocumentProperty();
51+
docProperty.builtIn = false;
52+
docProperty.name = "prop1";
53+
docProperty.value = "Val1";
54+
55+
pdfConfig.documentProperties.list = [ docProperty ];
56+
57+
pdfConfig.defaultPageConfig = new DefaultPageConfig();
58+
pdfConfig.defaultPageConfig.height = configParams.CROP_HEIGHT;
59+
pdfConfig.defaultPageConfig.width = configParams.CROP_WIDTH;
60+
61+
const response = await pdfApi.postCreateDocument(configParams.LOCAL_RESULT_DOCUMENT_NAME, pdfConfig, null, configParams.TEMP_FOLDER);
62+
console.log("Document #" + configParams.LOCAL_RESULT_DOCUMENT_NAME + " created.")
63+
return response;
64+
},
65+
};
66+
67+
async function main() {
68+
try {
69+
await PdfPageChanges.createPdfDocument();
70+
await PdfPageChanges.downloadResult();
71+
} catch (error) {
72+
console.error("Error:", error.message);
73+
}
74+
}
75+
76+
main();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
2+
import fs from 'node:fs/promises';
3+
import path from 'node:path';
4+
import { PdfApi } from "../../src/api/api.js";
5+
6+
const loccalFolder = "C:\\Samples\\";
7+
const pdfDocument = "output_sample.pdf";
8+
9+
// Create PDF Rest API object
10+
const pdfApi = new PdfApi(credentials.id, credentials.key);
11+
12+
// Create empty Pdf
13+
const pdfResponse = await pdfApi.putCreateDocument(pdfDocument, null, null)
14+
.then(async () => {
15+
console.log("Document #" + pdfDocument + " successfully created.");
16+
17+
// Download empty Pdf document to local folder
18+
await pdfApi.downloadFile(pdfDocument)
19+
.then (async (data) => {
20+
const filePath = path.join(loccalFolder, pdfDocument);
21+
await fs.writeFile(filePath, data.body);
22+
console.log("Downloaded: " + filePath);
23+
});
24+
})
25+
.except((error) => {
26+
// Catch any exceptions
27+
console.log("Failed to create empty DPDF dcument '" + pdfDocument + "' !")
28+
console.error("Error:", error.message);
29+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
6+
7+
const configParams = {
8+
LOCAL_FOLDER: "C:\\Samples\\",
9+
PDF_DOCUMENT_NAME: "sample-signed.pdf",
10+
}
11+
12+
const pdfApi = new PdfApi(credentials.id, credentials.key);
13+
14+
const pdfSignatures = {
15+
async uploadDocument () {
16+
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
17+
const pdfFileData = await fs.readFile(fileNamePath);
18+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
19+
console.log("File '" + configParams.PDF_DOCUMENT_NAME + "' successfully uploaded!");
20+
},
21+
22+
showSignatureFieldsArray(fields)
23+
{
24+
if (fields.list.length > 0)
25+
fields.list.forEach(function(item) {
26+
console.log("Signature => value: '" + item.signature.contact + "'");
27+
});
28+
else
29+
console.log("Signature fileds is empty!");
30+
},
31+
32+
async getSignatureFields () {
33+
if (pdfApi)
34+
{
35+
const response = await pdfApi.getDocumentSignatureFields(configParams.PDF_DOCUMENT_NAME);
36+
37+
if (response.body.code == 200 && response.body.fields) {
38+
console.log("getSignatureFields(): Signature fields successfully extracted in to the '" + configParams.PDF_DOCUMENT_NAME + "' documen:")
39+
this.showSignatureFieldsArray(response.body.fields);
40+
}
41+
else
42+
console.error("getSignatures(): Failed to extract signatures in the document. Response code: " + response.body.code);
43+
}
44+
},
45+
}
46+
47+
async function main() {
48+
try {
49+
await pdfSignatures.uploadDocument();
50+
await pdfSignatures.getSignatureFields();
51+
} catch (error) {
52+
console.error("Error:", error.message);
53+
}
54+
}
55+
56+
main();

0 commit comments

Comments
 (0)