Skip to content
Guides

Create, Move, and Delete Text and Images

ComPDFKit provides comprehensive methods for creating, moving, and deleting text and images.

Performing Actions Through CPDFView.

CPDFView provides basic interactive capabilities by default, allowing the user to create and delete text and images, drag and drop to move the position of images and text blocks, resize images and text blocks, etc. by using the mouse and keyboard.

Configure the Context Menu.

If you need to copy, paste, cut, or delete text or images, you can add these methods to the context menu through the menuItemsEditingAtPoint event of CPDFView.

This example shows how to configure the context menu:

swift
override func menuItemsEditing(at point: CGPoint, forPage page: CPDFPage) -> [UIMenuItem] {
      var menuItems = super.menuItemsEditing(at: point, forPage: page)

      self.menuPoint = point
      self.menuPage = page

      if editStatus == CEditingSelectStateEmpty {
          let addTextItem = UIMenuItem(title: NSLocalizedString("Add Text", comment: ""),
                                       action: #selector(addTextEditingItemAction(_:)))
          let addImageItem = UIMenuItem(title: NSLocalizedString("Add Image", comment: ""),
                                        action: #selector(addImageEditingItemAction(_:)))

          menuItems.append(addImageItem)
          menuItems.append(addTextItem)
      }

      return menuItems
  }
objective-c
- (NSArray<UIMenuItem *> *)menuItemsEditingAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
    NSArray * items = [super menuItemsEditingAtPoint:point forPage:page];
    NSMutableArray *menuItems = [NSMutableArray array];
    if (items)
        [menuItems addObjectsFromArray:items];
    
    self.menuPoint = point;
    self.menuPage = page;
    
    if(CEditingSelectStateEmpty == self.editStatus) {
        UIMenuItem *addTextItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Add Text", nil)
                                                              action:@selector(addTextEditingItemAction:)];
        UIMenuItem *addImageItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Add Image", nil)
                                                               action:@selector(addImageEditingItemAction:)];
        [menuItems addObject:addImageItem];
        [menuItems addObject:addTextItem];
        
    }
    
    return menuItems;
}

Inserting Text and Images

You can specify the ability to insert text and image blocks through the SetPDFEditCreateType method of CPDFView. The following code shows how to achieve this:

swift
// Insert Image
pdfView.changeEditingLoadType(.image)
pdfView.setShouAddEdit(.image)
// Insert Text
pdfView.changeEditingLoadType(.text)
pdfView.setShouAddEdit(.text)
// Cancel
pdfView.changeEditingLoadType([.image, .text])
pdfView.setShouAddEdit([])
objective-c
// Insert Image
[pdfView changeEditingLoadType:CEditingLoadTypeImage];
[pdfView setShouAddEditAreaType:CAddEditingAreaTypeImage];
// Insert Text
 [pdfView changeEditingLoadType:CEditingLoadTypeText];
[pdfView setShouAddEditAreaType:CAddEditingAreaTypeText];
// Cancel
[pdfView changeEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
[pdfView setShouAddEditAreaType:CAddEditingAreaTypeNone];