我们可以从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)
您可以在此处找到更多信息:技术QA1630:使用UIWebView显示选择的文档类型.
UIWebview
如果您将其作为NSData获取,Webview也可以加载pdf using 方法:
[self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];
在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];