Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom/Pinch the pdf which is loaded in a UIWebView

I have loaded a PDF file in a UIWebView from the resource bundle.

Now, I am not able to zoom/pinch the PDF file.

I found the below 2 relevant links, but none of the answers are marked as correct -

  1. How to zoom webView with pdf file

  2. Enable zooming/pinch on UIWebView

How to make the PDF zoomable/pinchable which has been loaded in a UIWebView from the resource bundle, Will the below solution work ?

  1. Problem with pdf while opening in UIWebView

Thanks for your help.

like image 392
user2486363 Avatar asked Jul 03 '13 10:07

user2486363


3 Answers

In your WebView on Interface Builder add check to Scale Page To Fit and you enable a Pinch ZoomIn ZoomOut ;)

Or if you want to lad better your PDF try to Look this code:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [webView loadRequest:[NSURLRequest requestWithURL:@"http:pdfURL"]];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPDFFile" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    //--------------AND HERE USE SCALE PAGE TO FIT------------------//
    [webView setScalesPageToFit:YES];

}

UIWebView Hope this help you.

like image 140
BlackSheep Avatar answered Sep 21 '22 01:09

BlackSheep


//Try like this

NSString *urlstr=@"www.example.com/yy.pdf";
web=nil;
web=[[UIWebView alloc] initWithFrame:CGRectMake(0, 98, 320, 367)];
web.delegate=self;
[self.view addSubview:web];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstr]]];
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
                                 initWithTarget:self action:@selector(handlePinch:)];
 pgr.delegate = self;
 [web addGestureRecognizer:pgr];

// Target Action

 - (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
 {
  recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
  }

Before this add UIGestureRecognizerDelegate in .h. Hope it will helps you..

like image 33
Alex Avatar answered Sep 19 '22 01:09

Alex


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:documentsDirectory]) 
{
    NSURL *url = [NSURL fileURLWithPath:strFileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webview loadRequest:request];
}
like image 25
Neha Avatar answered Sep 20 '22 01:09

Neha