Skip to content

Commit 041cbe3

Browse files
Aspose.PDF for JavaScript/Node.js via C++: AsposePdfReplaceTextEx
1 parent 51e0046 commit 041cbe3

6 files changed

Lines changed: 169 additions & 0 deletions

File tree

english/javascript-cpp/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Such operations are very time consuming, so we recommend using Web Worker.
100100
| [AsposePdfCropPages](./organize/asposepdfcroppages/) | Crop PDF-pages. |
101101
| [AsposePdfDeleteTextHeaders](./organize/asposepdfdeletetextheaders/) | Delete text headers from a PDF-file. |
102102
| [AsposePdfDeleteTextFooters](./organize/asposepdfdeletetextfooters/) | Delete text footers from a PDF-file. |
103+
| [AsposePdfReplaceTextEx](./organize/asposepdfreplacetextex/) | Replace text in a PDF-file with alignment control. |
103104

104105

105106
## Metadata PDF functions

english/javascript-cpp/organize/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ url: /javascript-cpp/organize/
5252
| [AsposePdfCropPages](./asposepdfcroppages/) | Crop PDF-pages. |
5353
| [AsposePdfDeleteTextHeaders](./asposepdfdeletetextheaders/) | Delete text headers from a PDF-file. |
5454
| [AsposePdfDeleteTextFooters](./asposepdfdeletetextfooters/) | Delete text footers from a PDF-file. |
55+
| [AsposePdfReplaceTextEx](./asposepdfreplacetextex/) | Replace text in a PDF-file with alignment control. |
5556

5657

5758
## Detailed Description
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: "AsposePdfReplaceTextEx"
3+
second_title: Aspose.PDF for JavaScript via C++
4+
description: "Replace text in a PDF-file with alignment control."
5+
type: docs
6+
url: /javascript-cpp/organize/asposepdfreplacetextex/
7+
---
8+
9+
_Replace text in a PDF-file with alignment control._
10+
11+
```js
12+
function AsposePdfReplaceTextEx(
13+
fileBlob,
14+
fileName,
15+
findText,
16+
replaceText,
17+
options,
18+
fileNameResult
19+
)
20+
```
21+
22+
**Parameters**:
23+
24+
* **fileBlob** Blob object
25+
* **fileName** file name
26+
* **findText** text fragment to search
27+
* **replaceText** text fragment to replace
28+
* **options** object, settings for text replacement:
29+
* `alignment` (string), text alignment (e.g., "left", "right", "auto")
30+
* `numPages` (string|number|Array), target pages to process:
31+
* number, page number as 2
32+
* string, include page numbers with intervals as "7, 20, 22, 30-32, 33, 36-40, 46"
33+
* Array, array of page numbers, such as [1,3]
34+
Pass an empty object `{}` for default behavior
35+
* **fileNameResult** result file name
36+
37+
**Return**:
38+
39+
JSON object
40+
41+
* **errorCode** - code error (0 no error)
42+
* **errorText** - text error ("" no error)
43+
* **fileNameResult** - result file name
44+
45+
46+
**Web Worker example**:
47+
```js
48+
/*Create Web Worker*/
49+
const AsposePDFWebWorker = new Worker("AsposePDFforJS.js");
50+
AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
51+
AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent =
52+
(evt.data == 'ready') ? 'loaded!' :
53+
(evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "application/pdf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
54+
55+
/*Event handler*/
56+
const ffileReplaceTextEx = e => {
57+
const file_reader = new FileReader();
58+
file_reader.onload = event => {
59+
const findText = 'Aspose';
60+
const replaceText = 'ASPOSE';
61+
const optionsText = {numPages: 1, alignment: "auto"};
62+
/*Replace text in a PDF-file with alignment control and save the "ResultReplaceTextEx.pdf" - Ask Web Worker*/
63+
AsposePDFWebWorker.postMessage({ "operation": 'AsposePdfReplaceTextEx', "params": [event.target.result, e.target.files[0].name, findText, replaceText, optionsText, "ResultReplaceTextEx.pdf"] }, [event.target.result]);
64+
};
65+
file_reader.readAsArrayBuffer(e.target.files[0]);
66+
};
67+
68+
/*Make a link to download the result file*/
69+
const DownloadFile = (filename, mime, content) => {
70+
mime = mime || "application/octet-stream";
71+
var link = document.createElement("a");
72+
link.href = URL.createObjectURL(new Blob([content], {type: mime}));
73+
link.download = filename;
74+
link.innerHTML = "Click here to download the file " + filename;
75+
document.body.appendChild(link);
76+
document.body.appendChild(document.createElement("br"));
77+
return filename;
78+
}
79+
```
80+
**Simple example**:
81+
```js
82+
var ffileReplaceTextEx = function (e) {
83+
const file_reader = new FileReader();
84+
file_reader.onload = (event) => {
85+
/*Replace text in a PDF-file with alignment control and save the "ResultReplaceTextEx.pdf"*/
86+
const json = AsposePdfReplaceTextEx(event.target.result, e.target.files[0].name, "Aspose", "ASPOSE", {alignment: "left"}, "ResultReplaceTextEx.pdf");
87+
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult
88+
else document.getElementById('output').textContent = json.errorText;
89+
/*Make a link to download the result file*/
90+
DownloadFile(json.fileNameResult, "application/pdf");
91+
};
92+
file_reader.readAsArrayBuffer(e.target.files[0]);
93+
};
94+
```

english/nodejs-cpp/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ is_root: true
9999
| [AsposePdfCropPages](./organize/asposepdfcroppages/) | Crop PDF-pages. |
100100
| [AsposePdfDeleteTextHeaders](./organize/asposepdfdeletetextheaders/) | Delete text headers from a PDF-file. |
101101
| [AsposePdfDeleteTextFooters](./organize/asposepdfdeletetextfooters/) | Delete text footers from a PDF-file. |
102+
| [AsposePdfReplaceTextEx](./organize/asposepdfreplacetextex/) | Replace text in a PDF-file with alignment control. |
102103

103104

104105
## Metadata PDF functions

english/nodejs-cpp/organize/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ url: /nodejs-cpp/organize/
5252
| [AsposePdfCropPages](./asposepdfcroppages/) | Crop PDF-pages. |
5353
| [AsposePdfDeleteTextHeaders](./asposepdfdeletetextheaders/) | Delete text headers from a PDF-file. |
5454
| [AsposePdfDeleteTextFooters](./asposepdfdeletetextfooters/) | Delete text footers from a PDF-file. |
55+
| [AsposePdfReplaceTextEx](./asposepdfreplacetextex/) | Replace text in a PDF-file with alignment control. |
5556

5657

5758
## Detailed Description
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: "AsposePdfReplaceTextEx"
3+
second_title: Aspose.PDF for Node.js via C++
4+
description: "Replace text in a PDF-file with alignment control."
5+
type: docs
6+
url: /nodejs-cpp/organize/asposepdfreplacetextex/
7+
---
8+
9+
_Replace text in a PDF-file with alignment control._
10+
11+
```js
12+
function AsposePdfReplaceTextEx(
13+
fileName,
14+
findText,
15+
replaceText,
16+
options,
17+
fileNameResult
18+
)
19+
```
20+
21+
**Parameters**:
22+
23+
* **fileName** file name
24+
* **findText** text fragment to search
25+
* **replaceText** text fragment to replace
26+
* **options** object, settings for text replacement:
27+
* `alignment` (string), text alignment (e.g., "left", "right", "auto")
28+
* `numPages` (string|number|Array), target pages to process:
29+
* number, page number as 2
30+
* string, include page numbers with intervals as "7, 20, 22, 30-32, 33, 36-40, 46"
31+
* Array, array of page numbers, such as [1,3]
32+
Pass an empty object `{}` for default behavior
33+
* **fileNameResult** result file name
34+
35+
**Return**:
36+
37+
JSON object
38+
39+
* **errorCode** - code error (0 no error)
40+
* **errorText** - text error ("" no error)
41+
* **fileNameResult** - result file name
42+
43+
44+
**CommonJS**:
45+
46+
```js
47+
const AsposePdf = require('asposepdfnodejs');
48+
const pdf_file = 'Aspose.pdf';
49+
AsposePdf().then(AsposePdfModule => {
50+
const findText = 'Aspose';
51+
const replaceText = 'ASPOSE';
52+
const optionsText = {numPages: 1, alignment: "auto"};
53+
/*Replace text in a PDF-file with alignment control and save the "ResultPdfReplaceTextEx.pdf"*/
54+
const json = AsposePdfModule.AsposePdfReplaceTextEx(pdf_file, findText, replaceText, optionsText, "ResultPdfReplaceTextEx.pdf");
55+
console.log("AsposePdfReplaceTextEx => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
56+
});
57+
```
58+
59+
**ECMAScript/ES6**:
60+
61+
```js
62+
import AsposePdf from 'asposepdfnodejs';
63+
const AsposePdfModule = await AsposePdf();
64+
const pdf_file = 'Aspose.pdf';
65+
const findText = 'Aspose';
66+
const replaceText = 'ASPOSE';
67+
const optionsText = {numPages: 1, alignment: "auto"};
68+
/*Replace text in a PDF-file with alignment control and save the "ResultPdfReplaceTextEx.pdf"*/
69+
const json = AsposePdfModule.AsposePdfReplaceTextEx(pdf_file, findText, replaceText, optionsText, "ResultPdfReplaceTextEx.pdf");
70+
console.log("AsposePdfReplaceTextEx => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
71+
```

0 commit comments

Comments
 (0)