IOS 使用webview 显示 doc/docx/xls/pdf等

在一款项目里添加阅读各种文档功能 那么对在线的文档或者是下载后的文档 进行阅读,比如 doc/docx/xls/pdf等文件

有两种方法总结如下:

1.
- (void)viewDidLoad
{
    [super viewDidLoad];
    webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, Phone_Weight, Phone_Height)];
    [self loadDocument:@"1.docx" inView:webView];
    webView.scalesPageToFit=YES;//点击伸缩效果的
    webView.delegate=self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
     [self.view addSubview:webView1];
}

2.
- (void)viewDidLoad
{
    [super viewDidLoad];
    webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, Phone_Weight, Phone_Height)];
    webView.scalesPageToFit=YES;//点击伸缩效果的
    NSString *documentLocation=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"docx"];
    NSURL *myDocument=[NSURL fileURLWithPath:documentLocation];
    NSURLRequest *request=[NSURLRequest requestWithURL:myDocument];
    [webView loadRequest:request];
    webView.delegate=self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
     [self.view addSubview:webView1];
}
webView.scalesPageToFit=YES;这句很重要,不然 读出的文档不能很好的适应屏幕
还有一点,在工程中添加测试文档时候要在Add to targets第一项打对勾 不然路径无效,如图:

效果图如下:(doc文件)



原文地址:https://www.cnblogs.com/someonelikeyou/p/3779286.html