Skip to content

PDFAPPS-6673: added use cases for Images #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions UsesCases/Images/add/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
// 2. Create an object to connect to the Pdf.Cloud API
// 3. Initialize neccessary values for append image
// 4. Perform the inserting image in Pdf document using postInsertImage() function and new image file initialized values
// 6. Check result and perform some actions with result.body object
// All values of variables starting with "YOUR_****" should be replaced by real user values

import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import { PdfApi } from "asposepdfcloud";

const LOCAL_PATH = "C:\\Samples\\";
const FILE_NAME = "Sample-Document";
const FILE_EXT = ".pdf";

const STORAGE_PATH = "YOUR_REMOTE_PATH/";
const PDF_DOCUMENT_NAME = FILE_NAME + FILE_EXT;

const IMAGE_FILE_NAME = "YOUR_IMAGE_FOLDER_AND_PATH";

const pdfApi = new PdfApi(credentials.id, credentials.key);

const storage = null;

const pdfFileData = fs.readFileSync(LOCAL_PATH + PDF_DOCUMENT_NAME);
await api.uploadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, pdfFileData, storage);

const folder = "Documents";

const destFolder = "Images";

const pageNum = 1;

const llx = 10;
const lly = 10;

const urx = 100;
const ury = 100;

const resultAppend = await pdfApi.postInsertImage(STORAGE_PATH + PDF_DOCUMENT_NAME, pageNum, llx, lly, urx, ury, IMAGE_FILE_NAME, storage, folder, null);

if (resultAppend.body.status == 200)
{
const newPdfData = pdfApi.downloadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, storage);

const filePath = "YOUR_LOCAL_OUTPUT_FOLDER/ResultInsertImageFile.pdf";

await fs.writeFile(filePath, newPdfData.body);
console.log("downloaded: " + filePath);
}
else
console.log("Unable to insert image into the file!!!");
40 changes: 40 additions & 0 deletions UsesCases/Images/get/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
// 2. Create an object to connect to the Pdf.Cloud API
// 3. Upload your document file
// 4. Perform the rtreiving image Id from Pdf document using getIimages function
// 5. Perform the extracting image from Pdf document using getImage() function and image ID value
// 6. Check result and perform some actions with result.body object
// All values of variables starting with "YOUR_****" should be replaced by real user values


import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import { PdfApi } from "asposepdfcloud";

const LOCAL_PATH = "C:\\Samples\\";
const FILE_NAME = "Sample-Document";
const FILE_EXT = ".pdf";

const STORAGE_PATH = "YOUR_REMOTE_PATH/";
const PDF_DOCUMENT_NAME = FILE_NAME + FILE_EXT;

const pdfApi = new PdfApi(credentials.id, credentials.key);

const storage = null;

const pdfFileData = fs.readFileSync(LOCAL_PATH + PDF_DOCUMENT_NAME);
await api.uploadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, pdfFileData, storage);

const folder = "Documents";

let resultImageId = await pdfApi.getImages(fileName, 1, storage, folder);
const imageID = resultImageId.body.images.list[0].id;

let resultImage = await pdfApi.getImage(fileName, imageID, storage, folder);

await Promise.all(
resultImage.body.images.map(async (image, index) => {
const filePath = "YOUR_LOCAL_OUTPUT_PATH/image_" + FILE_NAME + "_" + (index + 1) + ".jpg";
fs.writeFileSync(filePath, image.links[0].href);
})
);
42 changes: 42 additions & 0 deletions UsesCases/Images/get_common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
// 2. Create an object to connect to the Pdf.Cloud API
// 3. Upload your document file
// 4. Perform the retreiving image Id from Pdf document using getIimages() function
// 5. Perform the reading image from Pdf document using getImage() function and image ID value
// 6. Check result and perform some actions with result.body object
// 7. Perform extrating image in Png format from Pdf document into local folder using putImageExtractAsPng() function and image ID value
// 8. Check result and perform some actions with result.body object
// All values of variables starting with "YOUR_****" should be replaced by real user values

import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import { PdfApi } from "asposepdfcloud";

const LOCAL_PATH = "C:\\Samples\\";
const FILE_NAME = "Sample-Document";
const FILE_EXT = ".pdf";

const PDF_DOCUMENT_NAME = FILE_NAME + FILE_EXT;

const pdfApi = new PdfApi(credentials.id, credentials.key);

const storage = null;

const width = 512;

const heigth = 512;

const folder = "Documents";

const destFolder = "Images";

const pdfFileData = fs.readFileSync(LOCAL_PATH + PDF_DOCUMENT_NAME);
await api.uploadFile(PDF_DOCUMENT_NAME, pdfFileData, storage);

const resultImageId = await pdfApi.getImages(PDF_DOCUMENT_NAME, 1, storage, folder);

const imageID = resultImageId.body.images.list[0].id;

const extractResult = await pdfApi.putImageExtractAsPng(PDF_DOCUMENT_NAME, imageID, width, heigth, storage, folder, destFolder);

console.log(extractResult.body.status);
44 changes: 44 additions & 0 deletions UsesCases/Images/remove/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
// 2. Create an object to connect to the Pdf.Cloud API
// 3. Upload your document file
// 4. Perform the rtreiving image Id from Pdf document using getIimages function
// 5. Delete image from Pdf document using deleteImage function and image ID value
// 6. Check result and perform some actions with result.body object
// All values of variables starting with "YOUR_****" should be replaced by real user values

import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import { PdfApi } from "asposepdfcloud";

const LOCAL_PATH = "C:\\Samples\\";
const FILE_NAME = "Sample-Document";
const FILE_EXT = ".pdf";

const STORAGE_PATH = "YOUR_REMOTE_PATH/";
const PDF_DOCUMENT_NAME = FILE_NAME + FILE_EXT;

const pdfApi = new PdfApi(credentials.id, credentials.key);

const storage = null;

const folder = "Documents";

const pdfFileData = fs.readFileSync(LOCAL_PATH + PDF_DOCUMENT_NAME);
await api.uploadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, pdfFileData, storage);

let resultImageId = await pdfApi.getImages(fileName, 1, storage, folder);
const imageID = resultImageId.body.images.list[0].id;

let resultDelete = await pdfApi.deleteImage(STORAGE_PATH + PDF_DOCUMENT_NAME, imageID, storage, folder);

if (resultAppend.body.status == 200)
{
const newPdfData = pdfApi.downloadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, storage);

const filePath = "YOUR_LOCAL_OUTPUT_FOLDER/ResultDeleteImageFile.pdf";

await fs.writeFile(filePath, newPdfData.body);
console.log("downloaded: " + filePath);
}
else
console.log("Unable to delete image from the file!!!");
46 changes: 46 additions & 0 deletions UsesCases/Images/update/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
// 2. Create an object to connect to the Pdf.Cloud API
// 3. Upload your document file
// 4. Perform the retreiving image Id from Pdf document using getIimages() function
// 5. Perform the unpdating image in Pdf document using putReplaceImage() function, image ID value and new image file
// 6. Check result and perform some actions with result.body object
// All values of variables starting with "YOUR_****" should be replaced by real user values

import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import { PdfApi } from "asposepdfcloud";

const LOCAL_PATH = "C:\\Samples\\";
const FILE_NAME = "Sample-Document";
const FILE_EXT = ".pdf";

const STORAGE_PATH = "YOUR_REMOTE_PATH/";
const PDF_DOCUMENT_NAME = FILE_NAME + FILE_EXT;

const IMAGE_FILE_NAME = "YOUR_IMAGE_FOLDER_AND_PATH";

const pdfApi = new PdfApi(credentials.id, credentials.key);

const storage = null;

const pdfFileData = fs.readFileSync(LOCAL_PATH + PDF_DOCUMENT_NAME);
await api.uploadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, pdfFileData, storage);

const folder = "Documents";

let resultImageId = await pdfApi.getImages(fileName, 1, storage, folder);
const imageID = resultImageId.body.images.list[0].id;

let resultUpdate = await pdfApi.putReplaceImage(fileName, imageID, IMAGE_FILE_NAME, storage, folder, null);

if (resultAppend.body.status == 200)
{
const dropPdfData = pdfApi.downloadFile(STORAGE_PATH + PDF_DOCUMENT_NAME, storage);

const filePath = "YOUR_LOCAL_OUTPUT_FOLDER/ResultDeleteImageFile.pdf";

await fs.writeFile(filePath, dropPdfData.body);
console.log("downloaded: " + filePath);
}
else
console.log("Unable to insert image into the file!!!");