6

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.

Community
  • 1
  • 1
user2486363
  • 61
  • 1
  • 2

3 Answers3

8

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.

BlackSheep
  • 1,087
  • 12
  • 29
2

//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..

Kenster
  • 23,465
  • 21
  • 80
  • 106
Alex
  • 403
  • 8
  • 13
1
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];
}
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Neha
  • 27
  • 1
  • 5