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
TheDrawingContextobject 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 -> PDFPageParameters
drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject. -
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) -> PDFPageParameters
widthThe width of the page.
heightThe height of the page.
Return Value
A
PDFPageobject. -
Creates a new page of the specified width and height and adds it after the last page of the document.
Warning
TheDrawingContextobject 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 -> PDFPageParameters
widthThe width of the page.
heightThe height of the page.
drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject. -
Creates a new page of the specified size and direction and adds it after the last page of the document.
Declaration
Parameters
sizeA predefined page-size value.
directionThe direction of the page (portrait or landscape).
Return Value
A
PDFPageobject. -
Creates a new page of the specified size and direction and adds it after the last page of the document.
Warning
TheDrawingContextobject 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 -> PDFPageParameters
sizeA predefined page-size value.
directionThe direction of the page (portrait or landscape).
drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject. -
Creates a new page and inserts it just before the page with the specified index.
Declaration
Swift
@discardableResult public func insertPage(atIndex index: Int) -> PDFPageParameters
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.Return Value
A
PDFPageobject. -
Creates a new page and inserts it just before the page with the specified index.
Warning
TheDrawingContextobject 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 -> PDFPageParameters
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject. -
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) -> PDFPageParameters
widthThe width of the page.
heightThe height of the page.
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.Return Value
A
PDFPageobject. -
Creates a new page of the specified width and height and inserts it just before the page with the specified index.
Warning
TheDrawingContextobject 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 -> PDFPageParameters
widthThe width of the page.
heightThe height of the page.
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject. -
Creates a new page of the specified size and direction and inserts it just before the page with the specified index.
Declaration
Parameters
sizeA predefined page-size value.
directionThe direction of the page (portrait or landscape).
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.Return Value
A
PDFPageobject. -
Creates a new page of the specified size and direction and inserts it just before the page with the specified index.
Warning
TheDrawingContextobject 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 -> PDFPageParameters
sizeA predefined page-size value.
directionThe direction of the page (portrait or landscape).
indexThe index at which the new page will appear.
indexmust be a valid index of the arraypagesor equal to itsendIndexproperty.drawThe drawing operations to perform, or
nilif no drawing should be performed.Return Value
A
PDFPageobject.
-
Renders the document and returns its contents.
Declaration
Swift
public func getData() -> DataReturn 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
stylePDFDocument.PageNumberStyleenum casestartingPageThe first page that applies this labeling range.
firstPageNumberThe first page number to use.
prefixThe prefix for the page label. Default is
nil.
-
Loads a TrueType font from
dataand registers it to a document.Declaration
Swift
public func loadTrueTypeFont(from data: Data, embeddingGlyphData: Bool) throws -> FontParameters
dataContents of a
.ttffile.embeddingGlyphDataIf 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 -> FontParameters
dataContents of a
.ttcfile.indexThe index of the font to be loaded.
embeddingGlyphDataIf 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.invalidCompressionModeif the provided compression mode was invalid.Declaration
Swift
public func setCompressionMode(to mode: CompressionMode) throwsParameters
modeThe mode of compression (may be combined).
-
Sets a password for the document. If the password is set, the document contents are encrypted.
Throws
PDFError.encryptionInvalidPasswordif the owner password is zero length string or same value as the user password;PDFError.invalidEncryptionKeyLengthif an invalid key length was specified.Declaration
Swift
public func setPassword(owner: String, user: String? = nil, permissions: Permissions? = nil, encryptionMode: EncryptionMode? = nil) throwsParameters
ownerThe 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.
userThe password for the user of the document. May be set to
nilor zero length string.permissionsThe permission flags for the document. Default is
nil.encryptionModeThe 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
View on GitHub
PDFDocument Class Reference