Swift for Xojo Developers - Part 8a : Graphics : PDF (revisited)

In Feb of 2020, I wrote this post

I have just completed a total (well almost total) rewrite of my PDF code, but now it works for both iOS and macOS from a single project file (try THAT Xojo! :slight_smile: )

This library will support any font currently installed on the device (desktop or iPhone/iPad)
It supports Unicode characters
It uses a syntax very similar to those commands found in the Xojo Graphics object (except for Object2D)
It is written in 100% Swift with zero external dependecies
The generated PDF document is stored directly to the appropriate /DOCUMENTS folder

here is the basic list of public functions

#  classPDF - a PDF generator for iOS and macOS

Language : Swift5                                   
Platform : iOS and macOS                            
Author   : R.David Sisemore                         
Written  : 18Apr2023                                
Copyright © 2023 R.DavidSisemore  - All rights reserved      


class pdfCanvas {

    public enum lineStyle {
        case none
        case solid
        case dot
        case dash

    }
    public enum pageSizes {
        case letter
        case legal
        case tabloid
        case a4
        case a3
    }

   init(pdfPath:String,pageSize:pageSizes,author:String="",subject:String="",title:String="") {
  
   init(pdfPath:String, pageSize : CGSize = CGSize(width:612,height:792),author:String="",subject:String="",title:String="") {

    public var margin : CGPoint {
 
    //
    // Start a new page
    //
    public func nextPage() {
    //
    // CLOSE PDF Document
    //
    public func close() {
    //
    // Set/Get Font
    //
    public var font : dsFONT {
    *dsFONT* is an alias for either UIFont or NSFont depending on which OS the function is being compiled for
    //
    // Set/Get FontName
    //
    public var textFont : String {
    //
    // Set/Get FontSize
    //
    public var textSize : CGFloat {
    //
    // Set/Get Bold Trait
    //
    public var bold : Bool {
    //
    // Set/Get Italic Trait
    //
    public var italic : Bool {
    //
    // Set/Get Underline Trait
    //
    public var underline : Bool {

    public var textHeight : CGFloat 
    public var textAscent : CGFloat 
    public func stringWidth(text:String)  -> CGFloat 
    public func stringHeight(text:String) -> CGFloat 
    //
    // Set/Get ForeColor
    //
    public var foreColor : dsCOLOR {
    *dsCOLOR* is an alias for either UIColor or NSColor depending on which OS the function is being compiled for
    //
    // Set/Get Text Alignment
    //
    public var align : NSTextAlignment {
    //
    // Set the PenSize [ie. Line Drawing Width]
    //
    public var penSize : CGFloat {
    //
    // Set Pen Style [solid, dotted, dashed]
    //
    public var penStyle :lineStyle {
    //
    // Insert an IMAGE into document
    //
    public func drawPicture(img:dsIMAGE,rect:CGRect) {
    //
    // Insert Text into document
    //
    public func drawString(text:String,rect:CGRect) {
    public func drawString(text:String,x:CGFloat,y:CGFloat, width:CGFloat=0) {
    //
    // Draw a line from (X1,Y1) -> (X2,Y2)
    //
    public func drawLine(x1:CGFloat,y1:CGFloat,x2:CGFloat,y2:CGFloat) {
    //
    // OVAL
    //
    public func drawOval(rect:CGRect) { 
    public func drawOval(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
 
    public func fillOval(rect:CGRect) { 
    public func fillOval(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
 
    //
    // Rectangle
    //
    public func drawRect(rect:CGRect) { 
    public func drawRect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 
   
    public func fillRect(rect:CGRect) { 
    public func fillRect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) { 

    //
    // Rounded Rectangle
    //
    public func drawRoundrect(rect:CGRect,radius:CGFloat) {
    public func drawRoundrect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat,radius:CGFloat) { 
   
    public func fillRoundrect(rect:CGRect,radius:CGFloat) { 
    public func fillRoundrect(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat,radius:CGFloat) {
    //
    // Polygon
    //
    public func drawPolygon(points:[CGPoint],fill:Bool=false) {
    public func fillPolygon(points:[CGPoint]) { 
}
2 Likes

Updated to allow images to be drawn at any size and maintain aspect ratio,
Updated to provide automatic word wrapping of long text

3 Likes