Change PDF Media Box - Rect - Ruby 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
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12 PDFNet.Initialize(PDFTronLicense.Key)
13
14 # Relative path to the folder containing the test files.
15 input_path = "../../TestFiles/"
16 output_path = "../../TestFiles/Output/"
17
18 # Test - Adjust the position of content within the page.
19 puts "_______________________________________________"
20 puts "Opening the input pdf..."
21
22 input_doc = PDFDoc.new(input_path + "tiger.pdf")
23 input_doc.InitSecurityHandler
24 pg_itr1 = input_doc.GetPageIterator
25
26 media_box = Rect.new(pg_itr1.Current.GetMediaBox)
27
28 media_box.x1 -= 200 # translate the page 200 units (1 uint = 1/72 inch)
29 media_box.x2 -= 200
30
31 media_box.Update
32
33 input_doc.Save(output_path + "tiger_shift.pdf", 0)
34 input_doc.Close
35 PDFNet.Terminate
36 puts "Done. Result saved in tiger_shift..."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales