Controlling PDF Document Editing
There are several ways to modify PDF documents — for instance, by adding annotations or filling forms. The PDF format has standard restrictions (see the Secured Documents guide), but in addition to those built-in restrictions, PSPDFKit gives you control over how to restrict certain modifications of PDF documents.
Annotations
The following section assumes you’re familiar with annotations. If not, first see the Introduction to Annotations guide for more details.
Disable the Modification of All Annotation Types
You can disable all annotation modifications using PDFConfiguration
by setting the editableAnnotationTypes
property to nil
. This will prevent users from adding new annotations and editing existing ones:
let configuration = PDFConfiguration { $0.editableAnnotationTypes = nil }
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) { builder.editableAnnotationTypes = nil; }];
Enable Modifications Only for Specific Annotation Types
You can control which annotation types are editable and you can specify their types in editableAnnotationTypes
. For example, you can allow only the modification of ink annotations:
let configuration = PDFConfiguration { $0.editableAnnotationTypes = [.ink] }
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) { builder.editableAnnotationTypes = [NSSet setWithArray:@[PSPDFAnnotationStringInk]]; // Only ink annotations are editable. }];
Disable Adding New Annotations but Allow Modification of Existing Ones
Hiding your PDFViewController
’s annotationButtonItem
from the toolbar will prevent users from adding new annotations, but it will not stop them from editing or deleting existing ones:
// Notice that `pdfController.annotationButtonItem` is not included. pdfController.navigationItem.setRightBarButtonItems([pdfController.thumbnailsButtonItem, pdfController.outlineButtonItem, pdfController.searchButtonItem, pdfController.activityButtonItem], for: .document, animated: false)
// Notice that `pdfController.annotationButtonItem` is not included. [pdfController.navigationItem setRightBarButtonItems:@[pdfController.thumbnailsButtonItem, 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 can disable the modification of a specific annotation by updating its flags
property to use Annotation.Flag.readOnly
, like so:
// Update the annotation flags. annotation.flags.update(with: .readOnly)
// Update the annotation flags. annotation.flags |= ~PSPDFAnnotationFlagReadOnly;
For more information, see the Annotation Flags guide.
Disable Adding Annotations from Menu Items
Annotations can be created via menu items. You can configure annotation creation using the isCreateAnnotationMenuEnabled
property from PDFConfiguration
:
let configuration = PDFConfiguration { $0.isCreateAnnotationMenuEnabled = false }
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:
public func pdfViewController(_ pdfController: PDFViewController, shouldShow menuItems: [MenuItem], atSuggestedTargetRect rect: CGRect, for annotations: [Annotation]?, in annotationRect: CGRect, on pageView: PDFPageView) -> [MenuItem] { return menuItems.filter { $0.identifier != TextMenu.annotationMenuHighlight.rawValue } }
- (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 menu items in the following PDFViewControllerDelegate
methods:
-
pdfViewController(_:shouldShow:atSuggestedTargetRect:forSelectedText:in:on:)
-
pdfViewController(_:shouldShow:atSuggestedTargetRect:forSelectedImage:in:on:)
-
pdfViewController(_:shouldShow:atSuggestedTargetRect:for:in:on:)
See the Customizing Menus guide for more details.
Disable Drag and Drop
PDFs can be modified using drag and drop. For example, you can drag and drop text, images, and even other PDF documents. You can disable drag and drop by configuring your DragAndDropConfiguration
:
let dragAndDropConfiguration = DragAndDropConfiguration { $0.acceptedDropTypes = [] $0.allowedDropTargets = [] } let configuration = PDFConfiguration { $0.dragAndDropConfiguration = dragAndDropConfiguration }
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 you are familiar with forms. If not, first see the Introduction to Forms guide for more details.
Disable All Form Interactions
You can disable all form interactions and modifications using editableAnnotationTypes
:
let configuration = PDFConfiguration { var editableAnnotationTypes = $0.editableAnnotationTypes editableAnnotationTypes?.remove(.widget) $0.editableAnnotationTypes = editableAnnotationTypes }
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:
let document = Document(url: documentURL) // Loop through all form elements from a given document and only disable text field form elements from being modified. for formElement: FormElement in (document.formParser?.forms)! where formElement is TextFieldFormElement { formElement.isEditable = false; }
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; and the reordering, rotation, or deletion of pages. See the Document Editing guide for more details.