PSPDFDocumentEditor


@interface PSPDFDocumentEditor : NSObject

Manages document editing. Supports operations such as remove, move, rotate and add page.

Note

This class requires the Document Editor component to be enabled for your license.
  • Starts an editing session on the document or a new blank editing session.

    Warning

    Document editing is currently only supported for documents that contain no annotations or have all annotations embedded in the PDF file. Other annotation providers are not yet supported.

    Declaration

    Objective-C

    - (nullable instancetype)initWithDocument:(nullable PSPDFDocument *)document;

    Swift

    init?(document: PSPDFDocument?)

    Parameters

    document

    The document used to initialize the document editor or nil. If nil, the document editor is initialized with zero pages. If a document is used, it must be a valid document. The changes won’t be visible on the document until the document editor is saved.

  • Creates a document editor without a document. Use this to create a new document from scratch.

    Note

    You need to add at least one page before saving with saveToPath:withCompletionBlock:. An empty document has 0 pages, which does not constitute a valid PDF document.

    See

    initWithDocument:

    Declaration

    Objective-C

    - (nonnull instancetype)init;

    Swift

    convenience init()
  • Reference to the backing document. Will remain nonnull if initWithDocument: with a document was used.

    See

    initWithDocument:

    Declaration

    Objective-C

    @property (readonly, nonatomic, nullable) PSPDFDocument *document;

    Swift

    var document: PSPDFDocument? { get }
  • Allows you to set security options for saving.

    Declaration

    Objective-C

    @property (assign, readwrite, nonatomic, nullable)
        PSPDFDocumentSecurityOptions *securityOptions;

    Swift

    var securityOptions: PSPDFDocumentSecurityOptions? { get set }
  • Adds a document editor delegate to the subscriber list.

    Note

    Delegates are weakly retained, but be a good citizen and manually deregister.

    Declaration

    Objective-C

    - (void)addDelegate:(nonnull id<PSPDFDocumentEditorDelegate>)delegate;

    Swift

    func add(_ delegate: PSPDFDocumentEditorDelegate)
  • Removes a document editor delegate from the subscriber list.

    Declaration

    Objective-C

    - (BOOL)removeDelegate:(nonnull id<PSPDFDocumentEditorDelegate>)delegate;

    Swift

    func remove(_ delegate: PSPDFDocumentEditorDelegate) -> Bool
  • Returns the page count of the edited Document. If you remove or add pages, this will reflect that change.

    Declaration

    Objective-C

    @property (readonly, nonatomic) PSPDFPageCount pageCount;

    Swift

    var pageCount: PageCount { get }
  • Returns the page size, already rotated.

    Declaration

    Objective-C

    - (CGSize)pageSizeForPageAtIndex:(PSPDFPageIndex)pageIndex;

    Swift

    func pageSizeForPage(at pageIndex: PageIndex) -> CGSize
  • Adds multiple pages starting at the specified page index, with the configuration options specified in configuration.

    Adds multiple pages to the document, starting at the provided range’s location.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), the delegates are not going to be called and the return value will be an empty array.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)
      addPagesInRange:(NSRange)range
    withConfiguration:(nonnull PSPDFNewPageConfiguration *)configuration;

    Swift

    func addPages(in range: NSRange, with configuration: PSPDFNewPageConfiguration) -> [PSPDFEditingChange]

    Parameters

    range

    The range in which the new pages should be added. The provided range’s location should be within bounds of the document’s page count.

    configuration

    The configuration used to create each one of the new pages.

    Return Value

    Array of PSPDFEditingChange objects describing the changes performed by this method.

  • Combines all editing actions inside updateBlock into one.

    Aggregates all the changes made by the actions inside the block and notifies the delegate.

    Call updatesCompletedBlock when you’re done describing the updates to be performed.

    Declaration

    Objective-C

    - (void)groupUpdates:(nonnull void (^)(void (^_Nonnull)(BOOL)))updateBlock;

    Swift

    func groupUpdates(_ updateBlock: @escaping ((Bool) -> Void) -> Void)

    Parameters

    updateBlock

    Block that groups all the actions to be performed by the editor.

  • Combines all editing actions inside updateBlock into one.

    If a completionBlock is provided the editor’s delegates are not going to be called, and the aggregated updates will be delivered on the completion block.

    Call updatesCompletedBlock when you’re done describing the updates to be performed.

    Declaration

    Objective-C

    - (void)groupUpdates:(nonnull void (^)(void (^_Nonnull)(BOOL)))updateBlock
              completion:
                  (nullable void (^)(NSArray<PSPDFEditingChange *> *_Nonnull))
                      completionBlock;

    Swift

    func groupUpdates(_ updateBlock: @escaping ((Bool) -> Void) -> Void, completion completionBlock: (([PSPDFEditingChange]) -> Void)? = nil)

    Parameters

    updateBlock

    Block that groups all the actions to be performed by the editor.

    completionBlock

    Block to be executed when the editor finishes the changes. Parameter is a list of PSPDFEditingChange.

  • Moves pages at the given page indexes to a new page index.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), returns an empty array and the delegates are not called.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)
    movePages:(nonnull NSIndexSet *)pageIndexes
           to:(PSPDFPageIndex)destination;

    Swift

    func movePages(_ pageIndexes: IndexSet, to destination: PageIndex) -> [PSPDFEditingChange]
  • Removes pages at the given page indexes.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), returns an empty array and the delegates are not called.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)removePages:
        (nonnull NSIndexSet *)pageIndexes;

    Swift

    func removePages(_ pageIndexes: IndexSet) -> [PSPDFEditingChange]
  • Duplicates pages at the given page indexes. The duplicated pages will be inserted exactly after the original page.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), returns an empty array and the delegates are not called.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)duplicatePages:
        (nonnull NSIndexSet *)pageIndexes;

    Swift

    func duplicatePages(_ pageIndexes: IndexSet) -> [PSPDFEditingChange]
  • Rotates the pages with the given page indexes. Rotation can be 0, 90, 180 and 270. Clockwise and counter-clockwise (depending on the sign). The rotation is added to the current page rotation value.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), returns an empty array and the delegates are not called.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)rotatePages:
                                                   (nonnull NSIndexSet *)pageIndexes
                                                  rotation:(NSInteger)rotation;

    Swift

    func rotatePages(_ pageIndexes: IndexSet, rotation: Int) -> [PSPDFEditingChange]
  • Undoes the last action and returns information about what changed.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nullable NSArray<PSPDFEditingChange *> *)undo;

    Swift

    func undo() -> [PSPDFEditingChange]?
  • Redo the last undo and returns information about what changed.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nullable NSArray<PSPDFEditingChange *> *)redo;

    Swift

    func redo() -> [PSPDFEditingChange]?
  • Checks if you can redo.

    Declaration

    Objective-C

    @property (readonly, nonatomic) BOOL canRedo;

    Swift

    var canRedo: Bool { get }
  • Checks if you can undo.

    Declaration

    Objective-C

    @property (readonly, nonatomic) BOOL canUndo;

    Swift

    var canUndo: Bool { get }
  • Discards all changes and clears the undo and redo stack. Calls documentEditor:didPerformChanges: with a nil `changes array.

    Note

    Any cached state on the caller side should be reset after calling this.

    Declaration

    Objective-C

    - (void)reset;

    Swift

    func reset()
  • Specifies if it is possible to overwrite the PDF file represented by document by invoking -[PSPDFDocumentEditor save]. Returns YES if the document is backed by a single document provider with a valid and writable file path.

    Declaration

    Objective-C

    @property (readonly, nonatomic) BOOL canSave;

    Swift

    var canSave: Bool { get }
  • Overwrites the document PDF file and clears the document caches.

    Note

    If the PSPDFDocument referenced by document is currently displayed on a PDFViewController, you should call -[PDFViewController reloadData] after saving.

    Warning

    Don’t make any assumptions about the execution context of block. Can be called on a background queue.

    See

    canSave

    Declaration

    Objective-C

    - (void)saveWithCompletionBlock:(nullable PSPDFDocumentEditorSaveBlock)block;

    Swift

    func save(completionBlock block: PSPDFDocumentEditorSaveBlock? = nil)

    Parameters

    block

    If successful, returns a reference to the current document with cleared caches. Otherwise an error will be available.

  • Saves the modified document to a new PDF file at path.

    Note

    This does not affect the PSPDFDocument referenced by document.

    Warning

    Don’t make any assumptions about the execution context of block. Can be called on a background queue.

    Declaration

    Objective-C

    - (void)saveToPath:(nonnull NSString *)path
        withCompletionBlock:(nullable PSPDFDocumentEditorSaveBlock)block;

    Swift

    func save(toPath path: String, withCompletionBlock block: PSPDFDocumentEditorSaveBlock? = nil)

    Parameters

    path

    The destination path for the new document. Should be a directory to which the application can write to.

    block

    If successful, returns a new document that is configured for the given path. Otherwise an error will be available.

  • Saves the pages listed in pageIndexes to a new PDF at path.

    Note

    This does not affect the PSPDFDocument referenced by document.

    Warning

    Don’t make any assumptions about the execution context of block. Can be called on a background queue.

    Declaration

    Objective-C

    - (void)exportPages:(nonnull NSIndexSet *)pageIndexes
                     toPath:(nonnull NSString *)path
        withCompletionBlock:(nullable PSPDFDocumentEditorSaveBlock)block;

    Swift

    func exportPages(_ pageIndexes: IndexSet, toPath path: String, withCompletionBlock block: PSPDFDocumentEditorSaveBlock? = nil)

    Parameters

    pageIndexes

    A set of indexes corresponding to pages that should copied to the new document. All indexes need to be bounded by pageCount.

    path

    The destination path for the new document. Should be a directory to which the application can write to.

    block

    If successful, returns a new document that is configured for the given path. Otherwise an error will be available.

  • Imports pages from a given document into the currently edited document and returns a progress.

    As with any other document editor changes, the added pages are not preserved until a document editor save is invoked. The provided documents will be copied internally and preserved until a save is invoked or the document editor gets deallocated.

    Note

    If called inside an edition group (see -[PSPDFDocumentEditor groupUpdates:completion:]), the delegates are not going to be called and the first parameter on the completion block will be an empty array.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should use the main queue as the queue parameter of this method (or pass in nil). Use the completion block or delegate calls to apply the resulting changes (or perform a refresh). This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSProgress *)importPagesTo:(PSPDFPageIndex)index
                             fromDocument:(nonnull PSPDFDocument *)sourceDocument
                      withCompletionBlock:
                          (nullable PSPDFDocumentEditorImportBlock)block
                                    queue:(nullable dispatch_queue_t)queue;

    Swift

    func importPages(to index: PageIndex, from sourceDocument: PSPDFDocument, withCompletionBlock block: PSPDFDocumentEditorImportBlock?, queue: DispatchQueue?) -> Progress

    Parameters

    index

    The insertion index for the new sourceDocument pages amongst the current pages.

    sourceDocument

    The source document. Needs to be a fileURL backed PSPDFDocument.

    block

    A optional block, called when the import completes. Invoked on the provided queue.

    queue

    The queue that should be used to invoke the block and delegate calls on. If nil, the main queue will be used.

  • Returns the rendered page as an UIImage with custom scale.

    Declaration

    Objective-C

    - (nullable UIImage *)imageForPageAtIndex:(PSPDFPageIndex)pageIndex
                                         size:(CGSize)size
                                        scale:(CGFloat)scale;

    Swift

    func imageForPage(at pageIndex: PageIndex, size: CGSize, scale: CGFloat) -> NSImage?
  • Deprecated

    Deprecated in PSPDFKit 7.7 for iOS. Please use -[PSPDFDocumentEditor addPagesInrange:withConfiguration]

    Adds a new page at the specified page index, with the configuration options specified in configuration.

    Note

    If the document editor is used to drive a UI (e.g, a UICollectionView), you should call this method on the main thread and apply the resulting changes (or perform a refresh) to your UI immediately after the method returns. This will ensure that the UI state and the document editor model representation are always in sync.

    Declaration

    Objective-C

    - (nonnull NSArray<PSPDFEditingChange *> *)
            addPageAt:(PSPDFPageIndex)index
    withConfiguration:(nonnull PSPDFNewPageConfiguration *)configuration;

    Swift

    func addPage(at index: PageIndex, with configuration: PSPDFNewPageConfiguration) -> [PSPDFEditingChange]