Annotation iOS

matrosov.developer's Avatar

matrosov.developer

12 Nov, 2014 10:18 AM

Does FastPdfKit support select and highlight feature and save it like annotation?

  1. 1 Posted by matrosov.develo... on 12 Nov, 2014 06:59 PM

    matrosov.developer's Avatar

    I've tried implement this code but seems it won't work:

    @interface CustomReaderViewController () <FPKOverlayViewDataSource, MFDocumentOverlayDataSource>
    
    @property (nonatomic, strong) NSArray *glyphBoxes;
    @property (nonatomic, strong) NSMutableArray *selectedArea;
    
    @end
    
    @implementation CustomReaderViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addLongpressGesture:)];
        [self.view addGestureRecognizer:longPress];
        
        [self addOverlayDataSource:self];
    
        [self addOverlayViewDataSource:self];
    }
    
    - (void)addLongpressGesture:(UILongPressGestureRecognizer*)longPress
    {
        [self startSelectedArea:[self page]];
        
        CGPoint pointPDF = [self calculateRealPDFPoint:longPress];
        
        if ([self calculateSelectedArea:pointPDF page:[self page]])
        {
            [self reloadOverlay];
            
        }
    
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(CGPoint)calculateRealPDFPoint:(UILongPressGestureRecognizer*)longPress
    {
        CGPoint pointOnPage = [longPress locationInView:[self view]];
        CGPoint pointOnPDF = [self convertPoint:pointOnPage fromOverlayToPage:[self page]];
        return pointOnPDF;
    }
    
    - (void) startSelectedArea:(NSInteger)page {
        _glyphBoxes = [self.document glyphBoxesForPage:page];
    }
    
    
    - (BOOL)calculateSelectedArea:(CGPoint)point page:(NSInteger)page
    {
        CGRect tmpRect = CGRectZero;
        for (FPKGlyphBox * glyphBox in _glyphBoxes) {
            CGRect glyphBoxRect = glyphBox.box;
            if (CGRectContainsPoint(glyphBoxRect, point)) {
                tmpRect = glyphBoxRect;
            }
        }
        if (!CGRectIsEmpty(tmpRect)) {
            CGFloat valueOriginY = tmpRect.origin.y;
            
            for (FPKGlyphBox * glyphBox in _glyphBoxes) {
                CGRect glyphBoxRect = glyphBox.box;
                CGFloat originY = glyphBoxRect.origin.y;
                if (valueOriginY == originY) {
                    tmpRect = CGRectUnion(glyphBoxRect, tmpRect);
                }
            }
            
        }
        
        if (!CGRectIsEmpty(tmpRect)) {
            BOOL setted = NO;
            for (NSUInteger cpt = 0; cpt < [_selectedArea count]; cpt++) {
                NSValue * tmpValue = [_selectedArea objectAtIndex:cpt];
                CGRect rectValue = [tmpValue CGRectValue];
                CGFloat valueOriginY = rectValue.origin.y;
                CGFloat originY = tmpRect.origin.y;
                if (valueOriginY == originY) {
                    setted = YES;
                }
            }
            if (!setted) {
                [_selectedArea addObject:[NSValue valueWithCGRect:tmpRect]];
            }
        }
        
        return YES;
    }
    
    
    -(NSArray *)documentViewController:(MFDocumentViewController *)dvc overlayViewsForPage:(NSUInteger)page
    {
    
        NSMutableArray *drawables = [[NSMutableArray alloc]init];
        
        for (NSValue * tmpValue in _selectedArea)
        {
            UIView * selectedView = [[UIView alloc] initWithFrame:[tmpValue CGRectValue]];
            
            [selectedView setBackgroundColor:[UIColor blueColor]];
            
            [drawables addObject:selectedView];
        }
        
        return drawables;
    }
    
  2. 2 Posted by matrosov.develo... on 12 Nov, 2014 07:02 PM

    matrosov.developer's Avatar
     [self.document glyphBoxesForPage:page];
    

    return nil every time.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac