Iphone CGPDFDictionaryRef到一些有用的东西
一点背景: 我正在将图像附加到现有的pdf文件中。我发现没有一种方法可以只添加一个页面,你必须重新创建整个pdf,然后删除旧的,然后移动新的。(如果这是错误的,请告诉我,这会让我省去很多麻烦。)我已经完成了那部分,但我现在正在尝试复制辅助信息(标题、作者、密钥等)。问题是类型问题太多了,所以我现在使用的方法是获取CGPDFDocumentRef的CGPDFDictionaryRef,然后调用cgpdfdictionaryaapplierfunction。在我传递它的C函数中,我将每个键提取到一个NSMutableDictionary,这样我就可以对这些值进行处理,而不必将它们锁定在这种糟糕的CGPDF格式中 所以基本上我的问题是:Iphone CGPDFDictionaryRef到一些有用的东西,iphone,pdf,dictionary,Iphone,Pdf,Dictionary,一点背景: 我正在将图像附加到现有的pdf文件中。我发现没有一种方法可以只添加一个页面,你必须重新创建整个pdf,然后删除旧的,然后移动新的。(如果这是错误的,请告诉我,这会让我省去很多麻烦。)我已经完成了那部分,但我现在正在尝试复制辅助信息(标题、作者、密钥等)。问题是类型问题太多了,所以我现在使用的方法是获取CGPDFDocumentRef的CGPDFDictionaryRef,然后调用cgpdfdictionaryaapplierfunction。在我传递它的C函数中,我将每个键提取到一个
有更好的方法吗?我已经看了这么多文档文件,我无法想象会遗漏任何东西,但我真的希望我遗漏了,因为我要做的工作越来越荒谬。你可以通过绘制来完成你所描述的内容,但是如果你有一个复杂的PDF,你可能会尝试一些超出API预期的东西。您可以查看的第3.4节,了解您将要了解的内容。我已经完成了解决方法。我仍然想知道是否有更好的方法来做到这一点,所以如果你知道一个或有任何建议,我愿意尝试 注意:我只检查bool、int、string和array,因为根据苹果的文档,这些都应该包含在PDF的辅助信息中。数组位未经测试,因为我的辅助信息中没有包含数组的PDF。如果有人想为我测试它,或者链接到一个pdf文档,我会很乐意测试它 首先,在类标题中,在
@接口
标记之外创建一个id selfClass
,以便CGPDFDictionaryApplyFunction
调用的C函数可以访问当前类。还添加一个NSDictionary*auxInfo
用于存储信息。提取后,NS类型可以很容易地进行如下转换:
CFDictionaryRef newDictionary = (CFDictionaryRef)[self auxInfo];
我昨晚确实完成了,但我想我必须做另一轮循环才能从NS转换到CF,忘记了它们是无令牌桥接的。好了,希望大家都能从我的劳动中受益。如果你需要澄清,可以提问。再一次,如果有更简单的方法,如果只是优化我的代码,请这么说。我知道这不是一个非常优雅的方法,但它现在起作用了
- (void)extractPDFDictionary:(CGPDFDocumentRef)pdf{
NSLog(@"extractingPDFDictionary");
CGPDFDictionaryRef oldDict = CGPDFDocumentGetInfo(pdf);
CGPDFDictionaryApplyFunction(oldDict, copyDictionaryValues, NULL);
}
void copyDictionaryValues (const char *key, CGPDFObjectRef object, void *info) {
NSLog(@"key: %s", key);
CGPDFObjectType type = CGPDFObjectGetType(object);
switch (type) {
case kCGPDFObjectTypeString: {
CGPDFStringRef objectString;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &objectString)) {
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
[[selfClass auxInfo] setObject:tempStr
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
[tempStr release];
NSLog(@"set string value");
}
}
case kCGPDFObjectTypeInteger: {
CGPDFInteger objectInteger;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
[[selfClass auxInfo] setObject:[NSNumber numberWithInt:objectInteger]
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
NSLog(@"set int value");
}
}
case kCGPDFObjectTypeBoolean: {
CGPDFBoolean objectBool;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeBoolean, &objectBool)) {
[[selfClass auxInfo] setObject:[NSNumber numberWithBool:objectBool]
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
NSLog(@"set boolean value");
}
}
case kCGPDFObjectTypeArray : {
CGPDFArrayRef objectArray;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeArray, &objectArray)) {
NSArray *tempArr = [selfClass copyPDFArray:objectArray];
[[selfClass auxInfo] setObject:tempArr
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
[tempArr release];
NSLog(@"set array value");
}
}
}
}
- (NSArray *)copyPDFArray:(CGPDFArrayRef)arr{
int i = 0;
NSMutableArray *temp = [[NSMutableArray alloc] init];
for(i=0; i<CGPDFArrayGetCount(arr); i++){
CGPDFObjectRef object;
CGPDFArrayGetObject(arr, i, &object);
CGPDFObjectType type = CGPDFObjectGetType(object);
switch(type){
case kCGPDFObjectTypeString: {
CGPDFStringRef objectString;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &objectString)) {
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
[temp addObject:tempStr];
[tempStr release];
}
}
case kCGPDFObjectTypeInteger: {
CGPDFInteger objectInteger;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
[temp addObject:[NSNumber numberWithInt:objectInteger]];
}
}
case kCGPDFObjectTypeBoolean: {
CGPDFBoolean objectBool;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeBoolean, &objectBool)) {
[temp addObject:[NSNumber numberWithBool:objectBool]];
}
}
case kCGPDFObjectTypeArray : {
CGPDFArrayRef objectArray;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeArray, &objectArray)) {
NSArray *tempArr = [selfClass copyPDFArray:objectArray];
[temp addObject:tempArr];
[tempArr release];
}
}
}
}
return temp;
}
-(void)extractPDFDictionary:(CGPDFDocumentRef)pdf{
NSLog(“提取PDFDictionary”);
cgpdfdictionarefolddict=CGPDFDocumentGetInfo(pdf);
CGPDFDictionaryApplyFunction(oldDict,CopyDictionaryValue,NULL);
}
void CopyDictionaryValue(常量字符*键、CGPDFObjectRef对象、void*信息){
NSLog(@“密钥:%s”,密钥);
CGPDFObjectType=CGPDFObjectGetType(对象);
开关(类型){
案例kCGPDFObjectTypeString:{
CGPDFStringRef objectString;
if(CGPDFObjectGetValue(对象、kCGPDFObjectTypeString和objectString)){
NSString*tempStr=(NSString*)CGPDFStringCopyTextString(objectString);
[[selfClass auxInfo]setObject:tempStr
forKey:[NSString stringWithCString:键编码:NSUTF8StringEncoding]];
[临时释放];
NSLog(@“设置字符串值”);
}
}
案例kCGPDFObjectTypeInteger:{
CGPDFInteger objectInteger;
if(CGPDFObjectGetValue(对象、kCGPDFObjectTypeInteger和objectInteger)){
[[selfClass auxInfo]setObject:[NSNumber numberwhithint:objectInteger]
forKey:[NSString stringWithCString:键编码:NSUTF8StringEncoding]];
NSLog(@“设置整数值”);
}
}
案例kCGPDFObjectTypeBoolean:{
CGPDFBoolean objectBool;
if(CGPDFObjectGetValue(对象、kCGPDFObjectTypeBoolean和objectBool)){
[[selfClass auxInfo]setObject:[NSNumber numberWithBool:objectBool]
forKey:[NSString stringWithCString:键编码:NSUTF8StringEncoding]];
NSLog(@“设置布尔值”);
}
}
案例kCGPDFObjectTypeArray:{
CGPDFArrayRef对象数组;
if(CGPDFObjectGetValue(对象、kCGPDFObjectTypeArray和objectArray)){
NSArray*tempArr=[selfClass copyPDFArray:objectArray];
[[selfClass auxInfo]setObject:tempArr
forKey:[NSString stringWithCString:键编码:NSUTF8StringEncoding]];
[节拍释放];
NSLog(@“设置数组值”);
}
}
}
}
-(NSArray*)copyPDFArray:(CGPDFArrayRef)arr{
int i=0;
NSMutableArray*temp=[[NSMutableArray alloc]init];
对于(i=0;我今天也遇到了这个问题。还在研究解决方案。我会检查这个问题,如果我发现了什么,也会发布。谢谢。我会在完成后发布我的解决方法。我真的很讨厌它,因为它效率太低了。可能和SDK中没有内置方法一样好,但它很难看。是的,它很糟糕ks讨厌。我个人试图将pdf对象添加到pdfarray或pdf字典中,但遇到了同样的问题,即无法访问CGPDF对象的添加、创建、编辑或插入之类的内容。这真是一个笑话,api有多糟糕。因此,通过一个令人难以置信的精彩编码会话,我成功地提取了t上的所有变量他第一次尝试。好吧,除了数组之外,所有都签出了,因为我测试的PDF在辅助信息中没有。CGPDFDictionaryRef中的所有数据现在都在一个NSMutableDictionary中,所以它是可用的。明天我将制作