How to Automatically Crop Scanned PDF Images Using JavaScript
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.

- 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 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.
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.
Write a JavaScript loop that iterates through all document pages using the 'this.numPages' property.
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.
Set the field's 'fillColor' to 'color.white' (or match your page background), and ensure it is set to be read-only and printable.
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 Batch Page Cropping Tools
Instead of cropping the image itself via scripts, you can crop the PDF page bounding boxes across the entire document using built-in batch tools.
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. Open your PDF: Launch WPS Office and open your scanned PDF document.
- 2. Select the Crop Tool: Go to the 'Page' tab on the top ribbon and click on the 'Crop Page' option.
- 3. Adjust margins: Drag the edges of the crop box to remove the unwanted one-inch margin around your scanned image.
- 4. Apply globally: Select the option to apply this crop setting to all pages, then save the customized document.

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.




