> 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/android/page-manipulation/crop.md).

# Crop a page

Cropping PDF pages in Android is easy with the Apryse library, you can crop pages manually or automatically, as well as reset document pages to their original size. The UserCropDialogFragment allows a

There are two options to crop a page. First is using a UI component that can crop or reset pages. Second is an API guide to programmatically crop a page.

{% tabs %}
{% tab title="UI component" %}

## Crop document pages in Android

With the Apryse library you can crop pages manually or automatically, as well as reset document pages to their original size. The [`UserCropDialogFragment`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/controls/UserCropDialogFragment.html) allows a user to adjust the page's crop box which can then be applied to a subset of pages or to all the pages in a document.

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-829d16c6a0dd2a2b5a4590c036eb396908a49aeb%2Fbe20e1f5d7b00edf611f9ef5a75f79ddedd90f7c-600x1200.gif?alt=media)

## Show user crop pages dialog

To show a user crop pages dialog in your activity, create a new instance of [`UserCropDialogFragment`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/controls/UserCropDialogFragment.html) by calling `newInstance()` and setting the [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html):

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
private PDFViewCtrl mPdfViewCtrl;
// ...
public void showUserCropDialog(FragmentManager fragmentManager) {
    UserCropDialogFragment fragment = UserCropDialogFragment.newInstance();
    fragment.setPdfViewCtrl(mPdfViewCtrl);
    // Set a custom style for this dialog
    fragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.PDFTronAppTheme);
    // Show the dialog
    fragment.show(fragmentManager, "user_crop_pages_dialog");
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
private var mPdfViewCtrl: PDFViewCtrl? = null
// ...
fun showUserCropDialog(fragmentManager: FragmentManager) {
    val fragment = UserCropDialogFragment.newInstance()
    fragment.setPdfViewCtrl(mPdfViewCtrl!!)
    // Set a custom style for this dialog
    fragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.PDFTronAppTheme)
    // Show the dialog
    fragment.show(fragmentManager, "user_crop_pages_dialog")
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

In order to view user cropped pages in [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) make sure you set `Page.e_user_crop` as the default page box:

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
mPdfViewCtrl.setPageBox(Page.e_user_crop);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
mPdfViewCtrl?.setPageBox(Page.e_user_crop)
```

{% endcode %}
{% endtab %}
{% endtabs %}

You can also set a listener via [`setOnUserCropDialogDismissListener(...)`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html) to be notified when the user crop dialog is dismissed.

{% hint style="info" %}
`UserCropDialogFragment` does not crop pages permanently, it may not handled well by other PDF viewer. To crop pages permanently, see [cropping pages permanently](https://docs.apryse.com)
{% endhint %}

## Automatically crop pages

You can also automatically crop the edges of the document based on the position of the content. To do this, you can simply call the utility method [`UserCropUtilities.AutoCropInBackgroundTask`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/utils/UserCropUtilities.AutoCropInBackgroundTask.html) and pass in your activity and [`PDFViewCtrl`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/PDFViewCtrl.html).

**Example**

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
UserCropUtilities.AutoCropInBackgroundTask autoCropTask =
    new UserCropUtilities.AutoCropInBackgroundTask(getContext(), mPdfViewCtrl,
        new UserCropUtilities.AutoCropInBackgroundTask.AutoCropTaskListener() {
            @Override
            public void onAutoCropTaskDone() {
                // Auto crop task was finished or cancelled
            }
        });
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val autoCropTask = UserCropUtilities.AutoCropInBackgroundTask(context, mPdfViewCtrl,
    UserCropUtilities.AutoCropInBackgroundTask.AutoCropTaskListener {
        // Auto crop task was finished or cancelled
    })
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Remove user defined crop boxes

If you wish to remove user defined cropping for a given page you can call the utility method [`UserCropUtilities.removeUserCropFromPage(Page)`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/utils/UserCropUtilities.html#removeUserCropFromPage\(Page\)).

## Permanently crop pages

Cropping pages using the above methods are at user-level. User-level page-cropping may not be handled well by other PDF viewers. To apply crop pages permanently use [`UserCropUtilities.cropDoc(PDFDoc)`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/utils/UserCropUtilities.html#cropDoc\(PDFDoc\)).
{% endtab %}

{% tab title="API guide" %}

## API to programmatically crop pages in Android

To crop a page in a PDF document.

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);

// Access a PDF page
Page page = doc.getPage(1);

// Crop the page
page.setCropBox(new Rect(0, 0, 500, 600));
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)

// Access a PDF page
val page = doc.getPage(1)

// Crop the page
page.setCropBox(Rect(0, 0, 500, 600));
```

{% endcode %}
{% endtab %}
{% endtabs %}

## About cropping the page

The crop box defines the region to which the contents of the page are to be clipped (cropped) when displayed or printed. Unlike other types of boxes, the crop box has no defined meaning in terms of physical page geometry or intended use; it merely imposes clipping on the page contents. The default value is the page's media box. A new crop box can be imposed on a page with `Page.SetCropBox()`.
{% 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
