Drag and Drop

When Drag and Drop was introduced in iOS 11, we made it a priority to support this capability and incorporate it, beginning with PSPDFKit 7 for iOS. Now users can drag and drop various elements from and to an application that integrates PSPDFKit.

Drag Interactions

There are various elements users can initiate a drag from. The following interactions are currently possible with PSPDFKit:

  • Dragging images embedded on a document page.
  • Dragging text from a document page.

Drop Interactions

Additionally, users can drop elements on a document page, and these elements are then converted into PDF annotations. Here is the list of drop interactions that are currently supported:

  • Dropping text on a document page. The text will be converted into a free text annotation.
  • Dropping an image on a document page. The image will be converted into an image stamp annotation.
  • Dropping a PDF on a document page. The PDF will be converted into an image stamp annotation of the first page of the dropped document.

Customizing Drag and Drop

All Drag and Drop interactions are enabled by default. However, you can selectively control specific Drag and Drop components using PSPDFDragAndDropConfiguration. This might be interesting for you if you provide an application that serves documents with sensitive data.

PSPDFDragAndDropConfiguration can be constructed the same way as the main PSPDFConfiguration class. To customize a drag and drop configuration, you can set a PSPDFDragAndDropConfiguration object to the dragAndDropConfiguration property of PSPDFConfiguration, like so:

Copy
1
2
3
4
5
6
7
let configuration = PSPDFConfiguration { builder in
    let dragAndDropConfiguration = PSPDFDragAndDropConfiguration { dragAndDropConfigurationBuilder in
        // Customize the drag and drop configuration
    }
    builder.dragAndDropConfiguration = dragAndDropConfiguration
}
let pdfController = PSPDFViewController(document: document, configuration: configuration)
Copy
1
2
3
4
5
6
7
PSPDFConfiguration *configuration = [PSPDFConfiguration configurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
    PSPDFDragAndDropConfiguration *dragAndDropConfiguration = [PSPDFDragAndDropConfiguration configurationWithBuilder:^(PSPDFDragAndDropConfigurationBuilder *dragAndDropConfigurationBuilder) {
        // Customize the drag and drop configuration
    }];
    builder.dragAndDropConfiguration = dragAndDropConfiguration;
}];
PSPDFViewController *pdfController = [[PSPDFViewController alloc] initWithDocument:document configuration:configuration];

Control Drag Elements

The configuration of elements that users can initiate a drag from can be controlled via allowedDragTypes. By default, all types are allowed.

If you only want to allow dragging text from a document, you can configure this as follows:

1
dragAndDropConfigurationBuilder.allowedDragTypes = .text
1
dragAndDropConfigurationBuilder.allowedDragTypes = PSPDFDragTypeText;

Control Drag Targets

Drags that were initiated from a source application can be dropped either into the same application or into another application.

To restrict dragging inside the source application, you can disable allowDraggingToExternalApps.

Control Drop Elements

Configuring elements that users can drop onto a document page can be controlled via allowedDropTypes. By default, all types are accepted.

If you only want to accept dropping images and PDFs onto a document, you can configure this as follows:

1
dragAndDropConfigurationBuilder.acceptedDropTypes = [.image, .PDF]
1
dragAndDropConfigurationBuilder.acceptedDropTypes = PSPDFDropTypeImage | PSPDFDropTypePDF;

Control Drop Targets

Drop elements can be dropped on document pages, which can be customized via allowedDropTargets. By default, this is allowed for drags from any other page in the document, apart from the current one, and for drags from an external application.

To allow drops from all targets, including the document page where the drag was initiated from, use the following code:

1
dragAndDropConfigurationBuilder.allowedDropTargets = .all
1
dragAndDropConfigurationBuilder.allowedDropTargets = PSPDFDropTargetAll;