Skip to content

Commit 37c3afe

Browse files
author
kirill.novinskiy@aspose.com
committed
add useCases
1 parent 2d4287b commit 37c3afe

File tree

5 files changed

+480
-0
lines changed

5 files changed

+480
-0
lines changed

useCases/convertToPdf3A.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Import necessary classes from the Aspose PDF Cloud library
2+
const fs = require("fs");
3+
const { PdfApi } = require("asposepdfcloud");
4+
const { PdfAType } = require("asposepdfcloud/src/models/pdfAType");
5+
6+
// The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/
7+
const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY");
8+
9+
// Set the document name
10+
const fileName = "4pages.pdf";
11+
// Set the document name
12+
const outputPath = "PDF3A/4pages.pdf";
13+
// Use default storage (null indicates default storage)
14+
const storage = null;
15+
// Set the folder where the document is stored
16+
const folder = "Documents";
17+
// Set the pdf document has no password.
18+
const password = null;
19+
20+
/**
21+
* Convert the PDF document to PDF3A using the Aspose.PDF Cloud API.
22+
*
23+
* This function performs the following steps:
24+
* 1. Reads a PDF file from the local file system.
25+
* 2. Uploads the PDF file to the Aspose.PDF Cloud storage.
26+
* 3. Convert PDF document to PDF3A format using the Aspose.PDF Cloud API.
27+
* 5. Logs the result to the console.
28+
*
29+
* @returns {Promise<void>} A Promise that resolves when the adding text is complete.
30+
*/
31+
async function getPdfInStorageToPdfA()
32+
{
33+
// Read file from file system.
34+
const buffer = fs.readFileSync("testData/" + fileName);
35+
// Upload file to cloud storage.
36+
const uploadResponse = await api.uploadFile(folder + "/" +fileName, buffer, storage)
37+
// Swagger method for adding text: https://reference.aspose.cloud/pdf/#/Convert/PutPdfAInStorageToPdf
38+
// Call the API to convert the specified PDF document to PDF3A format
39+
const result = await api.putPdfInStorageToPdfA(
40+
uploadResponse.body.uploaded[0],
41+
outputPath,
42+
PdfAType.PDFA3A,
43+
folder,
44+
storage,
45+
password);
46+
47+
// Log the response to console.
48+
console.log(result.body.status); // Log the status of the operation
49+
}
50+
51+
// Execute the putAddText function
52+
getPdfInStorageToPdfA();

useCases/textAdd.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Import necessary classes from the Aspose PDF Cloud library
2+
const fs = require("fs");
3+
const { PdfApi } = require("../src/api/api");
4+
const { Color } = require("../src/models/color");
5+
const { FontStyles } = require("../src/models/fontStyles");
6+
const { LineSpacing } = require("../src/models/lineSpacing");
7+
const { Paragraph } = require("../src/models/paragraph");
8+
const { TextHorizontalAlignment } = require("../src/models/textHorizontalAlignment");
9+
const { VerticalAlignment } = require("../src/models/verticalAlignment");
10+
const { WrapMode } = require("../src/models/wrapMode");
11+
const { TextLine } = require("asposepdfcloud/src/models/textLine");
12+
const { Segment } = require("asposepdfcloud/src/models/segment");
13+
const { Rectangle } = require("asposepdfcloud/src/models/rectangle");
14+
const { TextState } = require("asposepdfcloud/src/models/textState");
15+
16+
// The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/
17+
const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY");
18+
19+
// Set the document name
20+
const fileName = "4pages.pdf";
21+
// Set the page number where the text will be added
22+
const pageNumber = 3;
23+
// Use default storage (null indicates default storage)
24+
const storage = null;
25+
// Set the folder where the document is stored
26+
const folder = "Documents";
27+
28+
/**
29+
* Add text to a PDF document page using the Aspose.PDF Cloud API.
30+
*
31+
* This function performs the following steps:
32+
* 1. Reads a PDF file from the local file system.
33+
* 2. Uploads the PDF file to the Aspose.PDF Cloud storage.
34+
* 3. Creates an `Paragraph` object with the inserted text.
35+
* 4. Put text on the third page of PDF document using the Aspose.PDF Cloud API.
36+
* 5. Logs the result to the console.
37+
*
38+
* @returns {Promise<void>} A Promise that resolves when the adding text is complete.
39+
*/
40+
async function putAddText()
41+
{
42+
// Read file from file system.
43+
const buffer = fs.readFileSync("testData/" + fileName);
44+
// Upload file to cloud storage.
45+
const uploadResponse = await api.uploadFile(folder + "/" +fileName, buffer, storage)
46+
47+
// Represents color DTO.
48+
const foregroundColor = new Color();
49+
// Set an alpha component.
50+
foregroundColor.a = 255;
51+
// Set the red component.
52+
foregroundColor.r = 0;
53+
// Set the green component.
54+
foregroundColor.g = 0;
55+
// Set the blue component.
56+
foregroundColor.b = 255;
57+
58+
// Represents color DTO.
59+
const backgroundColor = new Color();
60+
// Set an alpha component.
61+
backgroundColor.a = 100;
62+
// Set the red component.
63+
backgroundColor.r = 255;
64+
// Set the green component.
65+
backgroundColor.g = 255;
66+
// Set the blue component.
67+
backgroundColor.b = 0;
68+
69+
// Create the new paragraph object to represent text paragraphs
70+
const rectangle = new Rectangle();
71+
// Lower left x coordinate
72+
rectangle.lLX = 10;
73+
// Lower left y coordinate
74+
rectangle.lLY = 10;
75+
// Upper right x coordinate
76+
rectangle.uRX = 300;
77+
// Upper right y coordinate
78+
rectangle.uRY = 500;
79+
80+
// Create the new text state.
81+
const textState = new TextState();
82+
// Set font size of the text
83+
textState.fontSize = 20;
84+
// Set font name of the text
85+
textState.font = "Arial";
86+
// Set foreground color of the text
87+
textState.foregroundColor = foregroundColor;
88+
// Set background color of the text
89+
textState.backgroundColor = backgroundColor;
90+
// Set font style of the text
91+
textState.fontStyle = FontStyles.Regular;
92+
// Font file path in storage (null means default font)
93+
textState.fontFile = null;
94+
// Set underline for the text
95+
textState.underline = true;
96+
// Set strikeout for the text
97+
textState.strikeOut = false;
98+
// Set superscript mode for the text
99+
textState.superscript = false;
100+
// Set subscript mode for the text
101+
textState.subscript = false;
102+
103+
// Create the new text segment.
104+
const segment = new Segment();
105+
// Text value for the segment
106+
segment.value = "segment text 1 with text state";
107+
// Define the text state for formatting
108+
segment.textState = textState;
109+
110+
// Create the new text line.
111+
const textLine = new TextLine();
112+
// Set the line's horizontal alignment to Right
113+
textLine.horizontalAlignment = TextHorizontalAlignment.Right;
114+
// Define the segments that form the line
115+
textLine.segments = [segment];
116+
117+
// Create new text paragraph.
118+
const paragraph = new Paragraph();
119+
// Set the line spacing mode to FullSize
120+
paragraph.lineSpacing = LineSpacing.FullSize;
121+
// Set the word wrap mode to wrap by words
122+
paragraph.wrapMode = WrapMode.ByWords;
123+
// Set the horizontal alignment for the text inside the paragraph
124+
paragraph.horizontalAlignment = TextHorizontalAlignment.FullJustify;
125+
// Set the left margin for the paragraph
126+
paragraph.leftMargin = 100;
127+
// Set the right margin for the paragraph
128+
paragraph.rightMargin = 100;
129+
// Set the top margin for the paragraph
130+
paragraph.topMargin = 200;
131+
// Set the bottom margin for the paragraph
132+
paragraph.bottomMargin = 200;
133+
// Define the rectangle area for the paragraph
134+
paragraph.rectangle = rectangle;
135+
// Set the rotation angle of the text in degrees
136+
paragraph.rotation = 10,
137+
// Set the indent for subsequent lines
138+
paragraph.subsequentLinesIndent = 30,
139+
// Set the vertical alignment for the text inside the paragraph
140+
paragraph.vercolorticalAlignment = VerticalAlignment.Center,
141+
// Define an array of text lines for the paragraph
142+
paragraph.lines = [textLine];
143+
144+
// Swagger method for adding text: https://reference.aspose.cloud/pdf/#/Text/PutAddText
145+
// Call the API to add text to the specified PDF document page
146+
const result = await api.putAddText(
147+
uploadResponse.body.uploaded[0],
148+
pageNumber,
149+
paragraph,
150+
folder,
151+
storage);
152+
153+
// Log the response to console.
154+
console.log(result.body.status); // Log the status of the operation
155+
}
156+
157+
// Execute the putAddText function
158+
putAddText();

useCases/textDeletejs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Import necessary classes from the Aspose PDF Cloud library
2+
const fs = require("fs");
3+
const { PdfApi } = require("../src/api/api");
4+
const { TextReplace } = require("asposepdfcloud/src/models/textReplace");
5+
const { TextReplaceListRequest } = require("asposepdfcloud/src/models/textReplaceListRequest");
6+
7+
// The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/
8+
const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY");
9+
10+
// Set the document name
11+
const fileName = "4pages.pdf";
12+
// Use default storage (null indicates default storage)
13+
const storage = null;
14+
// Set the folder where the document is stored
15+
const folder = "Documents";
16+
17+
/**
18+
* Delete text of a PDF document using the Aspose.PDF Cloud API.
19+
*
20+
* This function performs the following steps:
21+
* 1. Reads a PDF file from the local file system.
22+
* 2. Uploads the PDF file to the Aspose.PDF Cloud storage.
23+
* 3. Creates an `TextReplaceListRequest` object with the replacements list.
24+
* 4. Delete text of PDF document using the Aspose.PDF Cloud API.
25+
* 5. Logs the result to the console.
26+
*
27+
* @returns {Promise<void>} A promise that is resolves after text deletion is complete.
28+
*/
29+
async function postDocumentTextDelete()
30+
{
31+
// Read file from file system.
32+
const buffer = fs.readFileSync("testData/" + fileName);
33+
// Upload file to cloud storage.
34+
const uploadResponse = await api.uploadFile(folder + "/" +fileName, buffer, storage)
35+
36+
// Create TextReplace request.
37+
const textReplace = new TextReplace();
38+
// Set the text to replace.
39+
textReplace.oldValue = "Page";
40+
// Set the replacement text.
41+
textReplace.newValue = "";
42+
// Set the oldValue is regex.
43+
textReplace.regex = false;
44+
45+
// Create TextReplaceListRequest instance.
46+
const textReplaceList = new TextReplaceListRequest();
47+
// Set the text replaces list.
48+
textReplaceList.textReplaces = [textReplace];
49+
// Set the start index of text items to replace.
50+
textReplaceList.startIndex = 2;
51+
// Set the count replacements.
52+
textReplaceList.countReplace = 2;
53+
54+
// Swagger method for adding text: https://reference.aspose.cloud/pdf/#/TextReplace/PostDocumentTextReplace
55+
// Call the API to replace text in the specified PDF document.
56+
const result = await api.postDocumentTextReplace(
57+
uploadResponse.body.uploaded[0],
58+
textReplaceList,
59+
storage,
60+
folder);
61+
62+
// Log the response to console.
63+
console.log(result.body.status);
64+
}
65+
66+
// Execute the postDocumentTextReplace function
67+
postDocumentTextDelete();

useCases/textExtract.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Import necessary classes from the Aspose PDF Cloud library
2+
const fs = require("fs");
3+
const { PdfApi } = require("asposepdfcloud");
4+
5+
// The initialization assumes that the necessary credentials (Application ID and Application Key) from https://dashboard.aspose.cloud/
6+
// const api = new PdfApi("YOUR_API_SID", "YOUR_API_KEY");
7+
const api = new PdfApi("http://172.17.0.1:5000/v3.0");
8+
9+
// The document name.
10+
const fileName = "4pages.pdf";
11+
// Use default storage.
12+
const storage = null;
13+
// Set document folder.
14+
const folder = "Documents";
15+
16+
/**
17+
* Extract text of a PDF document using the Aspose.PDF Cloud API.
18+
*
19+
* This function performs the following steps:
20+
* 1. Reads a PDF file from the local file system.
21+
* 2. Uploads the PDF file to the Aspose.PDF Cloud storage.
22+
* 3. Initializing parameters for searching text on a PDF document page.
23+
* 4. Read text of PDF document using the Aspose.PDF Cloud API.
24+
* 5. Logs an extracted text.
25+
*
26+
* @returns {Promise<void>} A promise that is resolves after text extraction is complete.
27+
*/
28+
async function extractText()
29+
{
30+
// Read file from file system.
31+
const buffer = fs.readFileSync("testData/" + fileName);
32+
// Upload file to cloud storage.
33+
const uploadResponse = await api.uploadFile(folder + "/" +fileName, buffer, storage)
34+
35+
// X-coordinate of lower-left corner.
36+
const llx = 100;
37+
// Y-coordinate of lower-left corner.
38+
const lly = 700;
39+
// X-coordinate of upper-right corner.
40+
const urx = 200;
41+
// Y-coordinate of upper-right corner.
42+
const ury = 800;
43+
// List of formats to search.
44+
const format = [ ".* Page" ];
45+
// Formats are specified as a regular expression.
46+
const regex = true;
47+
// Split result fragments (default is true).
48+
const splitRects = true;
49+
50+
// Swagger method definition available at
51+
// https://reference.aspose.cloud/pdf/#/Text/GetText
52+
// Extract document text.
53+
const result = await api.getText(
54+
uploadResponse.body.uploaded[0],
55+
llx,
56+
lly,
57+
urx,
58+
ury,
59+
format,
60+
regex,
61+
splitRects,
62+
folder,
63+
storage);
64+
65+
// Log extracted text.
66+
for (const text of result.body.textOccurrences.list)
67+
{
68+
console.log(text.text);
69+
}
70+
}
71+
72+
extractText();

0 commit comments

Comments
 (0)