How can we create a PDF with annotation

Can someone tell me how I can create a PDF with text annotation on it (so that the annotation can be seen when opening a PDF file using a PDF reader on the desktop)?

Currently, I can create a PDF file, but I cannot install a page-level dictionary for the Annots key. This is an example of the code I made to create the meta information about the page. Can someone tell me where I did wrong and any other approach that I should follow.

CFMutableDictionaryRef metaDataDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(metaDataDictionary, CFSTR("Subtype"), CFSTR("Text")); CFDictionarySetValue(metaDataDictionary, CFSTR("Contents"), CFSTR("This is a sample")); CFDictionarySetValue(metaDataDictionary, CFSTR("Subj"), CFSTR("Subject")); CFDictionarySetValue(metaDataDictionary, CFSTR("M"), CFSTR("Date")); CFDictionarySetValue(metaDataDictionary, CFSTR("NM"), CFSTR("Name of Annotation")); CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); CFArrayInsertValueAtIndex(array, 0, metaDataDictionary); CFDictionarySetValue(pageDictionary,CFSTR("Annots"), array); 

Thanks in advance

+6
source share
1 answer
 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = @"test.pdf"; NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]]; // Create PDF context CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, NULL, NULL); CGPDFContextBeginPage(pdfContext, NULL); UIGraphicsPushContext(pdfContext); // Flip coordinate system CGRect bounds = CGContextGetClipBoundingBox(pdfContext); CGContextScaleCTM(pdfContext, 1.0, -1.0); CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height); // Drawing commands [@"HEADER" drawAtPoint:CGPointMake(130, 50) withFont:[UIFont boldSystemFontOfSize:15.0f]]; [@"First line" drawAtPoint:CGPointMake(145, 80) withFont:[UIFont boldSystemFontOfSize:15.0f]]; [img drawAtPoint:CGPointMake(10,100)]; [@"Bye Bye" drawAtPoint:CGPointMake(10,500 ) withFont:[UIFont boldSystemFontOfSize:20.0f]]; // Clean up UIGraphicsPopContext(); CGPDFContextEndPage(pdfContext); CGPDFContextClose(pdfContext); 
0
source

Source: https://habr.com/ru/post/886618/


All Articles