添加链接到pdf无法在ios中工作?

时间:2014-04-08 12:31:30

标签: ios pdf pdf-generation

我想要在pdf文件中生成链接的功能。当用户点击该链接时,它应该导航到该文件。因为我使用了以下代码。它已生成pdf但我无法生成链接。我怎么能这样做?

 -(void)generatePDF
    {
        NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
        NSString* documentDirectory = [documentDirectories objectAtIndex:0];
        NSString *pdfFileName = [documentDirectory stringByAppendingPathComponent:@"mypdf.pdf"];
        UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
         UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
         NSString *myString = @"My PDF Heading";
        [myString drawInRect:CGRectMake(20, 100, 200, 34) withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

        UIGraphicsAddPDFContextDestinationAtPoint(@"Chapter1", CGPointMake(72, 72));
        UIGraphicsSetPDFContextDestinationForRect(@"Chapter1", CGRectMake(72, 528, 400, 40));



        UIGraphicsEndPDFContext();

         [self showPDFFile];
    }

1 个答案:

答案 0 :(得分:0)

使用以下代码我能够以pdf格式生成链接并导航到pdf中的其他页面

-(void)generatePDF
{

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    /// Set line break mode
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    /// Set text alignment
    paragraphStyle.alignment = NSTextAlignmentLeft;


    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13],NSParagraphStyleAttributeName:paragraphStyle};


    NSDictionary *attributes2 = @{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13],NSParagraphStyleAttributeName:paragraphStyle,NSBackgroundColorAttributeName:[UIColor yellowColor]};

    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString *pdfFileName = [documentDirectory stringByAppendingPathComponent:@"mypdf.pdf"];
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    NSString *myString = @"Page1";


    [myString drawInRect:CGRectMake(20, 100, 200, 34) withAttributes:attributes];

    UIGraphicsAddPDFContextDestinationAtPoint(@"NavigateToPage1", CGPointMake(0, 0));

    [self  drawTextLink2:@"NavigateToPage3" inFrame:CGRectMake(20, 400, 200, 34)];

    [@"Navigate to Page3" drawInRect:CGRectMake(20, 400, 200, 34) withAttributes:attributes];





    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
     [@"Page 2" drawInRect:CGRectMake(20, 100, 200, 34)withAttributes:attributes];


    [self  drawTextLink2:@"NavigateToPage1" inFrame:CGRectMake(20, 400, 200, 34)];

    UIGraphicsAddPDFContextDestinationAtPoint(@"NavigateToPage2", CGPointMake(0, 0));





    [@"Navigate to Page1" drawInRect:CGRectMake(20, 400, 200, 34) withAttributes:attributes];

     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    [@"Page3" drawInRect:CGRectMake(20, 100, 200, 34) withAttributes:attributes];


    [@"Navigate to Page2" drawInRect:CGRectMake(20, 400, 200, 34) withAttributes:attributes];
    UIGraphicsAddPDFContextDestinationAtPoint(@"NavigateToPage3", CGPointMake(0, 0));
    [self  drawTextLink2:@"NavigateToPage2" inFrame:CGRectMake(20, 400, 200, 34)];

      UIGraphicsEndPDFContext();
     [self showPDFFile];
}

- (void) drawTextLink:(NSString *) text inFrame:(CGRect) frameRect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform ctm = CGContextGetCTM(context);

    // Translate the origin to the bottom left.
    // Notice that 842 is the size of the PDF page.
    CGAffineTransformTranslate(ctm, 0.0, 842);

    // Flip the handedness of the coordinate system back to right handed.
    CGAffineTransformScale(ctm, 1.0, -1.0);

    // Convert the update rectangle to the new coordiante system.
    CGRect xformRect = CGRectApplyAffineTransform(frameRect, ctm);

    NSLog(@"xformrect is %@",NSStringFromCGRect(xformRect));

    NSURL *url = [NSURL URLWithString:text];
    UIGraphicsSetPDFContextURLForRect( url, xformRect );

    CGContextSaveGState(context);
    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    attributesDict = @{NSUnderlineStyleAttributeName : underline, NSForegroundColorAttributeName : [UIColor blueColor]};
    attString = [[NSMutableAttributedString alloc] initWithString:url.absoluteString attributes:attributesDict];

    [attString drawInRect:frameRect];

    CGContextRestoreGState(context);
}



- (void) drawTextLink2:(NSString *) text inFrame:(CGRect) frameRect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform ctm = CGContextGetCTM(context);

    // Translate the origin to the bottom left.
    // Notice that 842 is the size of the PDF page.
    CGAffineTransformTranslate(ctm, 0.0, 842);

    // Flip the handedness of the coordinate system back to right handed.
    CGAffineTransformScale(ctm, 1.0, -1.0);

    // Convert the update rectangle to the new coordiante system.
    CGRect xformRect = CGRectApplyAffineTransform(frameRect, ctm);

   // NSURL *url = [NSURL URLWithString:text];

    UIGraphicsSetPDFContextDestinationForRect(text, xformRect);
    NSLog(@"xfrom rect is %@",NSStringFromCGRect(xformRect));
   // UIGraphicsAddPDFContextDestinationAtPoint(text, CGPointMake(0,0));
   // UIGraphicsSetPDFContextURLForRect( url, xformRect );

    CGContextSaveGState(context);
    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    attributesDict = @{NSUnderlineStyleAttributeName : underline, NSForegroundColorAttributeName : [UIColor blueColor]};
    attString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDict];

    [attString drawInRect:frameRect];

    CGContextRestoreGState(context);


}




-(void)showPDFFile
{
    NSString* fileName = @"mypdf.pdf";

    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:0];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];

    NSURL *url = [NSURL fileURLWithPath:pdfFileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    webView.delegate=self;
    [webView loadRequest:request];

    [self.view addSubview:webView];
}