-
Notifications
You must be signed in to change notification settings - Fork 53
/
ExampleCode.swift
60 lines (41 loc) · 3.06 KB
/
ExampleCode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import UIKit
import SimplePDF
let a4PaperSize = CGSize(width: 595, height: 842)
let pdf = SimplePDF(pageSize: a4PaperSize)
pdf.setContentAlignment(.Center)
// add logo image
let logoImage = UIImage(named:"simple_pdf_logo")!
pdf.addImage(logoImage)
pdf.addLineSpace(30)
pdf.setContentAlignment(.Left)
pdf.addText("Normal text follows by line separator")
pdf.addLineSeparator()
pdf.addLineSpace(20.0)
pdf.setContentAlignment(.Right)
pdf.addText("Text after set content alignment to .Right")
pdf.addLineSpace(20.0)
pdf.addText("Cras quis eros orci.\nLorem ipsum dolor sit amet, consectetur adipiscing elit.\nDonec mollis vitae mi ut lobortis.\nUt ultrices mi vel neque venenatis, ut efficitur metus eleifend. Sed pellentesque lobortis est quis maximus. Maecenas ultricies risus et enim consectetur, id lobortis ante porta. Quisque at euismod enim. Vestibulum faucibus purus non justo fringilla, sit amet iaculis ex pellentesque. Vestibulum eget vulputate diam, sit amet ornare sem. Duis at eros non tortor malesuada accumsan.\nNunc vel libero ut sapien dictum iaculis a vel odio. Quisque purus turpis, tristique auctor ex non, consectetur scelerisque lorem. Mauris est justo, sollicitudin sit amet nisi a, mattis posuere orci. Sed elementum est at est tristique gravida. Aliquam iaculis, metus facilisis varius viverra, nunc libero ultricies arcu, in accumsan sem nibh vel purus.")
pdf.addLineSpace(30)
pdf.setContentAlignment(.Center)
pdf.addText("Center Text")
pdf.addLineSpace(20.0)
pdf.addText("Cras varius leo ac lectus malesuada, ut rhoncus nunc blandit.\n In venenatis diam et vehicula suscipit.\n Aliquam in ante at dolor sodales consectetur ut semper quam.\n Vivamus massa purus, congue sed leo sed, lobortis consectetur lacus. Nunc sed tortor nec augue mattis faucibus. Sed malesuada metus in sapien efficitur, ut aliquet nisl lobortis. Vestibulum faucibus purus non justo fringilla, sit amet iaculis ex pellentesque. Vestibulum eget vulputate diam, sit amet ornare sem. Aliquam erat volutpat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin scelerisque posuere mi, non consequat mauris auctor a. Fusce lacinia auctor lectus a elementum.")
pdf.addLineSpace(30.0)
pdf.setContentAlignment(.Left)
let textString = "This is an example of long text. If the text doesn't fit in the current page. Simple pdf will draw a part of text, and automatically begin a new page to draw the remaining text. This process will be repeated until there's no text left to draw. "
pdf.addText(textString)
pdf.beginNewPage()
pdf.addText("Begin new page")
// Generate PDF data and save to a local file.
if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
let fileName = "example.pdf"
let documentsFileName = documentDirectories + "/" + fileName
let pdfData = pdf.generatePDFdata()
do{
try pdfData.writeToFile(documentsFileName, options: .DataWritingAtomic)
print("\nThe generated pdf can be found at:")
print("\n\t\(documentsFileName)\n")
}catch{
print(error)
}
}