PDFDocument
public final class PDFDocument
A handle to operate on a document object.
-
An array of pages in the document. Initially is empty. Use the
addPage()
,addPage(width:height:)
,addPage(size:direction:)
orinsertPage(atIndex:)
,insertPage(width:height:atIndex:)
,insertPage(size:direction:atIndex:)
methods to add pages to your document and populate this array.Declaration
Swift
public private(set) var pages: [PDFPage]
-
The fonts loaded in the document. Initially is empty. Use the
loadTrueTypeFont(from:embeddingGlyphData:)
orloadTrueTypeFontFromCollection(from:index:embeddingGlyphData:)
methods to load fonts.This set does not include the base 14 fonts (see predefined values of
Font
) that can be used in the document without loading any external fonts.Declaration
Swift
public private(set) var fonts: Set<Font>
-
Creates an instance of a document object.
Declaration
Swift
public init()
Return Value
An instance of a document.
-
Creates a new page and adds it after the last page of the document.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func addPage(_ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object. -
Creates a new page of the specified width and height and adds it after the last page of the document.
Declaration
Swift
@discardableResult public func addPage(width: Float, height: Float) -> PDFPage
Parameters
width
The width of the page.
height
The height of the page.
Return Value
A
PDFPage
object. -
Creates a new page of the specified width and height and adds it after the last page of the document.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func addPage(width: Float, height: Float, _ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
width
The width of the page.
height
The height of the page.
draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object. -
Creates a new page of the specified size and direction and adds it after the last page of the document.
Declaration
Parameters
size
A predefined page-size value.
direction
The direction of the page (portrait or landscape).
Return Value
A
PDFPage
object. -
Creates a new page of the specified size and direction and adds it after the last page of the document.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func addPage(size: PDFPage.Size, direction: PDFPage.Direction, _ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
size
A predefined page-size value.
direction
The direction of the page (portrait or landscape).
draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object. -
Creates a new page and inserts it just before the page with the specified index.
Declaration
Swift
@discardableResult public func insertPage(atIndex index: Int) -> PDFPage
Parameters
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.Return Value
A
PDFPage
object. -
Creates a new page and inserts it just before the page with the specified index.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func insertPage(atIndex index: Int, _ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object. -
Creates a new page of the specified width and height and inserts it just before the page with the specified index.
Declaration
Swift
@discardableResult public func insertPage(width: Float, height: Float, atIndex index: Int) -> PDFPage
Parameters
width
The width of the page.
height
The height of the page.
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.Return Value
A
PDFPage
object. -
Creates a new page of the specified width and height and inserts it just before the page with the specified index.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func insertPage(width: Float, height: Float, atIndex index: Int, _ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
width
The width of the page.
height
The height of the page.
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object. -
Creates a new page of the specified size and direction and inserts it just before the page with the specified index.
Declaration
Parameters
size
A predefined page-size value.
direction
The direction of the page (portrait or landscape).
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.Return Value
A
PDFPage
object. -
Creates a new page of the specified size and direction and inserts it just before the page with the specified index.
Warning
TheDrawingContext
object should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
@discardableResult public func insertPage(size: PDFPage.Size, direction: PDFPage.Direction, atIndex index: Int, _ draw: (DrawingContext) throws -> Void) rethrows -> PDFPage
Parameters
size
A predefined page-size value.
direction
The direction of the page (portrait or landscape).
index
The index at which the new page will appear.
index
must be a valid index of the arraypages
or equal to itsendIndex
property.draw
The drawing operations to perform, or
nil
if no drawing should be performed.Return Value
A
PDFPage
object.
-
Renders the document and returns its contents.
Declaration
Swift
public func getData() -> Data
Return Value
The document’s contents
-
Determines how pages should be displayed. If this attribute is not set, the setting of the viewer application is used.
Declaration
Swift
public var pageLayout: PageLayout { get set }
-
Adds a page labeling range for the document. The page label is shown in the thumbnails view.
Example:
document.addPageLabel(.lowerRoman, fromPage: 0, startingWith: 1) document.addPageLabel(.decimal, fromPage: 4, startingWith: 1) document.addPageLabel(.decimal, fromPage: 7, startingWith: 8, withPrefix: "A-")
Result in a document with pages labeled:
i, ii, iii, iv, 1, 2, 3, A-8, A-9, …
Declaration
Swift
public func addPageLabel(_ style: PageNumberStyle, fromPage startingPage: Int, startingWith firstPageNumber: Int, withPrefix prefix: String = String())
Parameters
style
PDFDocument.PageNumberStyle
enum casestartingPage
The first page that applies this labeling range.
firstPageNumber
The first page number to use.
prefix
The prefix for the page label. Default is
nil
.
-
Loads a TrueType font from
data
and registers it to a document.Declaration
Swift
public func loadTrueTypeFont(from data: Data, embeddingGlyphData: Bool) throws -> Font
Parameters
data
Contents of a
.ttf
file.embeddingGlyphData
If this parameter is set to
true
, the glyph data of the font is embedded, otherwise only the matrix data is included in PDF file.Return Value
The loaded font.
-
Loads a TrueType font from a TrueType Collection and registers it to a document.
Declaration
Swift
public func loadTrueTypeFontFromCollection(from data: Data, index: Int, embeddingGlyphData: Bool) throws -> Font
Parameters
data
Contents of a
.ttc
file.index
The index of the font to be loaded.
embeddingGlyphData
If this parameter is set to
true
, the glyph data of the font is embedded, otherwise only the matrix data is included in PDF file.Return Value
The loaded font.
-
Set the mode of compression.
Throws
PDFError.invalidCompressionMode
if the provided compression mode was invalid.Declaration
Swift
public func setCompressionMode(to mode: CompressionMode) throws
Parameters
mode
The mode of compression (may be combined).
-
Sets a password for the document. If the password is set, the document contents are encrypted.
Throws
PDFError.encryptionInvalidPassword
if the owner password is zero length string or same value as the user password;PDFError.invalidEncryptionKeyLength
if an invalid key length was specified.Declaration
Swift
public func setPassword(owner: String, user: String? = nil, permissions: Permissions? = nil, encryptionMode: EncryptionMode? = nil) throws
Parameters
owner
The password for the owner of the document. The owner can change the permission of the document. Zero length string and the same value as user password are not allowed.
user
The password for the user of the document. May be set to
nil
or zero length string.permissions
The permission flags for the document. Default is
nil
.encryptionMode
The encryption mode. Ups the version of PDF to 1.4 when the mode is set to
.r3
. Default isnil
.
-
The metadata of the document: an author, keywords, creation date etc.
Declaration
Swift
public var metadata: PDFDocument.Metadata
-
The mode of compression for a document.
See moreDeclaration
Swift
public struct CompressionMode : OptionSet
-
The encryption mode to use for a document.
See moreDeclaration
Swift
public enum EncryptionMode
-
This structure encapsulates the metadata of a document.
See moreDeclaration
Swift
public struct Metadata : Equatable
-
Determines how pages in the document should be displayed.
See moreDeclaration
Swift
public enum PageLayout : UInt32
-
Determines a page labeling style of the document.
See moreDeclaration
Swift
public enum PageNumberStyle : UInt32
-
Permission flags to use in a document.
See moreDeclaration
Swift
public struct Permissions : OptionSet