سؤال

I want functionality in which link should be generated in pdf file.When user clicks on that link it should be navigated to that file.For that i have used following code.It has generated the pdf but i am not able to genreate the link.How can i do that?

 -(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];
    }
هل كانت مفيدة؟

المحلول

Using following code i was able to generate links in pdf and navigate to other page in 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];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top