Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Change a PDF Page's MediaBox in JavaScript

More languages

More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Go
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample JavaScript code for using PDFTron SDK to change a page's MediaBox using Rect class. Learn more about our JavaScript PDF Library and PDF Editing & Manipulation Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------


const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {

  exports.runRectTest = () => {

    const main = async() => {
      try {
        console.log('_______________________________________________');
        console.log('Opening the input pdf...');

        const inputPath = '../TestFiles/';
        const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
        doc.initSecurityHandler();

        const pgItr1 = await doc.getPageIterator();
        const mediaBox = await (await pgItr1.current()).getMediaBox();
        mediaBox.x1 -= 200; // translate page 200 units left(1 uint = 1/72 inch)
        mediaBox.x2 -= 200;

        await mediaBox.update();

        await doc.save(inputPath + 'Output/tiger_shift.pdf', 0);
        console.log('Done. Result saved in tiger_shift...');
      } catch (err) {
        console.log(err);
      }
    };
    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
  };
  exports.runRectTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=AnnotationTest.js