Some test text!

menu
search
chevron_right Python samples

Change a PDF Page's MediaBox in Python

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

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

#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

def main():
    PDFNet.Initialize()
    
    # Relative path to the folder containing the test files.
    input_path = "../../TestFiles/"
    output_path = "../../TestFiles/Output/"
    
    # Test  - Adjust the position of content within the page.
    print("_______________________________________________")
    print("Opening the input pdf...")
    
    input_doc = PDFDoc(input_path + "tiger.pdf")
    input_doc.InitSecurityHandler()
    pg_itr1 = input_doc.GetPageIterator()
    
    media_box = Rect(pg_itr1.Current().GetMediaBox())
    
    media_box.x1 -= 200     # translate the page 200 units (1 uint = 1/72 inch)
    media_box.x2 -= 200
    
    media_box.Update()
    
    input_doc.Save(output_path + "tiger_shift.pdf", 0)
    input_doc.Close()
    
    print("Done. Result saved in tiger_shift...")    

if __name__ == '__main__':
    main()
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.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close