Skip to content

Commit 0312063

Browse files
authored
Merge pull request #116 from aspose-pdf-cloud/develop
Develop
2 parents cefd483 + a6980b7 commit 0312063

27 files changed

+1600
-47
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +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.1
33+
## Enhancements in Version 25.2
3434
- A new version of Aspose.PDF Cloud was prepared using the latest version of Aspose.PDF for .NET.
35-
3635
## Installation
3736

3837
### NPM
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 Attachment with the required properties
5+
// 5. Append new Attachment to the document using postAddDocumentAttachment() function
6+
// 6. Perform some action after successful addition attachment
7+
// 7. Download result file after appending Attachment
8+
// All values of variables starting with "YOUR_****" should be replaced by real user values
9+
10+
import credentials from "./credentials.json" with { type: "json" };
11+
import fs from 'node:fs/promises';
12+
import path from 'node:path';
13+
import { PdfApi } from "asposepdfcloud";
14+
import { AttachmentInfo } from "asposepdfcloud/src/models/attachmentInfo.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_ATTACHMENT_FILE: "sample_video.mp4",
21+
NEW_ATTACHMENT_MIME: "video/mp4",
22+
NEW_ATTACHMENT_DECRIPTION: 'This is a sample attachment',
23+
PAGE_NUMBER: 2,
24+
};
25+
26+
const pdfApi = new PdfApi(credentials.id, credentials.key);
27+
28+
const pdfAttachments = {
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+
async appendAttachment() {
47+
48+
const newAttachment = new AttachmentInfo();
49+
newAttachment.path = configParams.NEW_ATTACHMENT_FILE;
50+
newAttachment.description = configParams.NEW_ATTACHMENT_DECRIPTION;
51+
newAttachment.name = configParams.NEW_ATTACHMENT_FILE;
52+
53+
const response = await pdfApi.postAddDocumentAttachment(configParams.PDF_DOCUMENT_NAME, newAttachment);
54+
const { code } = response.body;
55+
56+
if (code === 200) {
57+
console.log("Appended attachment successfulll");
58+
}
59+
},
60+
};
61+
62+
async function main() {
63+
try {
64+
await pdfAttachments.uploadDocument();
65+
await pdfAttachments.uploadFile(configParams.NEW_ATTACHMENT_FILE);
66+
await pdfAttachments.appendAttachment();
67+
await pdfAttachments.downloadResult();
68+
} catch (error) {
69+
console.error("Error:", error.message);
70+
}
71+
}
72+
73+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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. Get all document attachments by using getDocumentAttachments() function
5+
// 5. Perform some action with attachments array (for example, show information)
6+
// 6. Get attachment by path using getDocumentAttachmentByIndex() function
7+
// 7. Perform some action after successful retrieving attachmwent file name using getDownloadDocumentAttachmentByIndex() function
8+
// 8. Save response body to local file name
9+
// All values of variables starting with "YOUR_****" should be replaced by real user values
10+
import credentials from "./credentials.json" with { type: "json" };
11+
import fs from 'node:fs/promises';
12+
import path from 'node:path';
13+
import { PdfApi } from "asposepdfcloud";
14+
15+
const configParams = {
16+
LOCAL_FOLDER: "C:\\Samples\\",
17+
PDF_DOCUMENT_NAME: "sample_file_with_attachment.pdf",
18+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
19+
ATTACHMENT_PATH: "", // filed will be setting by using attachments.list[0].links[0].href after call getDocumentAttachments() function
20+
};
21+
22+
const pdfApi = new PdfApi(credentials.id, credentials.key);
23+
24+
const pdfAttachments = {
25+
async uploadDocument() {
26+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
27+
const pdfFileData = await fs.readFile(pdfFilePath);
28+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
29+
console.log("Uploaded file: " + configParams.PDF_DOCUMENT_NAME);
30+
},
31+
32+
showAttachments(attachments, prefix) {
33+
if (Array.isArray(attachments) && attachments.length > 0)
34+
{
35+
attachments.forEach(function(attachment) {
36+
console.log(prefix +" => name: '" + attachment.name + "', path: '" + attachment.links[0].href + "'");
37+
});
38+
}
39+
},
40+
41+
async getAttachments() {
42+
const response = await pdfApi.getDocumentAttachments(configParams.PDF_DOCUMENT_NAME);
43+
const { code, attachments } = response.body;
44+
45+
if (code === 200 && attachments.list && attachments.list.length > 0) {
46+
this.showAttachments(attachments.list, "all");
47+
configParams.ATTACHMENT_PATH = attachments.list[0].links[0].href;
48+
}
49+
else
50+
{
51+
console.error("Document has no attachmnets!!!");
52+
}
53+
},
54+
async getAttachmentById() {
55+
const response = await pdfApi.getDocumentAttachmentByIndex(configParams.PDF_DOCUMENT_NAME, configParams.ATTACHMENT_PATH);
56+
const { code, attachment } = response.body;
57+
58+
if (code == 200) {
59+
console.log("Attachment file: " + attachment.name);
60+
const attachmentFile = await pdfApi.getDownloadDocumentAttachmentByIndex(configParams.PDF_DOCUMENT_NAME, configParams.ATTACHMENT_PATH)
61+
const localPath = path.join(configParams.LOCAL_FOLDER, attachment.name);
62+
await fs.writeFile(localPath, attachmentFile.body);
63+
console.log("Downloaded: " + localPath);
64+
}
65+
}
66+
};
67+
68+
async function main() {
69+
try {
70+
await pdfAttachments.uploadDocument();
71+
await pdfAttachments.getAttachments();
72+
await pdfAttachments.getAttachmentById();
73+
} catch (error) {
74+
console.error("Error:", error.message);
75+
}
76+
}
77+
78+
main();
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 Bookmarks with the required properties
5+
// 5. Append new Bookmarks to the document using postBookmark() 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.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
import { Color } from "asposepdfcloud/src/models/color.js";
14+
import { Link } from "asposepdfcloud/src/models/link.js";
15+
import { Bookmark } from "asposepdfcloud/src/models/bookmark.js";
16+
import { Bookmarks } from "asposepdfcloud/src/models/bookmarks.js";
17+
18+
const configParams = {
19+
LOCAL_FOLDER: "C:\\Samples\\",
20+
PDF_DOCUMENT_NAME: "sample.pdf",
21+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
22+
NEW_BOOKMARK_TITLE: "• Productivity improvement",
23+
PARENT_BOOKMARK_FOR_APPEND: "", //The parent bookmark path. Specify an empty string when adding a bookmark to the root.
24+
NEW_BOOKMARK_PAGE_NUMBER: 2,
25+
};
26+
27+
const pdfApi = new PdfApi(credentials.id, credentials.key);
28+
29+
const pdfBookmarks = {
30+
async uploadDocument() {
31+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
32+
const pdfFileData = await fs.readFile(pdfFilePath);
33+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
34+
},
35+
36+
async downloadResult() {
37+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
38+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
39+
await fs.writeFile(filePath, changedPdfData.body);
40+
console.log("Downloaded: " + filePath);
41+
},
42+
43+
async appendBookmarkLink() {
44+
const bookmarkLink = new Link({ rel: "self" });
45+
const bookmarkColor = new Color({ a: 255, r: 0, g: 255, b: 0 });
46+
47+
const newBookmark = new Bookmark();
48+
newBookmark.title = configParams.NEW_BOOKMARK_TITLE;
49+
newBookmark.italic = true;
50+
newBookmark.bold = false;
51+
newBookmark.links = [bookmarkLink];
52+
newBookmark.color = bookmarkColor;
53+
newBookmark.action = "GoTo";
54+
newBookmark.level = 1;
55+
newBookmark.pageDisplayLeft = 83;
56+
newBookmark.pageDisplayTop = 751;
57+
newBookmark.pageDisplayZoom = 2;
58+
newBookmark.pageNumber = configParams.NEW_BOOKMARK_PAGE_NUMBER;
59+
60+
const response = await pdfApi.postBookmark(configParams.PDF_DOCUMENT_NAME, configParams.PARENT_BOOKMARK_FOR_APPEND, [newBookmark]);
61+
const { code, bookmarks } = response.body;
62+
63+
if (code === 200 && bookmarks) {
64+
const addedBookmark = bookmarks.list[bookmarks.list.length - 1];
65+
console.log("Appended bookmark: " + addedBookmark.links[0].href + " => " + addedBookmark.title);
66+
return addedBookmark;
67+
}
68+
},
69+
};
70+
71+
async function main() {
72+
try {
73+
await pdfBookmarks.uploadDocument();
74+
await pdfBookmarks.appendBookmarkLink();
75+
await pdfBookmarks.downloadResult();
76+
} catch (error) {
77+
console.error("Error:", error.message);
78+
}
79+
}
80+
81+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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. Retrieve required Bookmark from the document using getBookmark() function
5+
// 6. Perform some action after successful retrieving the Bookmark from document
6+
// All values of variables starting with "YOUR_****" should be replaced by real user values
7+
8+
import credentials from "credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
9+
import fs from 'node:fs/promises';
10+
import path from "node:path";
11+
import { PdfApi } from "asposepdfcloud";
12+
13+
const configParams = {
14+
LOCAL_FOLDER: "C:\\Samples\\",
15+
PDF_DOCUMENT_NAME: "sample.pdf",
16+
BOOKMARK_PATH: "/5",
17+
};
18+
19+
const pdfApi = new PdfApi(credentials.id, credentials.key);
20+
21+
const pdfBookmarks = {
22+
async uploadDocument() {
23+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
24+
const pdfFileData = await fs.readFile(pdfFilePath);
25+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
26+
},
27+
28+
async getBookmarkByPath() {
29+
const resultBookmark = await pdfApi.getBookmark(configParams.PDF_DOCUMENT_NAME, configParams.BOOKMARK_PATH);
30+
const { code, bookmark } = resultBookmark.body;
31+
32+
console.log(`Found bookmark title: ${bookmark.title}`);
33+
return bookmark;
34+
},
35+
36+
};
37+
38+
async function main() {
39+
try {
40+
await pdfBookmarks.uploadDocument();
41+
await pdfBookmarks.getBookmarkByPath();
42+
} catch (error) {
43+
console.error("Error:", error.message);
44+
}
45+
}
46+
47+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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. Retrieve required Bookmarks from the document using getBookmark() function
5+
// 6. Perform some action after successful retrieving the Bookmark from document
6+
// All values of variables starting with "YOUR_****" should be replaced by real user values
7+
8+
import credentials from "credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
9+
import fs from 'node:fs/promises';
10+
import path from "node:path";
11+
import { PdfApi } from "asposepdfcloud";
12+
13+
const configParams = {
14+
LOCAL_FOLDER: "C:\\Samples\\",
15+
PDF_DOCUMENT_NAME: "sample.pdf",
16+
BOOKMARK_PATH: "/5",
17+
};
18+
19+
const pdfApi = new PdfApi(credentials.id, credentials.key);
20+
21+
const pdfBookmarks = {
22+
async uploadDocument() {
23+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
24+
const pdfFileData = await fs.readFile(pdfFilePath);
25+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
26+
},
27+
28+
async getAllBookmarks() {
29+
const resultBookmarks = await pdfApi.getDocumentBookmarks(configParams.PDF_DOCUMENT_NAME);
30+
const { code, bookmarks } = resultBookmarks.body;
31+
32+
this.showBookmarks(bookmarks, "all");
33+
return bookmarks;
34+
},
35+
36+
async showBookmarks(bookmarks, prefix) {
37+
if (Array.isArray(bookmarks.list) && bookmarks.list.length > 0) {
38+
for (const bookmark of bookmarks.list) {
39+
console.log(`${prefix} => level: '${bookmark.level}', title: '${bookmark.title}'`);
40+
}
41+
}
42+
},
43+
};
44+
45+
async function main() {
46+
try {
47+
await pdfBookmarks.uploadDocument();
48+
await pdfBookmarks.getAllBookmarks();
49+
} catch (error) {
50+
console.error("Error:", error.message);
51+
}
52+
}
53+
54+
main();

0 commit comments

Comments
 (0)