How can we create A PDF with Annotation
Asked Answered
E

1

6

Can anyone tell me how I can create a PDF with a text annotation on it (so that the annotation can be visible while opening the PDF with the PDF reader in the Desktop) ?

Currently I am able to create a PDF, but I am not able to set the Page level dictionary for the key "Annots". This is the Sample code that I did to create the meta information about the page. Can Anyone tell me where I went 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

Engineman answered 25/4, 2011 at 11:33 Comment(0)
C
0
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);
Commingle answered 17/3, 2012 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.