简体   繁体   中英

PDFAnnotationText is not displaying pop-up on 10.12

PDFAnnotationText is not showing pop-up on macOS sierra 10.12.1 Beta (16B2548a). PDFAnnotationText is deprecated on 10.12 but new APIs are not drawing annotations.

Old APIs:

// display the PDF document
    [m_pdfView setDocument: [self pdfDocument]];

- (PDFDocument *)pdfDocument {
    // create a page

    PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/test/Downloads/Eticket.pdf"]];
    PDFAnnotationText* result = [[PDFAnnotationText alloc] initWithBounds:NSMakeRect(100, 100, 40, 40)];
    result.color = [NSColor redColor];
    result.contents = @"Hello";
    result.iconType = kPDFTextAnnotationIconNote;
    // add it to the PDF document
    [[document pageAtIndex:0] addAnnotation:result];
    return document;
}

10.12 New APIs:

- (PDFDocument *)pdfDocument {
    // create a page
    PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/test/Downloads/Eticket.pdf"]];

    NSMutableDictionary *popupDictionary = [[NSMutableDictionary alloc] init];

    [popupDictionary setObject:@"/Popup" forKey:kPDFAnnotationKey_Subtype];
    [popupDictionary setObject:[NSColor redColor] forKey:kPDFAnnotationKey_Color];
    [popupDictionary setObject:@"Hello" forKey:kPDFAnnotationKey_Contents];

    NSValue *rectValue = [NSValue valueWithRect:NSMakeRect(100, 100, 40, 40)];
    [popupDictionary setObject:rectValue forKey:kPDFAnnotationKey_Rect];

    PDFAnnotation *textAnnotation = [[PDFAnnotation alloc] initWithDictionary: popupDictionary forPage: [document pageAtIndex:0]];
    [[document pageAtIndex:0] addAnnotation:textAnnotation];    // add it to the PDF document
    return document;
}

I am using Xcode Version 8.0 (8A218a). Can anyone please help me out ?

I was able to create a simple black string annotation like this (in Swift 3):

let document = pdfView!.document
let page = document!.page(at:0)
let pageBounds = page!.bounds(for: PDFDisplayBox.artBox)
let annotation = PDFAnnotation()
annotation.setValue("/FreeText", forAnnotationKey: kPDFAnnotationKey_Subtype)
annotation.setValue("HELLO WORLD", forAnnotationKey: kPDFAnnotationKey_Contents)
annotation.setValue(NSColor.clear, forAnnotationKey: kPDFAnnotationKey_Color)
annotation.bounds = NSRect(x:10, y:pageBounds.height-50, width:400, height:40)
page!.addAnnotation(annotation)

It's using /FreeText, but maybe it will help you if you experiment with the values. Still could not figure out how to change the color of the text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM