Knowledge Base - Selected text area to image (iOS)

Here you could find a sample code to get a UIImage from text selection.
This code first of all get the rect from the selection, then render the page portion.
Add startIdx and endIdx methods, to RDVSel class to get the first and the last selected chars:


(.h)
- (int)startIdx;
- (int)endIdx;

(.m)
- (int)startIdx {
    return m_index1;
}
- (int)endIdx {
    return m_index2;
}



Then, you could add this code to OnSelTouchEnd method to check it (then you could create a custom method or move this code):


-(bool)OnSelTouchEnd:(CGPoint)point

{
    if( m_status != sta_sel ) return false;
    
    // TEST CODE BEGIN
    // get first and last char index
    int start = [m_sel startIdx];
    int end = [m_sel endIdx];
    int cur = start;
    
    // init draw_rect with the first char rect
    PDF_RECT draw_rect;
    [[m_sel pdfpage] objsCharRect:start :&draw_rect];
    
    // set the min left/top and max right/bottom
    PDF_RECT rect;
    while (cur <= end) {
        [[m_sel pdfpage] objsCharRect:cur :&rect];
        
        draw_rect.left = (rect.left < draw_rect.left) ? rect.left : draw_rect.left;
        draw_rect.top = (rect.top < draw_rect.top) ? rect.top : draw_rect.top;
        draw_rect.right = (rect.right > draw_rect.right) ? rect.right : draw_rect.right;
        draw_rect.bottom = (rect.bottom > draw_rect.bottom) ? rect.bottom : draw_rect.bottom;
        cur++;
    }
    
    // set dib size with the draw_rect
    int width = draw_rect.right - draw_rect.left;
    int height = draw_rect.bottom - draw_rect.top;
    
    // get the selected page
    PDFPage *page = [m_sel pdfpage];
    [page objsStart];
    PDFDIB *dib = [[PDFDIB alloc] init :width :height];
    
    // set the matrix with the draw_rect (we need only a portion of the page)
    PDFMatrix *mat = [[PDFMatrix alloc] init :1 :-1 :-draw_rect.left :draw_rect.bottom];
    [page renderPrepare :dib];
    [page render :dib :mat :2];//always render best.
    mat = NULL;
    
    // get the UIImage
    UIImage *img = [UIImage imageWithCGImage:[dib image]];
    
    // add code here to save the UIImage or the [dib image], that is CGImageRef
    //END TEST CODE
    
    if( m_del )
        [m_del OnSelEnd :m_tx/m_scale_pix :m_ty/m_scale_pix :point.x :point.y];
    return true;
}

Applies To

RadaeePDF SDK for iOS

Details

Created : 2019-08-07 09:41:44, Last Modified : 2019-08-07 09:41:44