How to Extract and Export the First Two PDF Pages to Excel with JavaScript
Question details
The user wants to isolate the first two pages of multiple PDF files and export the shortened documents to Excel using an automated JavaScript or Action workflow.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Batch processing multiple PDFs to retain only the first two pages and converting the specific retained data into Excel spreadsheets.
- Observed behavior
- Instead of manually extracting pages for each file, the user requires a script to automatically remove unwanted pages and trigger the Excel export command.
Before running automated JavaScript on your PDF files, ensure you have a backup of the original documents in a separate folder, as automated page deletion scripts can permanently modify files if saved accidentally.
Use Acrobat JavaScript to Delete Unwanted Pages and Export
Instead of creating a temporary extracted file, you can write a script to delete all pages after the second page and then save the file directly in Excel format.
In Acrobat, it is often more efficient to delete the unwanted pages from the current document rather than spawning a new document and copying pages over. Once the extra pages are deleted, you can call the SaveAs function to export the remaining document as an Excel file.
In Adobe Acrobat Pro, navigate to 'Tools' and select 'Action Wizard'. Click on 'New Action' to create a new batch process.
Under the 'Choose tools to add' panel, find 'More Tools' and click 'Execute JavaScript'. Add it to your Action steps.
Open the JavaScript editor and paste the following code to delete pages 3 and beyond: if (this.numPages > 2) { this.deletePages({nStart: 2, nEnd: this.numPages - 1}); this.dirty = false; }
Below the deletion code, add the command to save the file as an Excel spreadsheet: this.saveAs(this.path.replace(".pdf", ".xlsx"), "com.adobe.acrobat.xlsx");
Save the Action, select the folder containing your target PDFs, and run the Action to process the files in bulk.

Extract PDF Pages and Convert to Excel Easily with WPS Office
Skip the complex JavaScript coding. WPS Office provides a user-friendly interface to instantly extract specific PDF pages and convert them directly to Microsoft Excel format without needing any programming knowledge.
- 1. Open your PDF: Launch WPS Office and open the PDF document you want to convert.
- 2. Select PDF to Excel: Navigate to the 'Tools' tab on the top ribbon and select 'PDF to Excel'.
- 3. Set the page range: In the conversion dialog box, specify the 'Page Range' as '1-2' to only process the first two pages.
- 4. Convert and save: Click the 'Convert' button. WPS Office will instantly generate an Excel file containing only the data from the first two pages.

Frequently Asked Questions
Can I use this JavaScript in free PDF readers?
No, executing advanced document-modifying JavaScript and utilizing batch Actions typically requires a premium PDF editor like Adobe Acrobat Pro.
Why delete pages instead of extracting them in the script?
Deleting unwanted pages from the current document and exporting the result is computationally simpler in Acrobat JavaScript than creating a new blank document and moving pages over one by one.
How do I prevent the script from saving over my original PDF?
You can set 'this.dirty = false;' in your JavaScript right after the page deletion. This tricks the application into thinking the document hasn't been modified, allowing you to close it safely without overwriting the original file.




