Some test text!

Search
Hamburger Icon

Change a PDF Page's MediaBox in Swift

More languages

More languages
JavaScript
Java (Android)
C++
C#
C# (.NET Core)
Go
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample Swift code for using PDFTron SDK to change a page's MediaBox using Rect class. Learn more about our Swift PDF Library and PDF Editing & Manipulation Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

func runRectTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        
        
        do {
            try PTPDFNet.catchException {
                // Test  - Adjust the position of content within the page.
                print("_______________________________________________")
                print("Opening the input pdf...")
                
                let input_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "tiger", ofType: "pdf"))
                input_doc.initSecurityHandler()
                
                let pg_itr1: PTPageIterator = input_doc.getPageIterator(1)
                
                let media_box: PTPDFRect = pg_itr1.current().getMediaBox()
                
                media_box.setX1(media_box.getX1() - 200) // translate the page 200 units (1 uint = 1/72 inch)
                media_box.setX2(media_box.getX2() - 200)
                
                media_box.update(PTObj())
                
                input_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("tiger_shift.pdf").path, flags: 0)
                
                print("Done. Result saved in tiger_shift...")
            }
        } catch let e as NSError {
            print("Caught PDFNet exception: \(e)")
            ret = 1
        }
        
        return ret
    }
}