iOS中查看&打印 .pdf .doc .docx文件
来源:互联网 发布:2k16mc亚洲人捏脸数据 编辑:程序博客网 时间:2024/05/20 09:48
一、背景需求介绍
怎么让APP支持查看&打印word,pdf文档?
iOS是有支持这个功能的控件QLPreviewController.
配置:
只需导入库:
二、示例代码如下:
实现的功能是在一个webView中点击获取下载地址进行下载一个文件,之后用QLPreviewController打开,QLPreviewController支持打印功能.
在.h文件中
#import#import@interface ViewController : UIViewController//打开word文档需要引入的视图控制器@property(nonatomic,strong) QLPreviewController *previewController;@end
在.m文件中
#import "ViewController.h"#import "AFNetworking.h"@interface ViewController ()//保存本地的地址@property (nonatomic ,copy) NSString *path;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initWeb]; }- (void)initWeb{UIWebView *web =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];NSString *url = @"网页地址";NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];web.delegate = self;[web loadRequest:request];[self.view addSubview:web];}#pragma UIWebViewDelegate- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{//判断是否是单击if (navigationType == UIWebViewNavigationTypeLinkClicked){//点击网页中的文件截取到的网址(文件下载地址)NSURL *url = [request URL];if([[UIApplication sharedApplication]canOpenURL:url]){NSLog(@"url==%@",url);//缓存到本地沙盒的地址NSString *savedPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/附件详情"];[self downloadFileWithOption:nil withInferface:[url absoluteString] savedPath:savedPath downloadSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {} downloadFailure:^(AFHTTPRequestOperation *operation, NSError *error) {} progress:^(float progress) {}];}return NO;}return YES;}//下载文档,并保存(这里比较low还用的AFNetworking 2.x 来下载附件)- (void)downloadFileWithOption:(NSDictionary *)paramDic withInferface:(NSString*)requestURL savedPath:(NSString*)savedPath downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success downloadFailure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))failure progress:(void (^)(float progress))progress{AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];NSMutableURLRequest *request =[serializer requestWithMethod:@"GET" URLString:requestURL parameters:paramDic error:nil];AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:savedPath append:NO]];[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {float p = (float)totalBytesRead / totalBytesExpectedToRead;progress(p);// NSLog(@"download:%f", (float)totalBytesRead / totalBytesExpectedToRead);}];[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {success(operation,responseObject);_path = nil;NSDictionary *dic = operation.response.allHeaderFields;NSLog(@"下载成功,%@",dic);NSString *fileName = dic[@"Content-Disposition"];if(fileName){//判断格式if ([fileName rangeOfString:@".docx"].location !=NSNotFound) {// NSLog(@"docx文档");NSFileManager *manager = [NSFileManager new];_path = [savedPath stringByAppendingString:@".docx"];[manager moveItemAtPath:savedPath toPath:_path error:nil];}else if ([fileName rangeOfString:@".doc"].location !=NSNotFound){// NSLog(@"doc文档");NSFileManager *manager = [NSFileManager new];_path = [savedPath stringByAppendingString:@".doc"];[manager moveItemAtPath:savedPath toPath:_path error:nil];}else if([fileName rangeOfString:@".png"].location !=NSNotFound){// NSLog(@"图片");_path = [savedPath stringByAppendingString:@".png"];NSFileManager *manager = [NSFileManager new];_path = [savedPath stringByAppendingString:@".png"];[manager moveItemAtPath:savedPath toPath:_path error:nil];}else if([fileName rangeOfString:@".jpg"].location !=NSNotFound){// NSLog(@"图片");_path = [savedPath stringByAppendingString:@".jpg"];NSFileManager *manager = [NSFileManager new];_path = [savedPath stringByAppendingString:@".jpg"];[manager moveItemAtPath:savedPath toPath:_path error:nil];}}if (_path) {self.previewController = [QLPreviewController new];self.previewController.dataSource = self;[self.previewController setDelegate:self];//跳转到打开word文档页面[self presentViewController:self.previewController animated:YES completion:nil];}} failure:^(AFHTTPRequestOperation *operation, NSError *error) {success(operation,error);NSLog(@"下载失败,error==%@",error);}];[operation start];}#pragma QLPreViewDelegate- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller{ return 1;}- (id)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{return [NSURL fileURLWithPath:_path];}- (void)previewControllerDidDismiss:(QLPreviewController *)controller{if(![_path isEqual: @""]){NSFileManager * fileManager = [[NSFileManager alloc]init];[fileManager removeItemAtPath:_path error:nil];}}@end
之后的打开的页面如图:
点击print后,连接支持airPrint的打印机进行打印:
Demo下载链接:点击这里
0 0
- iOS中查看&打印 .pdf .doc .docx文件
- tableView 去掉多余的行数 && iOS 中直接用WebView 加载pdf doc docx 等文件
- 在网页中在线查看文档(doc、docx 、xls 、xlsx、 pdf 、swf )
- Android中pdf,doc,docx,xls,xlsx,ppt,pptx等office文件预览
- 获取txt pdf doc docx类型文件内容
- openoffice3.* doc/docx转pdf
- pdf/doc/text/jpg等文件查看
- 导入(doc,docx,pdf格式)简历,doc/docx,转pdf格式,并且检索doc,docx,pdf,字段
- 导入(doc,docx,pdf格式)简历,doc/docx,转pdf格式,并且检索doc,docx,pdf,字段
- Converting .docx into (.doc, .pdf, .html)
- 利用openoffice将doc、docx转为pdf
- 用java统计pdf,ppt,pptx,doc,docx文件的总页码
- Java 使用jacob ppt文件转pptx,doc转docx;word 转html、pdf等
- PHP上传和下载与删除doc、docx、ppt、pdf文件
- "doc", "docx", "xls", "xlsx", "ppt", "pptx",txt。等文件转化为pdf
- VBA批量转换:docx转pdf、doc、rtf、txt以及doc转docx
- android解析doc,docx,xls,xlsx文件
- java读取txt、doc、docx文件
- 搭建MySQL数据库服务RHCE-Day10
- 图像增强--空间域图像增强
- Android的系统架构
- HTTPS的交互流程
- 拐角II
- iOS中查看&打印 .pdf .doc .docx文件
- ZOJ
- android API文档地址
- 关于递推 2
- IPython下目录的操作-魔力函数(cd pwd bookmark dhist )- 千月的python linux 系统管理指南学习笔记(7)
- JSON解析类库之Gson(1) --- 简单JavaBean对象、带泛型的Bean对象与JSON互转
- Idea 14 最详细创建最简单web项目,并且发布在tomcat
- Linux安装-gedit
- 将Sublime打造成为C\C++的编译器