Skip to content

Instantly share code, notes, and snippets.

@RichFell
Created February 18, 2015 15:15
Show Gist options
  • Save RichFell/f4edc9362d3a5cf4be07 to your computer and use it in GitHub Desktop.
Save RichFell/f4edc9362d3a5cf4be07 to your computer and use it in GitHub Desktop.
Taking existing pdf and annotating on it with user input.
// PDFRenderer.swift
// MobileMakersAcademy
//
// Created by Rich Fellure on 2/2/15.
// Copyright (c) 2015 Mobile Makers. All rights reserved.
//
import Foundation
class PDFRenderer {
class func renderPDFForSchool(school: String, andTeacherName teacher: String, withEstNumOfStudents students: String, andStartDate date: NSDate, completedWithPath: (path: String)-> Void) {
var localUrl: NSURL = NSBundle.mainBundle().URLForResource("TeacherPlan", withExtension: "pdf")!
var pdfDocumentRef: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(localUrl as CFURLRef)
var page1: CGPDFPageRef = CGPDFDocumentGetPage(pdfDocumentRef, 1)
var paperSize = CGRectMake(0.0, 0.0, 792, 612)
var pageSize = CGPDFPageGetBoxRect(page1, kCGPDFArtBox);
var path = NSTemporaryDirectory()
var temporaryPdfFilePath = path.stringByAppendingPathComponent("actionPlan.pdf")
println(path)
var graphicsContext = UIGraphicsBeginPDFContextToFile(temporaryPdfFilePath, pageSize, nil)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
let currentContext: CGContextRef = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(currentContext, 0.0, paperSize.height)
CGContextScaleCTM(currentContext, 1.0, -1.0)
writeTextOnPageOne(school, andTeacherName: teacher, andNumberOfStudents: students, andStartDate: date.addDaysToDateAndReturnNormalString(0), forCurrentContext: currentContext, andDocumentRef: pdfDocumentRef, andPaperSize: paperSize)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
CGContextTranslateCTM(currentContext, 0.0, paperSize.height)
CGContextScaleCTM(currentContext, 1.0, -1.0)
writeTextOnPageTwo(date, forCurrentContext: currentContext, andDocumentRef: pdfDocumentRef, andPaperSize: paperSize)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
CGContextTranslateCTM(currentContext, 0.0, paperSize.height)
CGContextScaleCTM(currentContext, 1.0, -1.0)
writePageThreeTextForDate(date, forContext: currentContext, andDocumentRef: pdfDocumentRef, andPaperSize: paperSize)
UIGraphicsEndPDFContext()
completedWithPath(path: temporaryPdfFilePath)
}
private class func writeTextOnPageOne(schoolName: String, andTeacherName teacherName:String, andNumberOfStudents students: String, andStartDate date: String, forCurrentContext context: CGContextRef, andDocumentRef documentRef: CGPDFDocumentRef, andPaperSize size: CGRect) {
var page: CGPDFPageRef = CGPDFDocumentGetPage(documentRef, 1)
CGContextDrawPDFPage(context, page)
CGContextTranslateCTM(context, 0.0, size.height)
CGContextScaleCTM(context, 1.0, -1.0)
writeText(schoolName, inRect: CGRectMake(179, 322, 500, 500))
writeText(teacherName, inRect: CGRectMake(179, 382, 500, 500))
writeText(students, inRect: CGRectMake(290, 480, 200, 200))
writeText(date, inRect: CGRectMake(590, 480, 200, 200))
}
private class func writeTextOnPageTwo(date: NSDate, forCurrentContext context: CGContextRef, andDocumentRef docRef: CGPDFDocumentRef, andPaperSize size: CGRect) {
var page : CGPDFPageRef = CGPDFDocumentGetPage(docRef, 2)
CGContextDrawPDFPage(context, page)
CGContextTranslateCTM(context, 0.0, size.height)
CGContextScaleCTM(context, 1.0, -1.0)
writeDateText(date.addDaysToDateAndReturnNormalString(150), inRect: CGRectMake(75, 160, 200, 200))
writeDateText(date.addDaysToDateAndReturnNormalString(150), inRect: CGRectMake(75, 250, 200, 200))
writeDateText(date.addDaysToDateAndReturnNormalString(120), inRect: CGRectMake(75, 330, 200, 200))
writeDateText(date.addDaysToDateAndReturnNormalString(120), inRect: CGRectMake(75, 450, 200, 200))
writeDateText(date.addDaysToDateAndReturnNormalString(60), inRect: CGRectMake(75, 520, 200, 200))
}
private class func writePageThreeTextForDate(date: NSDate, forContext context: CGContextRef, andDocumentRef docReg: CGPDFDocumentRef, andPaperSize size: CGRect) {
var page : CGPDFPageRef = CGPDFDocumentGetPage(docReg, 3)
CGContextDrawPDFPage(context, page)
CGContextTranslateCTM(context, 0.0, size.height)
CGContextScaleCTM(context, 1.0, -1.0)
writeDateText(date.addDaysToDateAndReturnNormalString(30), inRect: CGRectMake(75, 160, 200, 200))
writeDateText(date.addDaysToDateAndReturnNormalString(1), inRect: CGRectMake(75, 215, 200, 200))
}
private class func writeText(text: String, inRect rect: CGRect) {
let font = UIFont(name: "Courier", size: 16)
let text: String = text
let rectText = rect
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
let textColor = UIColor.blackColor()
let textFontAttributes = [
NSFontAttributeName : font!,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle,
]
text.drawInRect(rectText, withAttributes: textFontAttributes)
}
private class func writeDateText(text:String, inRect rect: CGRect) {
let font = UIFont(name: "Courier", size: 12)
let text: String = text
let rectText = rect
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
let textColor = UIColor.blackColor()
let textFontAttributes = [
NSFontAttributeName : font!,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle,
]
text.drawInRect(rectText, withAttributes: textFontAttributes)
}
}
@lenichols
Copy link

How do you use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment