我试图在具有注释的代码中生成PDF,例如小部件和链接.我可以生成PDF并修改文档信息和页面的MediaBox,但我无法弄清楚如何添加到Annotations.我尝试将其添加到页面字典中,但由于某种原因,PDF不接受它.
有谁知道如何为新创建的PDF添加注释?下面是我要生成PDF的代码和我试图添加到它的测试Annotation字典:
-(void) createPDFFile: (CGPDFPageRef) pageRef{
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
const char *filepath = [[documentsDirectory stringByAppendingString:@"/fileTest.pdf"] UTF8String];
path = CFStringCreateWithCString (NULL, filepath, kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
CGRect pageRect = self.bounds;
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); // 5
CFRelease(myDictionary);
CFRelease(url);
CGRect mediaBox;
mediaBox = CGRectMake (0, 0, pageRect.size.width, 100);
CFStringRef myKeys[1];
CFTypeRef myValues[1];
myKeys[0] = CFSTR("MediaBox");
myValues[0] = (CFTypeRef) CFDataCreate(NULL,(const UInt8 *)&mediaBox, sizeof (CGRect));
CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **) myKeys,
(const void **) myValues, 1,
&kCFTypeDictionaryKeyCallBacks,
& kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef pageDictionaryMutable = CFDictionaryCreateMutableCopy(NULL, 0, pageDictionary);
CFMutableDictionaryRef metaDataDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(metaDataDictionary, CFSTR("DA"), CFSTR("/Helvetica 10 Tf 0 g"));
CFDictionarySetValue(metaDataDictionary, CFSTR("F"), CFSTR("4"));
CFDictionarySetValue(metaDataDictionary, CFSTR("FT"), CFSTR("Tx"));
CFDictionarySetValue(metaDataDictionary, CFSTR("Subtype"), CFSTR("Widget"));
CFDictionarySetValue(metaDataDictionary, CFSTR("T"), CFSTR("undefined"));
CFDictionarySetValue(metaDataDictionary, CFSTR("Type"), CFSTR("Annot"));
CFDictionarySetValue(metaDataDictionary, CFSTR("V"), CFSTR(""));
CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks);
CFArrayInsertValueAtIndex(array, 0, metaDataDictionary);
CFDictionarySetValue(pageDictionaryMutable, CFSTR("Annots"), array);
CGPDFContextBeginPage (pdfContext, pageDictionaryMutable);
CGPDFContextEndPage (pdfContext);
CGContextRelease (pdfContext);
CFRelease(pageDictionary);}
打印出CFMutableDictionary pageDictionaryMutable表示应该将Annots字典添加到PDF以及MediaBox:
(gdb) po pageDictionaryMutable
{
Annots = (
{
DA = "/Helvetica 10 Tf 0 g";
F = 4;
FT = Tx;
Subtype = Widget;
T = undefined;
Type = Annot;
V = "";
}
);
MediaBox = <00000000 00000000 00008044 0000c842>;
}
Media Box需要,但Annots没有.请帮助任何人!
最佳答案 看看PoDoFo Library:
http://podofo.sourceforge.net
PoDoFo根据GPL和LGPL获得部分许可.
有了它,您可以修改pdf文件并为其添加注释.它可以移植到iOS.查看文档,它是否可以处理您想要的内容并查看其工作原理.
如果您有兴趣,也可以查看this sample project on GitHub.我将库集成到iOS项目中,您可以使用iPad添加FreeText和Square Annotations.