我们如何使用Annotation创建PDF

时间:2011-04-25 11:33:57

标签: objective-c pdf-generation ipad quartz-graphics ios-4.2

任何人都可以告诉我如何创建带有文本注释的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);

提前致谢

1 个答案:

答案 0 :(得分: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);