2011-04-25 52 views
6

任何人都可以告诉我如何创建带有文本注释的PDF(以便在使用桌面PDF阅读器打开PDF时可以看到注释)?我们如何创建带注解的PDF

目前我能够创建一个PDF,但我无法为关键“Annots”设置页面级字典。这是我用来创建关于该页面的元信息的示例代码。任何人都可以告诉我我错了什么,以及我应该遵循的其他方法。

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); 

在此先感谢

回答

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);