2010-05-14 100 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) 
相關問題