Section:

Cropping PDF pages using JavaScript

PDF pages can be cropped using the 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.

1// config.js
2const docViewer = instance.Core.documentViewer;
3const rotation = instance.Core.PageRotation;
4
5docViewer.addEventListener('documentLoaded', async () => {
6 const doc = docViewer.getDocument();
7 const cropTop = 100, cropLeft = 0, cropRight = 0, cropBottom = 0;
8 const page1 = 1, page2 = 2, page3 = 3;
9
10 doc.getPageInfo(page1); // {width: 612, height: 792}
11
12 await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight)
13 doc.getPageInfo(page1); // {width: 612, height: 692}
14
15 await doc.rotatePages([page1], rotation.E_90);
16 docViewer.rotateCounterClockwise(page1);
17 doc.getPageInfo(page1); // {width: 692, height: 612}, width and height get swapped
18
19 // rotate the first page back so that it appears upright in the viewer
20 await doc.cropPages([page1], cropTop, cropBottom, cropLeft, cropRight);
21 doc.getPageInfo(page1); // {width: 692, height: 512}
22 // Even though the page appears upright, the document is rotated 90 degree clockwise
23 // So the left side of the document is cropped
24});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales