iPhone:我们可以使用UIWebView打开pdf文件吗?

我们可以从UIWebView打开PDF文件吗?

如果有任何人已经实施了,那么请帮助我。

任何代码片段或任何网站链接将有所帮助。

是的,这可以通过UIWebView来完成。

如果您试图显示驻留在某个服务器上的PDF文件,则可以直接将其加载到Web视图中:

Objective-C的

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; NSURL *targetURL = [NSURL URLWithString:@"https://www.example.com/document.pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView]; 

迅速

 let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200)) let targetURL = NSURL(string: "https://www.example.com/document.pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code let request = NSURLRequest(URL: targetURL) webView.loadRequest(request) view.addSubview(webView) 

或者,如果您的应用程序捆绑了PDF文件(在本例中名为“document.pdf”):

Objective-C的

 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; NSURL *targetURL = [[NSBundle mainBundle] URLForResource:@"document" withExtension:@"pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView]; 

迅速

 let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200)) let targetURL = NSBundle.mainBundle().URLForResource("document", withExtension: "pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code let request = NSURLRequest(URL: targetURL) webView.loadRequest(request) view.addSubview(webView) 

您可以在这里find更多信息: 技术QA1630:使用UIWebView显示select的文档types 。

在webview中使用这个打开的pdf文件

 NSString *path = [[NSBundle mainBundle] pathForResource:@"mypdf" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; [[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES]; [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]]; [webView loadRequest:request]; [self.view addSubview:webView]; [webView release]; 

Webviews也可以使用loadData方法加载pdf,如果您将其作为NSData获取:

 [self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil]; 
 NSString *folderName=[NSString stringWithFormat:@"/documents/%@",[tempDictLitrature objectForKey:@"folder"]]; NSString *fileName=[tempDictLitrature objectForKey:@"name"]; [self.navigationItem setTitle:fileName]; NSString *type=[tempDictLitrature objectForKey:@"type"]; NSString *path=[[NSBundle mainBundle]pathForResource:fileName ofType:type inDirectory:folderName]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 730)]; [webView setBackgroundColor:[UIColor lightGrayColor]]; webView.scalesPageToFit = YES; [[webView scrollView] setContentOffset:CGPointZero animated:YES]; [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]]; [webView loadRequest:request]; [self.view addSubview:webView]; 

Swift版本:

 if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") { let docURL = NSURL(fileURLWithPath: docPath) webView.loadRequest(NSURLRequest(URL: docURL)) } 

对MartinAlléus的回答进行更新,无论是手机还是iPad,无需硬编码即可获得全屏显示:

 CGRect rect = [[UIScreen mainScreen] bounds]; CGSize screenSize = rect.size; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView]; 
 NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; 
 UIWebView *pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; NSURL *targetURL = [NSURL URLWithString:@"application/uploads/2014/12/pdf-sample.pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [pdfWebView loadRequest:request]; [self.view addSubview:pdfWebView];