我正在尝试在具有注释(例如小部件和链接)的代码中生成 PDF。我可以生成 PDF 并修改页面的文档信息和 MediaBox,但我不知道如何添加到注释中。我尝试将其添加到页面字典中,但由于某种原因 PDF 不接受它。
有谁知道如何在新创建的 PDF 中添加注释?下面是我必须生成 PDF 的代码和我试图添加到其中的测试注释字典:
-(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 不需要。请帮助任何人!