How to Extract PDF Pages with Specific Comments Using JavaScript
Question details
The user needs to extract only the PDF pages that contain a specific type of annotation, such as text-box comments, using JavaScript instead of extracting all pages with any comment.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Extracting specific pages from a large PDF document based on the types of comments they contain for streamlined review and sharing.
- Observed behavior
- Basic scripts extract every page containing any comment, lacking the ability to filter by annotation subtype, which leads to unwanted pages being included in the output.
Ensure you have a backup copy of your original PDF document before running any custom JavaScript, as scripting actions can permanently alter the file structure or page order.
Use a JavaScript Loop to Filter Annotations by Type
Write a custom JavaScript to iterate through pages, retrieve the annotation array, check for the specific annotation type, and extract matching pages.
To selectively extract pages, a simple condition check is insufficient. You must retrieve the annotations array for each page using the getAnnots method and loop through it. By evaluating the annotation type or subtype (such as 'FreeText' for text boxes), you can identify which pages meet your exact criteria.
Open your PDF editor's Action Wizard or JavaScript debugging console to begin creating your custom script.
Set up a for-loop to iterate through all the pages in the document using the document's total page count property.
For each page index, call the getAnnots method to retrieve the oAnnots array, which contains all annotations present on that specific page.
Create an inner loop to examine the type property of each annotation in the array. If it matches your target type, such as 'FreeText' for text boxes, store the page index for extraction.
Execute the extraction command for the identified page and use a break statement to exit the inner loop immediately, preventing duplicate extractions of the same page.

Use Pre-built Action Scripts
Download and import a community-created Action script if you prefer not to write the JavaScript array logic from scratch.
Easily Manage and Extract PDF Pages in WPS Office
If writing JavaScript feels too complex, WPS Office provides an intuitive user interface to view all comments, navigate to specific annotations, and extract the exact pages you need without any coding required.
- 1. Open the Comment Pane: Open your PDF file in WPS Office, navigate to the Comment tab, and open the Comment List pane on the left side.
- 2. Identify Target Pages: Scroll through the list of annotations to visually identify the pages containing the specific text-box comments you need to extract.
- 3. Access Page Extraction: Navigate to the Page tab on the top ribbon and select the Extract Page tool.
- 4. Extract and Save: Enter the page numbers you identified in the comment pane and click Extract to seamlessly save those specific pages as a new PDF document.

Frequently Asked Questions
Can I filter annotations by author instead of just comment type?
Yes, when looping through the annotation array with JavaScript, you can check the author property of each annotation instead of or in addition to the type property. This allows you to extract pages based on who made the comment.
What is the JavaScript property name for a text box comment?
In standard PDF JavaScript APIs, a text box comment is typically identified by the annotation type property returning the exact string value 'FreeText'.
Will the extracted pages keep the original comments attached?
Yes, when you extract pages using standard PDF software or JavaScript page extraction methods, the annotations remain fully embedded and editable on those extracted pages.




