|
| 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 | +``` |
0 commit comments