微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过iPhone应用程序读取PDF文件作为字符串

我在iPhone阅读PDF中面临一些问题。
我已经尝试了以下代码。我知道我使用错误方法来解析 – 解析方法只是用于搜索目的。但是我想将整个pdf文本转换成字符串。说例如苹果的MobileHIG.pdf – 我在这代码中使用过。

@implementation NetPDFViewController

size_t totalPages;  // a variable to store total pages

// a method to get the pdf ref
CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename) {
    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    path = CFStringCreateWithCString (NULL,filename,kcfStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL,path,kcfURLPOSIXPathStyle,0);
    CFRelease (path);
    document = CGPDFDocumentCreateWithURL (url);// 2
    CFRelease(url);
    int count = CGPDFDocumentGetNumberOfPages (document);// 3
    if (count == 0) {
        printf("`%s' needs at least one page!",filename);
        return NULL;
    }
    return document;
}

// table methods to parse pdf
static void op_MP (CGPDFScannerRef s,void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s,&name))
        return;
    printf("MP /%s\n",name);   
}

static void op_DP (CGPDFScannerRef s,&name))
        return;
    printf("DP /%s\n",name);   
}

static void op_BMC (CGPDFScannerRef s,&name))
        return;
    printf("BMC /%s\n",name);  
}

static void op_BDC (CGPDFScannerRef s,&name))
        return;
    printf("BDC /%s\n",name);  
}

static void op_EMC (CGPDFScannerRef s,&name))
        return;
    printf("EMC /%s\n",name);  
}

// a method to display pdf page.

void Mydisplaypdfpage (CGContextRef myContext,size_t pageNumber,const char *filename) {
    CGPDFDocumentRef document;
    CGpdfpageRef page;
    document = MyGetPDFDocumentRef (filename);// 1
    totalPages=CGPDFDocumentGetNumberOfPages(document);
    page = CGPDFDocumentGetPage (document,pageNumber);// 2

    CGPDFDictionaryRef d;

    d = CGpdfpageGetDictionary(page);

// ----- edit   problem here - CGPDFDictionary is completely unkNown 
// ----- as we don't kNow keys & values of it.
    CGPDFScannerRef myScanner; 
    CGPDFOperatorTableRef myTable;
    myTable = CGPDFOperatorTableCreate();
    CGPDFOperatorTableSetCallback (myTable,"MP",&op_MP);
    CGPDFOperatorTableSetCallback (myTable,"DP",&op_DP);
    CGPDFOperatorTableSetCallback (myTable,"BMC",&op_BMC);
    CGPDFOperatorTableSetCallback (myTable,"BDC",&op_BDC);
    CGPDFOperatorTableSetCallback (myTable,"EMC",&op_EMC);

    CGPDFContentStreamRef myContentStream = CGPDFContentStreamCreateWithPage (page);// 3
    myScanner = CGPDFScannerCreate (myContentStream,myTable,NULL);// 4

    CGPDFScannerScan (myScanner);// 5

//  CGPDFDictionaryRef d;

    CGPDFStringRef str; // represents a sequence of bytes

    d = CGpdfpageGetDictionary(page);

    if (CGPDFDictionaryGetString(d,"Thumb",&str)){
        CFStringRef s;
        s = CGPDFStringcopyTextString(str);
        if (s != NULL) {
            //need something in here in case it cant find anything
            NSLog(@"%@ testing it",s);
        }
        CFRelease(s);       
//      CFDataRef data = CGPDFStreamcopyData (stream,CGPDFDataFormatRaw);
    }

// -----------------------------------  

    CGContextDrawpdfpage (myContext,page);// 3
    CGContextTranslateCTM(myContext,20);
    CGContextScaleCTM(myContext,1.0,-1.0);
    CGPDFDocumentRelease (document);// 4
}

- (void)viewDidLoad {
    [super viewDidLoad];


// -------------------------------------------------------- 
// code for simple direct image from pdf docs.
    UIGraphicsBeginImageContext(CGSizeMake(320,460));
    initialPage=28;
    Mydisplaypdfpage(UIGraphicsGetCurrentContext(),initialPage,[[[NSBundle mainBundle] pathForResource:@"MobileHIG" ofType:@"pdf"] UTF8String]);
    imgV.image=UIGraphicsGetimageFromCurrentimageContext();
    imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored];  
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint LasttouchPoint =  [touch locationInView:self.view];
    int LasttouchX = LasttouchPoint.x;
    startpoint=LasttouchX;
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint LasttouchPoint =  [touch locationInView:self.view];
    int LasttouchX = LasttouchPoint.x;
    endpoint=LasttouchX;
    if(startpoint>(endpoint+75)){
        initialPage++;
        [self loadPage:initialPage nextOne:YES];
    } else if((startpoint+75)<endpoint){
        initialPage--;
        [self loadPage:initialPage nextOne:NO];
    }
}


-(void)loadPage:(NSUInteger)page nextOne:(BOOL)yesOrNo{
    if(page<=totalPages && page>0){
        UIGraphicsBeginImageContext(CGSizeMake(720,720));  
        Mydisplaypdfpage(UIGraphicsGetCurrentContext(),page,[[[NSBundle mainBundle] pathForResource:@"MobileHIG" ofType:@"pdf"] UTF8String]);

        CATransition *transition = [CATransition animation];
        transition.duration = 0.75;
        transition.timingFunction = [camediatimingFunction functionWithName:kcamediatimingFunctionEaseInEaSEOut];
        transition.type=kCATransitionPush;
        if(yesOrNo){
            transition.subtype=kCATransitionFromright;
        } else {
            transition.subtype=kCATransitionFromLeft;
        }

        transition.delegate = self;
        [imgV.layer addAnimation:transition forKey:nil];
        imgV.image=UIGraphicsGetimageFromCurrentimageContext();
        imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored];
    }
}

但是,从pdf文档中我没有看到一条直线的成功。
还缺少什么

解决方法

我有一个图书馆,可以做这个确切的事情链接在这里Extracting pdf text in Objective C

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐