Change PDF Media Box - Rect - PHP 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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10 PDFNet::Initialize($LicenseKey);
11 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
12
13 // Relative path to the folder containing the test files.
14 $input_path = getcwd()."/../../TestFiles/";
15 $output_path = $input_path."Output/";
16
17 // Test - Adjust the position of content within the page.
18 echo nl2br("_______________________________________________\n");
19 echo nl2br("Opening the input pdf...\n");
20
21 $input_doc = new PDFDoc($input_path."tiger.pdf");
22 $input_doc->InitSecurityHandler();
23 $pg_itr1 = $input_doc->GetPageIterator();
24
25 $media_box = new Rect($pg_itr1->Current()->GetMediaBox());
26
27 $media_box->x1 -= 200;
28 $media_box->x2 -= 200;
29
30 $media_box->Update();
31 $input_doc->Save($output_path."tiger_shift.pdf", 0);
32 $input_doc->Close();
33 PDFNet::Terminate();
34 echo nl2br("Done. Result saved in tiger_shift...\n");
35?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales