How to Extract Data from a PDF Based on Keywords Using PDF.co Document Parser

PDF.co Document Parser can locate keywords in PDFs and extract the values associated with them. This is useful when values may move between documents but remain identifiable by labels such as:

  • Invoice Number
  • Invoice Date
  • Account Number
  • Customer Name
  • Amount Due
  • Total

In this tutorial, you will create a reusable Document Parser template that searches for keywords and returns the associated values as structured data.

Document Parser supports native PDFs, scanned PDFs, and JPG, PNG, and TIFF images.

Choosing the correct extraction method: Use Field Based on Text Search when a keyword identifies the value and its position may change. Use Field from Rectangle Selection only when the value consistently appears in the same location.

Step 1: Open Document Parser

Sign in to your PDF.co account.

Open Document Parser from the PDF.co dashboard, and then open the template-management area.

Choose the option to create a new Document Parser template.

Step 2: Load a Test Document

In the Document Parser Template Editor, load a representative PDF or image.

Choose a sample containing the same keywords and values that will appear in the documents you plan to process.

For example, an invoice might contain:

Invoice Number: 1234567
Invoice Date: August 17, 2026
Customer Name: Acme Corporation
Total: $1,287.00

The exact position of these values may vary, but the identifying keywords remain consistent.

For scanned documents, select the appropriate OCR language when that option is available. Clear, high-resolution source files produce more reliable results.

Step 3: Name and Configure the Template

Enter a descriptive template name, such as:

Keyword-Based Invoice Extraction

If applicable, select the document’s language or culture. The culture setting helps PDF.co interpret dates, decimal separators, and currency formats correctly.

The template will contain one extraction object for each value you want to return.

Step 4: Add a Text-Search Field

Add a new template object and choose:

Field Based on Text Search

Give the field a clear name. For example:

invoice_number

Enter an expression that identifies the keyword and captures its corresponding value.

For an invoice number, an expression could be:

Invoice Number: ({{Digits}})

Enable regular-expression processing if required by the field configuration.

This expression tells Document Parser to locate the words Invoice Number and return the digits that follow them.

Run the template and confirm that the result contains only the invoice number rather than the complete label and value.

Step 5: Extract a Date

Add another Field Based on Text Search object.

Name the field:

invoice_date

Use an expression such as:

Invoice Date: ({{SmartDate}})

Set the data type to Date when applicable.

The {{SmartDate}} macro recognizes many commonly used date formats. Setting a date data type also allows the parser to return the value in a standardized format.

Run the template and verify that the correct date is extracted.

Step 6: Extract a Monetary Amount

Add another text-search field and name it:

total

Use an expression such as:

Total: {{Dollar}}({{Number}})

Set the field’s data type to Decimal.

The {{Dollar}} macro locates the dollar sign, while {{Number}} captures the numeric amount.

If the documents use multiple currencies, use a broader currency macro or create a separate field for the currency.

Step 7: Extract General Text

Text values such as a customer or company name may require a more flexible expression.

Create a field named:

customer_name

An example expression is:

Customer Name: ({{SentenceWithSingleSpaces}})

This searches for the Customer Name label and returns the text that follows it.

If the value can contain multiple spaces, line breaks, or unusual characters, adjust the expression and test it with several representative documents.

Step 8: Use Value Markers When Needed

For more precise extraction, regular expressions can use PDF.co’s ?<value> marker to identify the part of the match that should become the final output.

For example:

Invoice Number: ?<value>(\d+)

With regular-expression processing enabled, this expression searches for the complete label-and-value pattern but returns only the digits.

PDF.co also supports a ?<key> marker when both field names and values must be generated dynamically from key-value pairs.

Use these advanced markers when a simple macro expression does not isolate the required value.

Step 9: Add the Remaining Fields

Repeat the process for each keyword you need to extract.

A typical invoice template might contain:

  • invoice_number
  • invoice_date
  • purchase_order_number
  • customer_name
  • subtotal
  • tax
  • total
  • payment_due_date

Use descriptive field names without spaces. These names will appear in the structured output and may be referenced by integrations, scripts, databases, or automation platforms.

Step 10: Test the Template

Run the template against the loaded test document.

Review every extracted value and confirm that:

  • The correct keyword was located.
  • Only the intended value was returned.
  • Dates and numbers use the appropriate data types.
  • Labels are not included unless they are required.
  • Values are extracted even when their positions change.
  • Fields do not accidentally match unrelated text.

Test the template with multiple representative documents. One sample may not reveal differences such as:

  • Longer customer names
  • Different date formats
  • Alternate currency formatting
  • Keywords that appear more than once
  • Values wrapped across multiple lines
  • Scanned documents with lower OCR quality

Refine the expressions until the template produces consistent results.

Step 11: Save the Template

When the results are correct, save the template.

Return to the template list and locate the saved template. Record its template ID because it will be required when processing documents through the PDF.co API or an automation integration.

If the source-document wording changes later, update and retest the template.

Step 12: Test the Template with the PDF.co API Tester

Open the PDF.co API Tester and select the Document Parser endpoint:

POST /v1/pdf/documentparser

Provide a direct URL to the source PDF or upload the test file to obtain an accessible URL.

Enter a request similar to:

{
  "url": "https://example.com/sample-invoice.pdf",
  "templateId": 123,
  "async": false,
  "password": ""
}

Replace 123 with the ID of your saved Document Parser template.

If the PDF is password-protected, enter its password. Otherwise, leave the value empty.

Run the request.

For large or long-running documents, use asynchronous processing and retrieve the completed result with the Background Job Check endpoint.

Step 13: Review the JSON Result

A successful response contains the objects extracted by the template.

A simplified result might look like this:

{
  "body": {
    "templateName": "Keyword-Based Invoice Extraction",
    "objects": [
      {
        "name": "invoice_number",
        "objectType": "field",
        "value": "1234567",
        "pageIndex": 0
      },
      {
        "name": "invoice_date",
        "objectType": "field",
        "value": "2026-08-17T00:00:00",
        "pageIndex": 0
      },
      {
        "name": "customer_name",
        "objectType": "field",
        "value": "Acme Corporation",
        "pageIndex": 0
      },
      {
        "name": "total",
        "objectType": "field",
        "value": 1287.00,
        "pageIndex": 0
      }
    ]
  },
  "error": false,
  "status": 200
}

Each object includes the field name assigned in the template and the extracted value. Depending on the template and response, additional information such as the page index or extraction rectangle may also be included.

The structured data can then be sent to:

  • A spreadsheet
  • A database
  • Accounting software
  • A CRM
  • An approval workflow
  • Another API or application

Keyword Search Versus Rectangle Extraction

Choose the extraction method according to the source documents.

Use Field Based on Text Search when:

  • A consistent keyword identifies the value.
  • The field’s position may move.
  • Documents contain different amounts of preceding content.
  • Values need to be recognized by patterns such as dates or currency.

Use Field from Rectangle Selection when:

  • The document layout is fixed.
  • The value always appears in the same coordinates.
  • The field does not have a reliable identifying label.

A template can combine both methods when different fields require different strategies.

Troubleshooting

The Result Includes the Keyword

Adjust the expression so that only the value is captured. Use a capturing group or the ?<value> marker.

The Wrong Occurrence Is Returned

Make the expression more specific by including surrounding text, line boundaries, or a more precise keyword.

A Number Is Returned as Text

Set the field’s data type to Integer or Decimal, depending on the expected value.

A Date Is Not Recognized

Confirm the template culture and use a suitable date macro, such as:

{{SmartDate}}

No Text Is Found in a Scanned PDF

Confirm that OCR is enabled and that the correct OCR language is selected. Test with a clearer or higher-resolution scan if recognition remains unreliable.

The Template Works with Only One Document

Test several representative files and avoid relying on fixed coordinates for fields that move. Expand or refine the keyword expressions to accommodate realistic variations.

You have created a PDF.co Document Parser template that extracts structured values from PDFs based on identifying keywords.

Unlike rectangle-based extraction, keyword-based text search can continue locating values when their positions move within the document. Macros, regular expressions, value markers, and data types can make the extraction rules more precise and reliable.

After testing the template with several representative documents, you can use its template ID with the PDF.co API or supported automation integrations to process documents automatically.

Related Tutorials

See Related Tutorials