Link Search Menu Expand Document

PDF Search and Replace Text

Explore Samples

Description: Modify a PDF file by searching for specific text and replacing it with new text.

Note: PDF.co /pdf/edit/replace-text endpoint has certain limitations and restrictions. For adding fully-featured text, we recommend using the PDF.co pdf/edit/add endpoint, For more information visit PDF Add Text, Signatures and Images to PDF. Additionally, you can utilize the PDF.co Edit Add Helper to generate a configuration for filling out PDFs.

Status Errors

CodeDescription
200The request has succeeded
400bad input parameters
401unauthorized
403not enough credits
405Timeout error. To process large documents or files please use asynchronous mode ( set the async parameter to true) and then check the status using the /job/check endpoint. If a file contains many pages then specify a page range using the pages parameter. The number of pages of the document can be obtained using the endpoint /pdf/info

Example 1: single text replacement

GET

https://api.pdf.co/v1/pdf/edit/replace-text?url=pdfco-test-files.s3.us-west-2.amazonaws.compdf-split/sample.pdf&searchString=conspicuous&replaceString=replaced&x-api-key=YOUR-API-KEY

or

POST

{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-split/sample.pdf",
  "searchString": "conspicuous feature",
  "replaceString": "Replaced1",
  "caseSensitive": true,
  "name": "finalFile"
}

or

cURL (uploading your file along with the request)

curl -F "x-api-key=PASTE_YOUR_API_KEY_HERE" -F "searchString=conspicuous feature" -F "replaceString=Replaced1"  -F "file=@./sample.pdf" https://api.pdf.co/v1/pdf/edit/replace-text

Example 2: batch texts replacement

Sample Request:

! Don’t forget to set x-api-key url param or http header param (preferred) to API key, get yours here

POST

{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-split/sample.pdf",
  "searchStrings": [
    "conspicuous feature",
    "Martian"
  ],
  "replaceStrings": [
    "Replaced1",
    "Replaced2"
  ],
  "caseSensitive": true,
  "name": "finalFile"
}

Available Methods

[POST] /pdf/edit/replace-text (multiple replacements)

Attributes
url required
URL to the source file. Supports links from Google Drive, Dropbox, and PDF.co built-in files storage. To upload files via API, Check out the Files Upload section.

Note: If you experience intermittent Too Many Requests or Access Denied errors, please try to add cache: to enable built-in URL caching. (e.g cache:https://example.com/file1.pdf)

For data security, you have the option to encrypt output files and decrypt input files. Learn more about user-controlled data encryption.
httpusername optional
HTTP auth user name if required to access source url.
httppassword optional
HTTP auth password if required to access source url.
searchStrings[] required
The array of strings to search.
replaceStrings[] required
The array of replacements strings.
replacementLimit optional
Limit the number of searches & replacements for every item. The default value is 0 which means unlimited searches and replacements so every found occurrence will be replaced.
caseSensitive optional
Set to false to use case-insensitive search.
regex optional
Set to true to use regular expression for a search string(s).
name optional
File name for the generated output, The input must be in string format.
expiration optional
Set the expiration time for the output link in minutes (default is 60 i.e 60 minutes or 1 hour), After this specified duration, any generated output file(s) will be automatically deleted from PDF.co temporary files storage. The maximum duration for link expiration varies based on your current subscription plan. Learn more

To store permanent input files (e.g. re-usable images, pdf templates, documents), Consider using PDF.co built-in Files Storage.
password optional
The password of the PDF file, The input must be in string format.
pages optional
Comma-separated list of page indices (or ranges) to process.
IMPORTANT: The very first page starts at 0 (zero). To set a range use the dash -, for example: 0,2-5,7-. To set a range from the index to the last page use range like this: 2- (from page #3 as the index starts at zero and till the of the document). For ALL pages just leave this param empty.
Example: 0,2-5,7- means first page, then 3rd page to 6th page, and then the range from 8th (index = 7) page till the end of the document, The input must be in string format.n
async optional
Set async to true for long processes to run in the background, API will then return a jobId which you can use with /job/check endpoint to check the status of the process and retrieve the output while you can proceed with other tasks without waiting for this process to finish.
IMPORTANT: Also set the inline param to true to get a direct link to the final output pdf in both sync and async modes. Otherwise, you will be getting a direct link to pdf in sync mode but also a link to the .json file in the async mode.
  • Method: POST
  • URL: /v1/pdf/edit/replace-text

Query parameters

No query parameters accepted.

Body payload

{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-search-and-replace/sample-agreement-template-signature-page-1.pdf",
  "searchStrings": [
    "[CLIENT-NAME]",
    "[CLIENT-COMPANY]"
  ],
  "replaceStrings": [
    "John Doe",
    "Skynet 3000"
  ],
  "caseSensitive": true,
  "replacementLimit": 1,
  "pages": "",
  "password":"",
  "name": "finalFile",
  "async": false
}

Example responses

/pdf/edit/replace-text (multiple replacements)
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/e79f0b9c82984740973ca670d7c93cad/finalFile.pdf",
    "pageCount": 1,
    "error": false,
    "status": 200,
    "name": "finalFile.pdf",
    "remainingCredits": 99089875,
    "credits": 21
}

Code Snippet

CURL
curl --location --request POST 'https://api.pdf.co/v1/pdf/edit/replace-text' \
--header 'Content-Type: application/json' \
--header 'x-api-key: ' \
--data-raw '{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-search-and-replace/sample-agreement-template-signature-page-1.pdf",
  "searchStrings": [
    "[CLIENT-NAME]",
    "[CLIENT-COMPANY]"
  ],
  "replaceStrings": [
    "John Doe",
    "Skynet 3000"
  ],
  "caseSensitive": true,
  "replacementLimit": 1,
  "pages": "",
  "password":"",
  "name": "finalFile",
  "async": false
}'

[POST] /pdf/edit/replace-text (single replacement)

Attributes
url required
URL to the source file. Supports links from Google Drive, Dropbox, and PDF.co built-in files storage. To upload files via API, Check out the Files Upload section.

Note: If you experience intermittent Too Many Requests or Access Denied errors, please try to add cache: to enable built-in URL caching. (e.g cache:https://example.com/file1.pdf)

For data security, you have the option to encrypt output files and decrypt input files. Learn more about user-controlled data encryption.
httpusername optional
HTTP auth user name if required to access source url.
httppassword optional
HTTP auth password if required to access source url.
searchStrings[] required
The array of strings to search.
replaceStrings[] required
The array of replacements strings.
caseSensitive optional
Set to false to use case-insensitive search.
regex optional
Set to true to use regular expression for a search string(s).
replacementLimit optional Limit the number of searches & replacements for every item. default is 0 which means unlimited searches and replacements so every found occurrence will be replaced.
name optional
The file name for the generated output, The input must be in string format.
expiration optional
Set the expiration time for the output link in minutes (default is 60 i.e 60 minutes or 1 hour), After this specified duration, any generated output file(s) will be automatically deleted from PDF.co temporary files storage. The maximum duration for link expiration varies based on your current subscription plan. Learn more

To store permanent input files (e.g. re-usable images, pdf templates, documents), Consider using PDF.co built-in Files Storage.
password optional
Password of PDF file, The input must be in string format.
pages optional
Comma-separated list of page indices (or ranges) to process.
IMPORTANT: The very first page starts at 0 (zero). To set a range use the dash -, for example: 0,2-5,7-. To set a range from the index to the last page use range like this: 2- (from page #3 as the index starts at zero and till the of the document). For ALL pages just leave this param empty.
Example: 0,2-5,7- means first page, then 3rd page to 6th page, and then the range from 8th (index = 7) page till the end of the document, The input must be in string format.
async optional
Set async to true for long processes to run in the background, API will then return a jobId which you can use with /job/check endpoint to check the status of the process and retrieve the output while you can proceed with other tasks without waiting for this process to finish.
IMPORTANT: Also set the inline param to true to get a direct link to the final output pdf in both sync and async modes. Otherwise, you will be getting a direct link to pdf in sync mode but also a link to the .json file in the async mode.
  • Method: POST
  • URL: /v1/pdf/edit/replace-text

Query parameters

No query parameters accepted.

Body payload

{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-search-and-replace/sample-agreement-template-signature-page-1.pdf",
  "searchString": "[CLIENT-NAME]",
  "replaceString": "John Doe",
  "caseSensitive": true,
  "replacementLimit": 1,
  "pages": "",
  "password":"",
  "name": "finalFile",
  "async": false
}

Example responses

/pdf/edit/replace-text
{
    "url": "https://pdf-temp-files.s3.amazonaws.com/1a0e9a51e2d04fe4bcce0a6a31c365a8/finalFile.pdf",
    "pageCount": 4,
    "error": false,
    "status": 200,
    "name": "finalFile",
    "remainingCredits": 98310
}

Code Snippet

CURL
curl --location --request POST 'https://api.pdf.co/v1/pdf/edit/replace-text' \
--header 'x-api-key: ' \
--data-raw '{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-search-and-replace/sample-agreement-template-signature-page-1.pdf",
  "searchString": "[CLIENT-NAME]",
  "replaceString": "John Doe",
  "caseSensitive": true,
  "replacementLimit": 1,
  "pages": "",
  "password":"",
  "name": "finalFile",
  "async": false
}'

Samples