Other PDF Issues

How to Read PDF Text Objects by Location with Acrobat JavaScript

Aamir Naveed AkramAamir Naveed Akram Nov 2, 2026 869 views

Question details

The user needs a programmatic method to read PDF text objects and group them based on their specific physical locations on the page.

How to Read PDF Text Objects by Location Using Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Extracting text from a PDF document and organizing it into an Excel spreadsheet by sorting the text values according to their structural page coordinates.
Observed behavior
Requires a script or function to loop through PDF words, capture their text and quadrilateral coordinates, and restructure the visual layout for data export.
Before you start

Ensure you have access to the Adobe Acrobat Pro Action Wizard or JavaScript Console to test your scripts. Basic knowledge of JavaScript array sorting and multidimensional loops is recommended.

Solution 1Recommended

Extract Text and Coordinates using Acrobat JavaScript

Use Acrobat's built-in JavaScript functions to loop through pages, read words, and capture their geometric coordinates for restructuring.

Acrobat JavaScript provides specialized methods to read text by the word rather than interacting with arbitrary low-level text objects. While this might not preserve the exact raw text-object structure created by the authoring software, reading words and their quadrilateral coordinates (quads) allows you to mathematically reconstruct rows and columns for spreadsheet formats.

1
Calculate Total Words

Use the `getPageNumWords(pageNum)` method in your script to determine the total number of words on the target page.

2
Loop Through the Page

Create a loop that iterates through every word index, from zero up to the total number of words on the page.

3
Retrieve Text and Position

Inside the loop, call `getPageNthWord(pageNum, index)` to capture the actual text string, and `getPageNthWordQuads(pageNum, index)` to get its positional coordinate data.

4
Sort by Coordinates

Push the extracted text and their bounding box coordinates into an array. Group the data by Y-coordinates to form rows, and sort by X-coordinates to order the columns correctly.

5
Export the Formatted Data

Format the newly sorted array as a comma-separated values (CSV) string and output it to the console or a text file, which can then be imported into Excel.

Extract Text and Coordinates using Acrobat JavaScript
Understanding Quads: A 'quad' is an array of 8 numbers representing the X and Y coordinates of the four corners of the bounding box surrounding a specific word. You must use these values to accurately calculate the vertical and horizontal placement of the text.
Easier Alternative

Extract PDF Data to Excel Automatically with WPS Office

If your goal is simply to get PDF text into an Excel sheet while preserving its layout, you can bypass complex JavaScript coding entirely. WPS Office's built-in PDF to Excel converter automatically analyzes text locations, reconstructs the visual layout, and exports the data directly into a spreadsheet.

  1. 1. Open your PDF: Launch WPS Office and open the PDF document containing the text or tables you want to extract.
  2. 2. Use the PDF to Excel Tool: Navigate to the 'Tools' tab on the top ribbon and select the 'PDF to Excel' feature.
  3. 3. Convert and Save: Choose your conversion settings. WPS Office will automatically calculate the spatial locations of the text objects and output a perfectly formatted Excel spreadsheet.
Automatically identifies text coordinates and table structuresRequires absolutely no JavaScript or programming knowledgeFully compatible with Microsoft Excel (.xlsx) file formatsPreserves the original document layout quickly and accurately
microsoft office alternative - wps office

Frequently Asked Questions

Can Acrobat JavaScript read raw, low-level text objects instead of words?

No, Acrobat JavaScript's `getPageNthWord()` function reads text as grouped words rather than the exact underlying, low-level text objects. You must use the coordinate data (quads) to interpret how those words relate to each other visually.

What is the exact format of the coordinates returned by getPageNthWordQuads?

The function returns an array of coordinates (quadrilaterals). Each quad contains 8 numbers representing the precise X and Y coordinates for the four corners of the bounding box that surrounds the extracted word.

Why are my exported PDF words completely out of order?

PDF text objects are stored internally in the order they were created, modified, or compiled, which frequently differs from the visual reading order on the page. You must programmatically sort the extracted words by their X and Y coordinate values to successfully reconstruct the visual layout.

Can I export the extracted text directly into an Excel file using JavaScript?

Acrobat JavaScript cannot natively write directly to an .xlsx file format due to security and environment limitations. However, you can format your sorted text as a CSV or tab-delimited text string and save it locally, which Excel can open without issue.