Some test text!

menu
Crop pageskeyboard_arrow_down

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.

WebViewer(...)
  .then(instance => {
    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
    });
  });

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.