Cropping PDF 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 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.

Apryse Docs Image

Show user crop pages dialog

To show a user crop pages dialog in your activity, create a new instance of UserCropDialogFragment by calling newInstance() and setting the PDFViewCtrl:

1private PDFViewCtrl mPdfViewCtrl;
2// ...
3public void showUserCropDialog(FragmentManager fragmentManager) {
4 UserCropDialogFragment fragment = UserCropDialogFragment.newInstance();
5 fragment.setPdfViewCtrl(mPdfViewCtrl);
6 // Set a custom style for this dialog
7 fragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.PDFTronAppTheme);
8 // Show the dialog
9 fragment.show(fragmentManager, "user_crop_pages_dialog");
10}

In order to view user cropped pages in PDFViewCtrl make sure you set Page.e_user_crop as the default page box:

1mPdfViewCtrl.setPageBox(Page.e_user_crop);

You can also set a listener via setOnUserCropDialogDismissListener(...) to be notified when the user crop dialog is dismissed.

UserCropDialogFragment does not crop pages permanently, it may not handled well by other PDF viewer. To crop pages permanently, see cropping pages permanently

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 and pass in your activity and PDFViewCtrl.

Example
1UserCropUtilities.AutoCropInBackgroundTask autoCropTask =
2 new UserCropUtilities.AutoCropInBackgroundTask(getContext(), mPdfViewCtrl,
3 new UserCropUtilities.AutoCropInBackgroundTask.AutoCropTaskListener() {
4 @Override
5 public void onAutoCropTaskDone() {
6 // Auto crop task was finished or cancelled
7 }
8 });

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).

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).

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales