iOS中查阅&打印 .pdf .doc .docx文件

发布时间:2019-09-16  栏目:Python  评论:0 Comments

怎么让应用软件援协助调查看&打字与印刷word,pdf文书档案?

iOS是有支撑这一个功效的控件QLPreviewController.

只需导入库:

图片 1

doc文件如何调换来docx文件 5C
哪些不注重插件,通过java代码使doc文件转换来docx文件?

福寿双全的功效是在二个webView中式茶食击获取下载地址进行下载二个文件,之后用QLPreviewController展开,QLPreviewController帮助打字与印刷效率.

#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

– viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a
nib.

[self initWeb];

}

– 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

– 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 来下载附属类小部件)

– downloadFileWithOption:(NSDictionary *)paramDic
withInferface:(NSString*)requestURL savedPath:(NSString*)savedPath
downloadSuccess:(AFHTTPRequestOperation *operation, id
responseObject))success downloadFailure:(AFHTTPRequestOperation
*operation,NSError *error))failure progress:(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 = totalBytesRead / totalBytesExpectedToRead;

progress;

// NSLog(@”download:%f”, 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{

//推断格式

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;

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 {

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;}- previewController:
(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

{

return [NSURL fileURLWithPath:_path];

}

– previewControllerDidDismiss:(QLPreviewController *)controller

{

if(![_path isEqual: @””]){

NSFileManager * fileManager = [[NSFileManager alloc]init];

[fileManager removeItemAtPath:_path error:nil];

}

}

@end

随后的开拓的页面如图:

图片 2图片 3

Github地址

留下评论

网站地图xml地图