Sample C# code for using Apryse SDK to change a page's MediaBox using Rect class. 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//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11
12namespace RectTestCS
13{
14 /// <summary>
15 /// Summary description for Class1.
16 /// </summary>
17 class Class1
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static Class1() {}
21
22 /// <summary>
23 /// The main entry point for the application.
24 /// </summary>
25 [STAThread]
26 static void Main(string[] args)
27 {
28 PDFNet.Initialize(PDFTronLicense.Key);
29 // Relative path to the folder containing test files.
30 string input_path = "../../../../TestFiles/";
31 string output_path = "../../../../TestFiles/Output/";
32
33 Console.WriteLine("_______________________________________________");
34 Console.WriteLine("Opening the input pdf...");
35
36 try // Test - Adjust the position of content within the page.
37 {
38 using (PDFDoc input_doc = new PDFDoc(input_path + "tiger.pdf"))
39 {
40 input_doc.InitSecurityHandler();
41
42 Page pg = input_doc.GetPage(1);
43 Rect media_box = pg.GetMediaBox();
44
45 media_box.x1 -= 200; // translate the page 200 units (1 uint = 1/72 inch)
46 media_box.x2 -= 200;
47
48 media_box.Update();
49
50 input_doc.Save(output_path + "tiger_shift.pdf", 0);
51 }
52
53 Console.WriteLine("Done. Result saved in tiger_shift...");
54 }
55 catch (PDFNetException e)
56 {
57 Console.WriteLine(e.Message);
58 }
59 PDFNet.Terminate();
60 }
61 }
62}
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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14func main(){
15 PDFNetInitialize(PDFTronLicense.Key)
16 // Relative path to the folder containing test files.
17 var inputPath = "../../TestFiles/"
18 var outputPath = "../../TestFiles/Output/"
19 // Test - Adjust the position of content within the page.
20 fmt.Println("_______________________________________________")
21 fmt.Println("Opening the input pdf...")
22
23 inputDoc := NewPDFDoc(inputPath + "tiger.pdf")
24 inputDoc.InitSecurityHandler()
25 pgItr1 := inputDoc.GetPageIterator()
26
27 mediaBox := NewRect(pgItr1.Current().GetMediaBox())
28
29 mediaBox.SetX1(mediaBox.GetX1() - 200) // translate the page 200 units (1 uint = 1/72 inch)
30 mediaBox.SetX2(mediaBox.GetX2() - 200)
31
32 mediaBox.Update()
33
34 inputDoc.Save(outputPath + "tiger_shift.pdf", uint(0))
35 inputDoc.Close()
36
37 PDFNetTerminate()
38 fmt.Println("Done. Result saved in tiger_shift...")
39}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8
9public class RectTest {
10
11 public static void main(String[] args) {
12 PDFNet.initialize(PDFTronLicense.Key());
13
14 // Relative path to the folder containing test files.
15 String input_path = "../../TestFiles/";
16 String output_path = "../../TestFiles/Output/";
17
18 try (PDFDoc input_doc = new PDFDoc((input_path + "tiger.pdf"))) // Test - Adjust the position of content within the page.
19 {
20 System.out.println("_______________________________________________");
21 System.out.println("Opening the input pdf...");
22
23 input_doc.initSecurityHandler();
24
25 PageIterator pg_itr1 = input_doc.getPageIterator();
26
27 Rect media_box = pg_itr1.next().getMediaBox();
28
29 media_box.setX1(media_box.getX1() - 200); // translate the page 200 units (1 uint = 1/72 inch)
30 media_box.setX2(media_box.getX2() - 200);
31
32 media_box.update();
33
34 input_doc.save(output_path + "tiger_shift.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
35 System.out.println("Done. Result saved in tiger_shift...");
36 } catch (Exception e) {
37 System.out.println(e);
38 }
39
40 PDFNet.terminate();
41 }
42}
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
7const { PDFNet } = require('@pdftron/pdfnet-node');
8const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
10((exports) => {
11
12 exports.runRectTest = () => {
13
14 const main = async() => {
15 try {
16 console.log('_______________________________________________');
17 console.log('Opening the input pdf...');
18
19 const inputPath = '../TestFiles/';
20 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tiger.pdf');
21 doc.initSecurityHandler();
22
23 const pgItr1 = await doc.getPageIterator();
24 const mediaBox = await (await pgItr1.current()).getMediaBox();
25 mediaBox.x1 -= 200; // translate page 200 units left(1 uint = 1/72 inch)
26 mediaBox.x2 -= 200;
27
28 await mediaBox.update();
29
30 await doc.save(inputPath + 'Output/tiger_shift.pdf', 0);
31 console.log('Done. Result saved in tiger_shift...');
32 } catch (err) {
33 console.log(err);
34 }
35 };
36 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
37 };
38 exports.runRectTest();
39})(exports);
40// eslint-disable-next-line spaced-comment
41//# sourceURL=AnnotationTest.js
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?>
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()
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..."
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12
13Module RectTestVB
14 Dim pdfNetLoader As PDFNetLoader
15 Sub New()
16 pdfNetLoader = pdftron.PDFNetLoader.Instance()
17 End Sub
18
19 Sub Main()
20
21 PDFNet.Initialize(PDFTronLicense.Key)
22
23 ' Relative path to the folder containing test files.
24 Dim input_path As String = "../../../../TestFiles/"
25 Dim output_path As String = "../../../../TestFiles/Output/"
26
27 Try
28 Console.WriteLine("-------------------------------------------------")
29 Console.WriteLine("Opening the input pdf...")
30
31 ' Test - Adjust the position of content within the page.
32 Using input_doc As PDFDoc = New PDFDoc(input_path + "tiger.pdf")
33 input_doc.InitSecurityHandler()
34
35 Dim pg As Page = input_doc.GetPage(1)
36 Dim media_box As Rect = pg.GetMediaBox()
37
38 media_box.x1 -= 200 ' translate the page 200 units (1 uint = 1/72 inch)
39 media_box.x2 -= 200
40
41 media_box.Update()
42
43 input_doc.Save(output_path + "tiger_shift.pdf", 0)
44
45 Console.WriteLine("Done. Result saved in tiger_shift.pdf")
46 End Using
47 Catch ex As PDFNetException
48 Console.WriteLine(ex.Message)
49 Catch ex As Exception
50 MsgBox(ex.Message)
51 End Try
52 PDFNet.Terminate()
53 End Sub
54End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales