Change PDF Media Box - Rect - Python Sample Code

Sample code for using Apryse SDK to change a page's MediaBox using Rect class. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14def main():
15 PDFNet.Initialize(LicenseKey)
16
17 # Relative path to the folder containing the test files.
18 input_path = "../../TestFiles/"
19 output_path = "../../TestFiles/Output/"
20
21 # Test - Adjust the position of content within the page.
22 print("_______________________________________________")
23 print("Opening the input pdf...")
24
25 input_doc = PDFDoc(input_path + "tiger.pdf")
26 input_doc.InitSecurityHandler()
27 pg_itr1 = input_doc.GetPageIterator()
28
29 media_box = Rect(pg_itr1.Current().GetMediaBox())
30
31 media_box.x1 -= 200 # translate the page 200 units (1 uint = 1/72 inch)
32 media_box.x2 -= 200
33
34 media_box.Update()
35
36 input_doc.Save(output_path + "tiger_shift.pdf", 0)
37 input_doc.Close()
38
39 PDFNet.Terminate()
40 print("Done. Result saved in tiger_shift...")
41
42if __name__ == '__main__':
43 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales