Swift for Xojo Developers - Part 8 : Graphics : PDF

One of the features that is most talked about for Xojo is the ability to create PDF documents. There are a number of 3rd party tools including DynaPDF and my own gPDF, so I can attest to the complexity of the code involved.

Ironically for iOS, this is a relatively simple task. The below code creates the required context and by default is for the US Letter size (but can be easily altered)

This class is also subclassed from the previously posted CANVAS class

/* *************************** */
/* ****                   **** */
/* **** P D F   C L A S S **** */
/* **** Super : CANVAS    **** */
/* ****                   **** */
/* *************************** */
class PDF : Canvas {
    private let zPageSize   = CGSize(width: 612,height: 792) // standard US Letter Size Page [72DPI]
    private var zPageRect   : CGRect = CGRect.zero
    private var zPDFPath    : String = ""
    private var zPageOpen   : Bool   = false
    private var zDocOpen    : Bool   = false
    private var zPageNumber : Int    = 0

    //
    // Create PDF Document
    //

    init(_ pdfPath:String) {
        // define specific context for PDF document, must be done BEFORE initializing CANVAS superclass
        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = paths[0] as NSString
        zPDFPath  = documentsDirectory.appending("/" + "\(pdfPath).pdf")
        zPageRect = CGRect(x: 0,y: 0,width: zPageSize.width,height: zPageSize.height)
        UIGraphicsBeginPDFContextToFile(zPDFPath,zPageRect, nil)
        let pdfContext : CGContext = UIGraphicsGetCurrentContext()!
        //
        super.init(context: pdfContext,offset: CGPoint(x:0,y:0)) // Initialize CANVAS superclass
        Margin=CGPoint(x:9,y:9)
        //
        zPageNumber=0
        nextPage() // start out at top of first page
        zDocOpen=true
        print(zPDFPath)
    }

    deinit { if zDocOpen { close() }  }// in case user forgot to close the stream

    var Margin : CGPoint {
        get { return Offset }
        set { Offset=CGPoint(x:max(9,newValue.x),y:max(9,newValue.y)) }
    }
    //
    // CLOSE PDF Document
    //
    func close() {
        if zPageOpen { pageEND() }
        UIGraphicsEndPDFContext()
        zDocOpen=false
    }

    //
    // Close current Page
    //
    private func pageEND() { zPageOpen=false }

    //
    // Return path for PDF Document (will always be in /Document folder)
    //
    var path       : String  { get { return zPDFPath }}
    var pageWidth  : CGFloat { get { return zPageSize.width  - (2*Offset.x) }}
    var pageHeight : CGFloat { get { return zPageSize.height - (2*Offset.y)  }}
    var pageNumber : Int     { get { return zPageNumber } }

    //
    // Start a new page
    //
    func nextPage() {
        if zPageOpen { pageEND() }
        UIGraphicsBeginPDFPageWithInfo(zPageRect, nil)
        zPageOpen    = true
        zPageNumber += 1
        position     = CGPoint.zero
    }
}