Document Sharing

PSPDFKit allows you to securely share PDF documents with other apps. Additionally, you can print or send them via the PSPDFDocumentSharingViewController, which provides several methods and configurations for that purpose. You can share any document by tapping the share button in the navigation bar and then choosing your preferred way of sharing the document.

Sharing Options

You can configure sharing options using the PSPDFDocumentSharingOptions, which lets you choose document processing options for flattening or removing annotations on the shared document or extracting the current page, a range of pages or all pages. PSPDFDocumentSharingOptions can be set either when creating a PSPDFViewController using the sharingOptions properties of PSPDFConfiguration for each individual sharing service or you can modify the sharing options parameter from the PSPDFDocumentSharingViewControllerDelegate callbacks. You can choose which pages or which page range you want to export, how you want the annotations to be configured and if the document should be shared as the original document or as an image.

Copy
1
2
3
4
5
let configuration = PSPDFConfiguration { builder in
    // Set the default to share the current page of document via Message
    // Enable image sharing options so that we can share an image via Message
    builder.messageSharingOptions = [.currentPageOnly, .image]
}
Copy
1
2
3
4
5
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    // Set the default to share the current page of document via Message
    // Enable image sharing options so that we can share an image via Message
    builder.messageSharingOptions = PSPDFDocumentSharingOptionCurrentPageOnly | PSPDFDocumentSharingOptionImage;
}];

Sharing Controller

In the sharing view controller, the user is presented with options on how to export the document. You can select the pages, change how annotations are exported, and choose if you want to share an image or the document itself. To edit or restrict some of the default sharing options, you can do so by conforming to the PSPDFViewControllerDelegate and edit the options you want in the callback methods.

The following is a brief example showing how to only offer the option to flatten annotations and sharing the current page only when printing. You can find a full example of how to do this in our catalog app inside the PSCPrintDefaultsExample.

Copy
1
2
3
4
5
6
7
8
9
10
11
12
func pdfViewController(_ pdfController: PSPDFViewController, shouldShow controller: UIViewController, options: [String : Any]? = nil, animated: Bool) -> Bool {
    // Intercept and customize the document sharing view controller.
    if let sharingController = controller as? PSPDFDocumentSharingViewController {

        // Only modify if we're mailing. This controller is used for printing or open in as well.
        if sharingController.delegate is PSPDFMailCoordinator {
            sharingController.selectedOptions = [.flattenAnnotations, .currentPageOnly]
        }
    }

    return true
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController shouldShowController:(UIViewController *)controller options:(nullable NSDictionary<NSString *, id> *)options animated:(BOOL)animated {
    // Intercept and customize the document sharing view controller.
    if ([controller isKindOfClass:PSPDFDocumentSharingViewController.class]) {
        PSPDFDocumentSharingViewController *sharingController = (PSPDFDocumentSharingViewController *)controller;

        // Only modify if we're mailing. This controller is used for printing or open in as well.
        if ([sharingController.delegate isKindOfClass:PSPDFMailCoordinator.class]) {
            sharingController.selectedOptions = PSPDFDocumentSharingOptionAnnotationsSummary|PSPDFDocumentSharingOptionCurrentPageOnly;
        }
    }

    return YES;
}

Processing Options

You have the ability to edit processing options like editing the file name, showing a progress screen, or altering the files themselves to override the delegate callbacks from PSPDFDocumentSharingViewControllerDelegate in a subclass of PSPDFDocumentSharingCoordinator like PSPDFMailCoordinator or PSPDFPrintCoordinator.

The following is a brief example showing how to edit the file name of the shared document. You can find a full exmaple inside the CustomSharingFileNamesExample of our catalog app.

Copy
1
2
3
4
override func documentSharingViewController(_ shareController: PSPDFDocumentSharingViewController, willShare files: [PSPDFFile]) -> [PSPDFFile] {
    let newFiles = files.map { PSPDFFile(name: "PSPDFKit", url: $0.fileURL, data: $0.fileData) }
    return newFiles
}
Copy
1
2
3
4
5
6
7
8
9
10
- (NSArray<PSPDFFile *> *)documentSharingViewController:(PSPDFDocumentSharingViewController *)shareController willShareFiles:(NSArray<PSPDFFile *> *)files {
    NSMutableArray<PSPDFFile *> *newFiles = [NSMutableArray<PSPDFFile *> array];

    [files enumerateObjectsUsingBlock:^(PSPDFFile *file, NSUInteger idx, BOOL *stop) {
        PSPDFFile *newFile = [[PSPDFFile alloc] initWithName: @"PSPDFKit" URL: file.fileURL data: file.fileData];
        [newFiles addObject:newFile];
    }];

    return newFiles;
}

If you want to know more about how processing a document works, read this article.