> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/salesforce/page-manipulation/crop.md).

# Cropping PDF pages using JavaScript

Learn how to crop PDF pages with the cropPages function. Pass an array of pages & specify the amount to crop from each side based on document rotation. Click to optimize your PDFs now! Salesforce WebV

PDF pages can be cropped using the [cropPages](https://sdk.apryse.com/api/web/Core.Document.html#cropPages) function. Simply pass in an array of pages to crop and the amount to be cropped from each side. One thing to note is that sides are based on the document rotation instead of the UI or total rotation.

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const docViewer = instance.Core.documentViewer;
const rotation = instance.Core.PageRotation;

docViewer.addEventListener('documentLoaded', async () => {
  const doc = docViewer.getDocument();
  const cropTop = 100, cropLeft = 0, cropRight = 0, cropBottom = 0;
  const page1 = 1, page2 = 2, page3 = 3;

  doc.getPageInfo(page1); // {width: 612, height: 792}

  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight)
  doc.getPageInfo(page1); // {width: 612, height: 692}

  await doc.rotatePages([page1], rotation.E_90);
  docViewer.rotateCounterClockwise(page1);
  doc.getPageInfo(page1); // {width: 692, height: 612}, width and height get swapped

  // rotate the first page back so that it appears upright in the viewer
  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight);
  doc.getPageInfo(page1); // {width: 692, height: 512}
  // Even though the page appears upright, the document is rotated 90 degree clockwise
  // So the left side of the document is cropped
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.rotateCounterClockwise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#rotateCounterClockwise__anchor) [Document.getPageInfo](https://sdk.apryse.com/api/web/Core.Document.html#getPageInfo__anchor) [Document.cropPages](https://sdk.apryse.com/api/web/Core.Document.html#cropPages)
{% endtab %}

{% tab title="JavaScript (SDK v7.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const docViewer = instance.docViewer;
const rotation = instance.CoreControls.PageRotation;

docViewer.on('documentLoaded', async () => {
  const doc = docViewer.getDocument();
  const cropTop = 100, cropLeft = 0, cropRight = 0, cropBottom = 0;
  const page1 = 1, page2 = 2, page3 = 3;

  doc.getPageInfo(page1); // {width: 612, height: 792}

  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight)
  doc.getPageInfo(page1); // {width: 612, height: 692}

  await doc.rotatePages([page1], rotation.E_90);
  docViewer.rotateCounterClockwise(page1);
  doc.getPageInfo(page1); // {width: 692, height: 612}, width and height get swapped

  // rotate the first page back so that it appears upright in the viewer
  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight);
  doc.getPageInfo(page1); // {width: 692, height: 512}
  // Even though the page appears upright, the document is rotated 90 degree clockwise
  // So the left side of the document is cropped
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.rotateCounterClockwise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#rotateCounterClockwise__anchor) [Document.getPageInfo](https://sdk.apryse.com/api/web/Core.Document.html#getPageInfo__anchor) [Document.cropPages](https://sdk.apryse.com/api/web/Core.Document.html#cropPages)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
// config.js
const docViewer = instance.docViewer;
const rotation = instance.CoreControls.PageRotation;

docViewer.on('documentLoaded', async () => {
  const doc = docViewer.getDocument();
  const cropTop = 100, cropLeft = 0, cropRight = 0, cropBottom = 0;
  const page1 = 1, page2 = 2, page3 = 3;

  doc.getPageInfo(page1 - 1); // {width: 612, height: 792}

  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight)
  doc.getPageInfo(page1); // {width: 612, height: 692}

  await doc.rotatePages([page1], rotation.e_90);
  docViewer.rotateCounterClockwise(page1);
  doc.getPageInfo(page1 - 1); // {width: 692, height: 612}, width and height get swapped

  // rotate the first page back so that it appears upright in the viewer
  await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight);
  doc.getPageInfo(page1 - 1); // {width: 692, height: 512}
  // Even though the page appears upright, the document is rotated 90 degree clockwise
  // So the left side of the document is cropped
});
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.rotateCounterClockwise](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#rotateCounterClockwise__anchor) [Document.getPageInfo](https://sdk.apryse.com/api/web/Core.Document.html#getPageInfo__anchor) [Document.cropPages](https://sdk.apryse.com/api/web/Core.Document.html#cropPages)
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/salesforce/page-manipulation/crop.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
