PDF Editing Issues

How to Extract PDF Pages with Specific Comments Using JavaScript

Maira MehtabMaira Mehtab Nov 2, 2026 869 views

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.

Extracting PDF Pages Containing Specific Comment Types via Script
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.
Before you start

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.

Solution 1Recommended

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.

1
Access the JavaScript Console

Open your PDF editor's Action Wizard or JavaScript debugging console to begin creating your custom script.

2
Loop Through Document Pages

Set up a for-loop to iterate through all the pages in the document using the document's total page count property.

3
Retrieve Page Annotations

For each page index, call the getAnnots method to retrieve the oAnnots array, which contains all annotations present on that specific page.

4
Examine Annotation Types

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.

5
Extract and Break Loop

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 a JavaScript Loop to Filter Annotations by Type
Script Optimization: Always break the inner loop as soon as a matching comment is found. This significantly speeds up the script's execution time on large PDF files.

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. 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. 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. 3. Access Page Extraction: Navigate to the Page tab on the top ribbon and select the Extract Page tool.
  4. 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.
Visual comment pane to quickly filter and locate text-box annotationsSeamless page extraction tool for fast document splittingFully compatible with standard PDF files and Microsoft Office formats
microsoft office alternative - wps office

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.