Change PDF Media Box - Rect - C++ 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-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <iostream>
9#include "../../LicenseKey/CPP/LicenseKey.h"
10
11using namespace std;
12
13using namespace pdftron;
14using namespace PDF;
15using namespace Common;
16
17
18int main(int argc, char *argv[])
19{
20 int ret = 0;
21 PDFNet::Initialize(LicenseKey);
22
23 // Relative path to the folder containing test files.
24 string input_path = "../../TestFiles/";
25 string output_path = "../../TestFiles/Output/";
26
27 try // Test - Adjust the position of content within the page.
28 {
29 cout << "_______________________________________________" << endl;
30 cout << "Opening the input pdf..." << endl;
31
32 PDFDoc input_doc((input_path + "tiger.pdf").c_str());
33 input_doc.InitSecurityHandler();
34
35 PageIterator pg_itr1 = input_doc.GetPageIterator();
36
37 Rect media_box(pg_itr1.Current().GetMediaBox());
38
39 media_box.x1 -= 200; // translate the page 200 units (1 uint = 1/72 inch)
40 media_box.x2 -= 200;
41
42 media_box.Update();
43
44 input_doc.Save((output_path + "tiger_shift.pdf").c_str(), 0 , NULL);
45
46 cout << "Done. Result saved in tiger_shift..." << endl;
47 }
48 catch(Exception& e)
49 {
50 cout << e << endl;
51 ret = 1;
52 }
53 catch(...)
54 {
55 cout << "Unknown Exception" << endl;
56 ret = 1;
57 }
58
59 PDFNet::Terminate();
60 return ret;
61}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales