Some test text!

menu

PDF imposition in Swift

More languages

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

Sample Swift code for using PDFTron SDK to impose (combine) multiple PDF pages. Page imposition can be used to arrange/order pages prior to printing or for document assembly (assemble a 'master' page from several 'source' pages). It is also possible to write applications that can re-order the pages such that they will display in the correct order when the hard copy pages are compiled and folded correctly. Learn more about our Swift PDF Library and PDF Editing & Manipulation Library.

Get StartedSamplesDownload

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

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

import PDFNet
import Foundation

//-----------------------------------------------------------------------------------
// The sample illustrates how multiple pages can be combined/imposed
// using PDFNet. Page imposition can be used to arrange/order pages
// prior to printing or to assemble a 'master' page from several 'source'
// pages. Using PDFNet API it is possible to write applications that can
// re-order the pages such that they will display in the correct order
// when the hard copy pages are compiled and folded correctly.
//-----------------------------------------------------------------------------------
func runImpositionTest() -> Int {
    return autoreleasepool {
        var ret: Int = 0
        
        
        // Relative path to the folder containing test files.
        let input_path: String? = Bundle.main.path(forResource: "newsletter", ofType: "pdf")
        let output_path: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_booklet.pdf").path
        
        do {
            try PTPDFNet.catchException {
                print("-------------------------------------------------")
                print("Opening the input pdf...")
                
                let filein: String? = input_path
                let fileout: String = output_path
                
                let in_doc: PTPDFDoc = PTPDFDoc(filepath: filein)
                in_doc.initSecurityHandler()
                
                // Create a list of pages to import from one PDF document to another.
                let import_pages: PTVectorPage = PTVectorPage()
                let itr: PTPageIterator = in_doc.getPageIterator(1)
                while itr.hasNext() {
                    import_pages.add(itr.current())
                    itr.next()
                }
                
                let new_doc: PTPDFDoc = PTPDFDoc()
                let imported_pages: PTVectorPage = new_doc.importPages(import_pages, import_bookmarks: false)
                
                // Paper dimension for A3 format in points. Because one inch has
                // 72 points, 11.69 inch 72 = 841.69 points
                let media_box: PTPDFRect = PTPDFRect(x1: 0, y1: 0, x2: 1190.88, y2: 841.69)
                let mid_point: Double = media_box.width() / 2
                
                let builder: PTElementBuilder = PTElementBuilder()
                let writer: PTElementWriter = PTElementWriter()
                
                var i: Int32 = 0
                while i < imported_pages.size() {
                    // Create a blank new A3 page and place on it two pages from the input document.
                    let new_page: PTPage = new_doc.pageCreate(media_box)
                    
                    writer.writerBegin(with: new_page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)
                
                    // Place the first page
                    var src_page: PTPage = imported_pages.get(i)
                    var element: PTElement = builder.createForm(with: src_page)
                    
                    var sc_x: Double = mid_point / src_page.getWidth(e_ptcrop)
                    var sc_y: Double = media_box.height() / src_page.getHeight(e_ptcrop)
                    var scale: Double = sc_x < sc_y ? sc_x : sc_y // min(sc_x, sc_y)
                    element.getGState().setTransform(scale, b: 0, c: 0, d: scale, h: 0, v: 0)
                    writer.writePlacedElement(element)
                    
                    // Place the second page
                    i += 1
                    if i < imported_pages.size() {
                        src_page = imported_pages.get(i)
                        element = builder.createForm(with: src_page)
                        sc_x = mid_point / src_page.getWidth(e_ptcrop)
                        sc_y = media_box.height() / src_page.getHeight(e_ptcrop)
                        scale = sc_x < sc_y ? sc_x : sc_y
                        // min(sc_x, sc_y)
                        element.getGState().setTransform(scale, b: 0, c: 0, d: scale, h: mid_point, v: 0)
                        writer.writePlacedElement(element)
                    }
                    
                    writer.end()
                    new_doc.pagePushBack(new_page)
                    i += 1
                }
                
                new_doc.save(toFile: fileout, flags: e_ptlinearized.rawValue)
                print("Done. Result saved in newsletter_booklet.pdf...")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        print("Done.")
        return ret
    }
}
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close