Frequently Asked Question

How to set custom annotation icon on iOS
Last Updated 11 months ago

This article shows how to add a custom annotation icon:  

- (BOOL)setCustomIcon:(CGImageRef)ref forAnnotAtIndex:(int)index atPage:(int)pageNum
{
    //get the PDFVPage
    PDFVPage *vpage = [m_view vGetPage:pageNum];
    if( !vpage ) return NO;   
    //get the PDFPage
    PDFPage *page = [vpage GetPage];
    if (!page) {
        return NO;
    }    
    //get the annotation
    PDFAnnot *annot = [page annotAtIndex:index];    

    //init PDFDocForm and PDFPageContent

    PDFDocForm *form = [m_doc newForm];
    PDFPageContent *content = [[PDFPageContent alloc] init];

    //create PDFDocImage with CGImageRef
    PDFDocImage *image = [m_doc newImage:ref :YES];
    PDF_PAGE_IMAGE rimg = [form addResImage:image];

    //set the matrix 20x20
    PDFMatrix *matrix = [[PDFMatrix alloc] init:20 :20 :0 :0];
    [content gsCatMatrix:matrix];
    matrix = nil;   

    //draw the image on the PDFPageContent
    [content drawImage:rimg];
    [content gsRestore];    

    //set the content on the PDFDocForm
    [form setContent:0 :0 :20 :20 :content];

    //set the custom icon
    BOOL success = [annot setIcon2:@"myIcon" :form];    

    //free objects
    content = nil;
    page = nil;    

    return success;
}

For example, you can call this method while you're adding a PopupText annot.

In this case in vAddTextAnnot you can add:


-(void)vAddTextAnnot :(int)x :(int)y :(NSString *)text
{
    struct PDFV_POS pos;
    [m_view vGetPos:&pos :x * m_scale :y * m_scale];

    if(pos.pageno>=0)
    {
        PDFVPage *vpage = [m_view vGetPage:pos.pageno];
        if( !vpage ) return;
        PDFPage *page = [vpage GetPage];
        if (!page) {
            return;
        }
        m_modified = true;        

        PDF_POINT pt;
        pt.x = pos.x ;
        pt.y = pos.y ;
        [page addAnnotNote:&pt];

        PDFAnnot *annot = [page annotAtIndex: [page annotCount] - 1];
        [annot setPopupText:text];        

        //set the custom icon
        UIImage *im = [UIImage imageNamed:@"image.png"];
        [self setCustomIcon:im.CGImage forAnnotAtIndex:[page annotCount] - 1 atPage:m_cur_page];

        [m_view vRenderSync:pos.pageno];
        [m_doc save];
    }
}

You can also modify the method and get the annotation and the page number as parameters instead of the annotation index.

To use this method you have to set the cache path.

Here is an example of how to set the cache:

NSString *cacheFile = [[NSTemporaryDirectory() stringByAppendingString:@""] stringByAppendingString:@"cache.dat"];
[m_doc setCache:cacheFile];

Loading ...