2010-05-14 102 views

回答

265

是的,这可以通过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) 

您可以在这里找到更多的信息:Technical QA1630: Using UIWebView to display select document types

+0

非常感谢...... alleus。我们可以将它保存在iPhone的本地文档目录中吗? – 2010-05-14 07:15:53

+3

是的,这是可能的,但是您必须使用其他方法来获取WebView以外的文档。在Stack Overflow上搜索它。例如:http://stackoverflow.com/questions/2102267/downloading-a-file-from-url-and-saving-to-resources-on-iphone – alleus 2010-05-14 07:22:34

+0

使用你的代码,但不适合我工作.... – Jitendra 2013-07-11 06:20:58

9

在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]; 
3
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]; 
+0

请首先创建PDF文件的路径并将其转换为网址,然后使用此代码加载网页视图,它适用于我,请在代码中使用相同的代码 – dheerendra 2013-12-09 12:37:15

2

马丁Allé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]; 
9

网页视图还可以加载使用loadData方法的PDF,如果获得它作为NSData的:

[self.webView loadData:self.pdfData 
       MIMEType:@"application/pdf" 
     textEncodingName:@"UTF-8" 
       baseURL:nil]; 
2

斯威夫特版本:

if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") { 
    let docURL = NSURL(fileURLWithPath: docPath) 
    webView.loadRequest(NSURLRequest(URL: docURL)) 
} 
1
NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"]; 
NSURL *targetURL = [NSURL fileURLWithPath:path]; 
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
[webView loadRequest:request]; 
0
UIWebView *pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; 

NSURL *targetURL = [NSURL URLWithString:@"http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
    [pdfWebView loadRequest:request]; 
    [self.view addSubview:pdfWebView]; 
0

斯威夫特4个

 let webView = UIWebView(frame: view.bounds) 
    let targetURL = Bundle.main.url(forResource: "sampleFileName", withExtension: "pdf") 
    let request = URLRequest(url: targetURL!) 
    webView.loadRequest(request) 
    view.addSubview(webView) 
相关问题