Controlling PDF Document Editing

PDF documents can be modified in several ways, like adding annotations or filling forms. In addition to the standard PDF restrictions (see SECURED Documents), PSPDFKit gives you more control over how to restrict certain modifications of PDF documents.

Annotations

The following section assumes that you are familiar with annotations. See Introduction to Annotations for more details.

Disable the Modification of All Annotation Types

You can disable all annotation modifications using PSPDFConfiguration by setting the editableAnnotationTypes property to nil. This will prevent users from adding new annotations and editing existing ones:

1
2
3
let configuration = PSPDFConfiguration { (builder) in
    builder.editableAnnotationTypes = nil
}
Copy
1
2
3
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    builder.editableAnnotationTypes = nil;
}];

Enable Modifications Only for Specific Annotation Types

You can control which annotation types are editable or visible, and you can specify their type in editableAnnotationTypes. For example, you can only allow the modification of Ink annotations:

Copy
1
2
3
let configuration = PSPDFConfiguration { (builder) in
    builder.editableAnnotationTypes = [PSPDFAnnotationStringInk] // Only Ink annotations are editable and visible
}
Copy
1
2
3
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    builder.editableAnnotationTypes = [NSSet setWithArray:@[PSPDFAnnotationStringInk]]; // Only Ink annotations are editable and visible
}];

Disable Adding New Annotations but Allow Modification of Existing Ones

Hiding your PSPDFViewController's annotationButtonItem from the toolbar will prevent users from adding new annotations, but it will not stop them from editing or deleting existing ones:

Copy
1
2
// Notice that `pdfController.annotationButtonItem` is not included.
pdfController.navigationItem.setRightBarButtonItems([pdfController.thumbnailsButtonItem, pdfController.outlineButtonItem, pdfController.searchButtonItem, pdfController.activityButtonItem], for: .document, animated: false)
Copy
1
2
// Notice that `pdfController.annotationButtonItem` is not included.
[pdfController.navigationItem setRightBarButtonItems:@[pdfController.thumbnailsButtonItem, pdfController.annotationButtonItem, pdfController.outlineButtonItem, pdfController.searchButtonItem, pdfController.activityButtonItem] forViewMode:PSPDFViewModeDocument animated:NO];

For more information, see the Configuring Editable/Visible Annotation types guide.

Disable the Modification of a Specific Annotation

You disable the modification of a specific annotation by updating its flags property to use PSPDFAnnotationFlagReadOnly, like so:

1
2
// Update the annotation flags 
annotation.flags.update(with: .readOnly)
1
2
// Update the annotation flags
annotation.flags |= ~PSPDFAnnotationFlagReadOnly;

For more information, see the Annotation Flags guide.

Disable Adding Annotations from Menu Items

Annotations can be crated via menu items. You can configure annotation creation using the createAnnotationMenuEnabled property from PSPDFConfiguration.

1
2
3
let configuration = PSPDFConfiguration { (builder) in
    builder.isCreateAnnotationMenuEnabled = false
}
Copy
1
2
3
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    builder.createAnnotationMenuEnabled = NO;
}];

Annotations can also be modified by copying and pasting them or by selecting text or images. For example, when you select text in a document you can highlight it. Here's how to disable the highlight menu item when selecting text:

Copy
1
2
3
public func pdfViewController(_ pdfController: PSPDFViewController, shouldShow menuItems: [PSPDFMenuItem], atSuggestedTargetRect rect: CGRect, for annotations: [PSPDFAnnotation]?, in annotationRect: CGRect, on pageView: PSPDFPageView) -> [PSPDFMenuItem] {
    return menuItems.filter { $0.identifier != PSPDFAnnotationMenuHighlight }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
- (NSArray<PSPDFMenuItem *> *)pdfViewController:(PSPDFViewController *)pdfController shouldShowMenuItems:(NSArray<PSPDFMenuItem *> *)menuItems atSuggestedTargetRect:(CGRect)rect forSelectedText:(NSString *)selectedText inRect:(CGRect)textRect onPageView:(PSPDFPageView *)pageView {
    NSMutableArray *newMenuItems = [menuItems mutableCopy];
    // Remove the highlight menu item
    for (PSPDFMenuItem *menuItem in menuItems) {
        if ([menuItem isKindOfClass:PSPDFMenuItem.class] && [menuItem.identifier isEqualToString:PSPDFAnnotationMenuHighlight]) {
            [newMenuItems removeObjectIdenticalTo:menuItem];
            break;
        }
    }
    return newMenuItems;
}

You can configure the menu items in following PSPDFViewControllerDelegate methods:

See the Customizing Menus article for more details.

Disable Drag and Drop

PDFs can be modified using drag and drop. For example, you can drop text, images, and even other PDF documents. You can disable drag and drop by configuring your PSPDFDragAndDropConfiguration:

Copy
1
2
3
4
5
6
7
let dragAndDropConfiguration = PSPDFDragAndDropConfiguration { (builder) in
    builder.acceptedDropTypes = []
    builder.allowedDropTargets = []
}
let configuration = PSPDFConfiguration { (builder) in
    builder.dragAndDropConfiguration = dragAndDropConfiguration
}
Copy
1
2
3
4
5
6
7
PSPDFDragAndDropConfiguration *dragAndDropConfiguration = [PSPDFDragAndDropConfiguration configurationWithBuilder:^(PSPDFDragAndDropConfigurationBuilder * builder) {
    builder.acceptedDropTypes = PSPDFDropTypeNone;
    builder.allowedDropTargets = PSPDFDropTargetNone;
}];
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    builder.dragAndDropConfiguration = dragAndDropConfiguration;
}];

For more information, see the Drag and Drop guide.

Forms

The following section assumes that you are familiar with Forms. See Introduction to Forms for more details.

Disable All Forms Interactions

You can disable all forms interactions and modifications using editableAnnotationTypes:

Copy
1
2
3
4
5
let configuration = PSPDFConfiguration { builder in
    var editableAnnotationTypes = builder.editableAnnotationTypes
    editableAnnotationTypes?.remove(PSPDFAnnotationString.widget)
    builder.editableAnnotationTypes = editableAnnotationTypes
}
Copy
1
2
3
4
5
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    NSMutableSet *editableAnnotationTypes = [builder.editableAnnotationTypes mutableCopy];
    [editableAnnotationTypes removeObject:PSPDFAnnotationStringWidget];
    builder.editableAnnotationTypes = editableAnnotationTypes;
}];

Disable Specific Form Element Types

You can specify which form elements can be modified. For example, you can disable the modification of all text field form elements and allow all other form elements to be editable:

Copy
1
2
3
4
5
6
let document = PSPDFDocument(url: documentURL)

// Loop through all form elements from a given document and only disable text field form elements from being modified
for formElement: PSPDFFormElement in (document.formParser?.forms)! where formElement is PSPDFTextFieldFormElement {
    formElement.isEditable = false;
}
Copy
1
2
3
4
5
6
7
8
PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:documentURL];

// Loop through all form elements from a given document and only disable text field form elements from being modified
for (PSPDFFormElement *formElement in document.formParser.forms) {
    if ([formElement isKindOfClass:PSPDFTextFieldFormElement.class]) {
        formElement.editable = NO;
    }
}

Restricting Features

Document Editor

PDF documents can be modified using the Document Editor feature which allows new page creation, page duplication, reordering, rotation or deletion of pages. See the Document Editor guide for more details.