iphone - 在 iOS 上使用 Quartz 获取当前页面的 PDF 超链接

标签 iphone objective-c ipad pdf core-graphics

我目前正在使用 Get PDF hyperlinks on iOS with Quartz 中的一些修改后的代码在 pdf 中搜索注释,然后在注释上放置按钮以进行导航。它目前正在传递整个 pdf,因为我希望它只是传递当前页面并在该页面上放置注释按钮。我已经浏览了关于这个主题的文档,该文档非常无望,并且已经研究了一天左右,但没有运气。

我确信我错过了一些愚蠢的东西,因此我们将不胜感激任何帮助。

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:(CGRect)frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"testpdf" ofType:@"pdf"];
        NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
        document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        currentPage = 1;
    }
    return self;
}

-(void)drawRect:(CGRect)inRect{
    if(document) {
        CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
        CGContextDrawPDFPage(ctx, page);    
        CGContextRestoreGState(ctx);
        int pageCount = CGPDFDocumentGetNumberOfPages (document);

        for(i=0; i<=currentPage; i++) {
            CGPDFPageRef page = CGPDFDocumentGetPage(document, i+1);

            CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page);

            CGPDFArrayRef outputArray;
            if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
                break;
            }

            int arrayCount = CGPDFArrayGetCount( outputArray );
            if(!arrayCount) {
                continue;
            }
            for( int j = 0; j < arrayCount; ++j ) {
                CGPDFObjectRef aDictObj;
                if(!CGPDFArrayGetObject(outputArray, j, &aDictObj)) {
                    break;
                }
                NSLog(@"poop2");
                CGPDFDictionaryRef annotDict;
                if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &annotDict)) {
                    break;
                }
                CGPDFDictionaryRef aDict;
                if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) {
                    break;
                }                
                CGPDFStringRef uriStringRef;
                if(!CGPDFDictionaryGetString(aDict, "URI", &uriStringRef)) {
                    break; 
                }                
                CGPDFArrayRef rectArray;
                if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) {
                    break;
                }
                int arrayCount = CGPDFArrayGetCount( rectArray );
                CGPDFReal coords[4];
                for( int k = 0; k < arrayCount; ++k ) {
                    CGPDFObjectRef rectObj;
                    if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) {
                        break;
                    }
                    CGPDFReal coord;
                    if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) {
                        break;
                    }
                    coords[k] = coord;
                }           
                char *uriString = (char *)CGPDFStringGetBytePtr(uriStringRef);
                NSString *uri = [NSString stringWithCString:uriString encoding:NSUTF8StringEncoding];
                if (950 <= coords[3] && coords[3] < 1000){                    
                    coords[3]= (coords[3]*1.01);
                    coords[1]= (coords[1]*1.01)-10;
                }
                else if (900 <= coords[3] && coords[3] < 949){                    
                    coords[3]= (coords[3]*1.02);
                    coords[1]= (coords[1]*1.02)-10;
                }
                else if (850 <= coords[3] && coords[3] < 899){                    
                    coords[3]= (coords[3]*1.03);
                    coords[1]= (coords[1]*1.03)-10;
                }                
                else if (800 <= coords[3] && coords[3] < 849){                    
                    coords[3]= (coords[3]*1.035);
                    coords[1]= (coords[1]*1.035)-10;
                }                
                else if (750 <= coords[3] && coords[3] < 799){                    
                    coords[3]= (coords[3]*1.04);
                    coords[1]= (coords[1]*1.04)-10;
                }
                else if (700 <= coords[3] && coords[3] < 749){                    
                    coords[3]= (coords[3]*1.05);
                    coords[1]= (coords[1]*1.05)-10;
                }
                else if (650 <= coords[3] && coords[3] < 699){                    
                    coords[3]= (coords[3]*1.06);
                    coords[1]= (coords[1]*1.06)-10;
                }
                else if (600 <= coords[3] && coords[3] < 649){                    
                    coords[3]= (coords[3]*1.075);
                    coords[1]= (coords[1]*1.075)-10;
                }                
                else if (550 <= coords[3] && coords[3] < 599){                    
                    coords[3]= (coords[3]*1.09);
                    coords[1]= (coords[1]*1.09)-10;
                }
                else if (500 <= coords[3] && coords[3] < 549){                    
                    coords[3]= (coords[3]*1.1);
                    coords[1]= (coords[1]*1.1)-10;
                }
                else if (450 <= coords[3] && coords[3] < 499){                    
                    coords[3]= (coords[3]*1.13);
                    coords[1]= (coords[1]*1.13)-10;
                }
                else if (400 <= coords[3] && coords[3] < 449){                    
                    coords[3]= (coords[3]*1.15);
                    coords[1]= (coords[1]*1.15)-10;
                }
                else if (300 <= coords[3] && coords[3] < 399){                    
                    coords[3]= (coords[3]*1.18);
                    coords[1]= (coords[1]*1.18)-10;
                }
                else if (250 <= coords[3] && coords[3] < 299){
                    coords[3]= (coords[3]*1.30);
                    coords[1]= (coords[1]*1.30)-10;
                }
                else if (200 <= coords[3] && coords[3] < 249){
                    coords[3]= (coords[3]*1.4);
                    coords[1]= (coords[1]*1.4)-10;
                }
                else if (150 <= coords[3] && coords[3] < 199){
                    coords[3]= (coords[3]*1.6);
                    coords[1]= (coords[1]*1.6)-10;
                }
                else if (100 <= coords[3] && coords[3] < 149){
                    coords[3]= (coords[3]*1.8);
                    coords[1]= (coords[1]*1.8)-10;
                }
                else if (50 <= coords[3] && coords[3] < 99){
                    coords[3]= (coords[3]*2);
                    coords[1]= (coords[1]*2)-10;
                }
                else if (0 <= coords[3] && coords[3] < 49){
                    coords[3]= (coords[3]*2.5);
                    coords[1]= (coords[1]*2.5-10);
                }
                CGRect rect = CGRectMake(coords[0] /1.11, coords[1],coords[2],coords[3]);
                CGPDFInteger pageRotate = 0;
                CGPDFDictionaryGetInteger( pageDictionary, "Rotate", &pageRotate ); 
                CGRect pageRect = CGRectIntegral( CGPDFPageGetBoxRect( page, kCGPDFMediaBox ));
                if( pageRotate == 90 || pageRotate == 270 ) {
                    CGFloat temp = pageRect.size.width;
                    pageRect.size.width = pageRect.size.height;
                    pageRect.size.height = temp;
                }
                rect.size.width -= rect.origin.x;
                rect.size.height -= rect.origin.y;
                CGAffineTransform trans = CGAffineTransformIdentity;
                trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height);
                trans = CGAffineTransformScale(trans, 1.0, -1.0);
                rect = CGRectApplyAffineTransform(rect, trans);
                // do whatever you need with the coordinates.
                // e.g. you could create a button and put it on top of your page
                // and use it to open the URL with UIApplication's openURL
                url = [NSURL URLWithString:uri];
                CGPDFContextSetURLForRect(ctx, (CFURLRef)url, rect);
                buttonArray = [[NSMutableArray alloc] init];
                for(NSInteger b = 0; b < CGPDFArrayGetCount(outputArray); b++){
                    button = [[UIButton alloc] initWithFrame:rect];
                    [button setTitle:@"" forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                    [button setBackgroundColor:[UIColor blueColor]];
                    [button.titleLabel setBackgroundColor:[UIColor clearColor]];
                    button.tag = currentPage;
                    [button addTarget:self action:@selector(openLink:) forControlEvents:UIControlEventTouchUpInside];
                    [self addSubview:button];
                    [buttonArray addObject:button];
                }
                NSLog(@"%i", CGPDFArrayGetCount(buttonArray));                
            }
        }   
    }   
}

最佳答案

所以这一行:

for(i=0; i<=currentPage; i++) {

循环(迭代)从第一页到当前页的所有页面。您应该删除该循环(带有关联的 }),然后更改此行:

CGPDFPageRef page = CGPDFDocumentGetPage(document, i+1);

到此

CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);

您在 initWithFrame 中将 currentPage 设置为 1。这将为您提供当前页面。

关于iphone - 在 iOS 上使用 Quartz 获取当前页面的 PDF 超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6367878/

相关文章:

ios - 在单元格中异步加载图像的 UITableView - 图像在慢速连接时不断变化

ios - iPad 在没有互联网的情况下强制连接到 WiFi

iphone - 使用构造方法创建一个http body

iphone - iOS 5 中 UILabel 的多色

objective-c - 在 OS X 中将图像元数据 (EXIF/TIFF/IPTC) 写入图像文件

iphone - 将变量从一个 View 转移到另一个 View

iphone - OCUnit - 中断测试用例失败?

iphone - 在 Objective-C 中从模型生成表单(a django ModelForms)

ios - 无效图像 - 对于 iOS 应用程序,二进制提交中包含的图标必须为 PNG 格式

iphone - iPhone崩溃报告:SIGABRT