PDF Editing Issues

How to Automatically Crop Scanned PDF Images Using JavaScript

Huda QurayshiHuda Qurayshi Nov 3, 2026 869 views

Question details

The user wants to automate the process of cropping approximately one inch from scanned images on each PDF page using JavaScript or an automated action.

How to Automatically Crop Scanned PDF Images Using JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Batch processing a scanned PDF document to remove unwanted margins or edges from the images contained on each page.
Observed behavior
Attempting to find a JavaScript method to set image clipping regions or crop page images, which is not natively supported by standard PDF JavaScript APIs.
Before you start

Before modifying page margins or flattening objects in your PDF, save a copy of the original scanned document, as these layout changes cannot be easily reversed.

Solution 1Recommended

Use Opaque Fields as a JavaScript Workaround

Since JavaScript cannot directly set clipping regions on images, you can use a script to place opaque form fields or buttons over the unwanted margins and then flatten the document.

Acrobat JavaScript API lacks a direct function to crop image objects or define clipping regions. The most viable programmatic workaround is to visually obscure the unwanted portions.

By dynamically generating opaque annotations (like buttons with no borders and a solid background color) over the one-inch margin area on every page, you simulate a cropped appearance.

1
Create the automation script

Write a JavaScript loop that iterates through all document pages using the 'this.numPages' property.

2
Add the opaque field

Inside the loop, use 'this.addField()' to create a button on each page. Set the field's rectangle coordinates to cover the specific one-inch margin you wish to hide.

3
Style the field

Set the field's 'fillColor' to 'color.white' (or match your page background), and ensure it is set to be read-only and printable.

4
Flatten the document

Execute a document flattening command (e.g., 'this.flattenPages()') to permanently merge the opaque covers with the scanned images, preventing them from being moved or deleted.

Use Opaque Fields as a JavaScript Workaround
Important: This method hides the unwanted area rather than deleting the image data. The hidden parts will still exist in the file's underlying code unless you use a redaction tool.
Effortless PDF Cropping

Easily Crop Multi-Page PDFs with WPS Office

Bypass complex JavaScript workarounds. WPS Office provides a highly intuitive visual tool to instantly crop margins on scanned PDFs and apply those adjustments across all pages in a few clicks.

  1. 1. Open your PDF: Launch WPS Office and open your scanned PDF document.
  2. 2. Select the Crop Tool: Go to the 'Page' tab on the top ribbon and click on the 'Crop Page' option.
  3. 3. Adjust margins: Drag the edges of the crop box to remove the unwanted one-inch margin around your scanned image.
  4. 4. Apply globally: Select the option to apply this crop setting to all pages, then save the customized document.
Visual drag-and-drop page crop toolApply uniform crop margins to all pages instantlySeamlessly view and manage scanned PDF documentsHigh compatibility with all standard PDF formatsLightweight, fast, and completely free to download
microsoft office alternative - wps office

Frequently Asked Questions

Can Acrobat JavaScript directly edit images inside a PDF?

No, standard Acrobat JavaScript API is designed for form manipulation and document-level actions. It does not contain methods for direct image editing, cropping, or setting clipping paths on embedded images.

Does hiding content with an opaque box reduce PDF file size?

No. Placing an opaque field over an image merely covers it visually. The original image data remains intact within the file structure. To reduce file size, you must crop the image externally or use redaction and optimization tools.

How do I ensure the opaque covers cannot be removed?

After applying the opaque boxes or buttons via JavaScript, you must flatten the PDF. Flattening converts interactive elements like annotations and form fields into static page content.

Is it possible to automate the Crop Pages tool without scripting?

Yes, many advanced PDF editors feature an 'Action Wizard' or batch processing module where you can define a standard set of crop margins and automatically apply it to a whole folder of scanned PDFs.