5

我正在尝试在具有注释(例如小部件和链接)的代码中生成 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 不需要。请帮助任何人!

4

1 回答 1

2

看看 PoDoFo 库:http ://podofo.sourceforge.net

PoDoFo 在 GPL 和 LGPL 下获得部分许可。

有了它,您可以修改 pdf 文件并为其添加注释。它可以移植到 iOS。查看文档,如果它可以处理您想要的内容并了解它是如何工作的。

如果您有兴趣,还可以查看GitHub 上的这个示例项目。在那里,我将库集成到了一个 iOS 项目中,您可以使用 iPad 添加 FreeText 和 Square Annotations。

于 2013-02-24T21:18:23.680 回答