123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- //
- // KMConvertPDFManagerOC.m
- // PDF Master
- //
- // Created by wanjun on 2023/3/8.
- //
- #import "KMConvertPDFManagerOC.h"
- @implementation KMConvertPDFManagerOC
- #pragma mark - Public
- + (NSArray *)supportFileType
- {
- NSMutableArray *supportArray = [NSMutableArray array];
-
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportImages]];
-
- if ([KMConvertPDFManagerOC isSupportConvertPages]) {
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPages]];
- } else if ([KMConvertPDFManagerOC isSupportConvertWord]){
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportWord]];
- }
-
- if ([KMConvertPDFManagerOC isSupportConvertKeynote]) {
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportKeynote]];
- } else if ([KMConvertPDFManagerOC isSupportConvertPPTX]){
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportPPTX]];
- }
- if ([KMConvertPDFManagerOC isSupportConvertNumber]) {
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportNumber]];
- } else if ([KMConvertPDFManagerOC isSupportConvertExcel]){
- /**
- 经测试(2011版 Excel)转档成功以后地址会存在问题,暂时建议不要用
- **/
- [supportArray addObjectsFromArray:[KMConvertPDFManagerOC supportExcel]];
- }
- return supportArray;
- }
- + (void)convertFile:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- NSString *tSavePath = savePath;
- if ([[tSavePath substringToIndex:1] isEqualToString:@"/"]) {
- tSavePath = [tSavePath substringFromIndex:1];
- }
- tSavePath = [tSavePath stringByReplacingOccurrencesOfString:@"/" withString:@":"];
-
- NSString *tFilePath = filePath;
- if ([[tFilePath substringToIndex:1] isEqualToString:@"/"]) {
- tFilePath = [tFilePath substringFromIndex:1];
- }
- tFilePath = [tFilePath stringByReplacingOccurrencesOfString:@"/" withString:@":"];
- NSString * extension = [filePath.pathExtension lowercaseString];
-
- if ([[KMConvertPDFManagerOC supportPages] containsObject:extension]) {
- if ([[KMConvertPDFManagerOC supportWord] containsObject:extension]){
- [KMConvertPDFManagerOC convertWordPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
- complet(success,errorDic);
- }];
- } else {
- [KMConvertPDFManagerOC convertApplicationName:@"Pages" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
- complet(success,errorDic);
- }];
- }
-
- } else if ([[KMConvertPDFManagerOC supportKeynote] containsObject:extension]){
- if ([[KMConvertPDFManagerOC supportPPTX] containsObject:extension]){
- [KMConvertPDFManagerOC convertPPTXPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
- complet(success,errorDic);
- }];
- } else {
- [KMConvertPDFManagerOC convertApplicationName:@"Keynote" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
- complet(success,errorDic);
- }];
- }
- } else if ([[KMConvertPDFManagerOC supportNumber] containsObject:extension]){
- if ([[KMConvertPDFManagerOC supportExcel] containsObject:extension]){
- [KMConvertPDFManagerOC convertExcelPath:tFilePath savePath:tSavePath completionHandler:^(BOOL success,NSDictionary *errorDic){
- complet(success,errorDic);
- }];
- } else {
- [KMConvertPDFManagerOC convertApplicationName:@"Numbers" filePath:tFilePath savePath:tSavePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
- complet(success,errorDic);
- }];
- }
- } else if ([[KMConvertPDFManagerOC supportImages] containsObject:extension]){
- [KMConvertPDFManagerOC convertImagePath:filePath savePath:savePath completionHandler:^(BOOL success, NSDictionary *errorDic) {
- complet(success,errorDic);
- }];
- } else {
- NSDictionary * dic = nil;
- complet(NO,dic);
- }
- }
- + (NSArray *)supportImages
- {
- return [NSArray arrayWithObjects:@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi", nil];
- }
- #pragma mark - convert Word
- + (BOOL)isSupportConvertWord
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Word"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportWord
- {
- return [NSArray arrayWithObjects:@"docx",@"doc",nil];
- }
- + (void)convertWordPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath];
- convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
- convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Word\"\n"];
- convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
- convertString = [convertString stringByAppendingFormat:@"open file filePath\n"];
- convertString = [convertString stringByAppendingFormat:@"save as active document file format format PDF file name savePath\n"];
- convertString = [convertString stringByAppendingFormat:@"close active document\n"];
- convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
- convertString = [convertString stringByAppendingFormat:@"end tell\n"];
-
- NSDictionary * dic = nil;
- NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
- [docScript executeAndReturnError:&dic];
- dispatch_async(dispatch_get_main_queue(), ^{
- complet(YES,dic);
- });
- });
- }
- #pragma mark - convert Numbers
- + (BOOL)isSupportConvertNumber
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Numbers"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportNumber
- {
- return [NSArray arrayWithObjects:@"xls",@"xlsx",@"numbers",@"csv",nil];
- }
- + (void)convertApplicationName:(NSString *)appName filePath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSString * convertString = [NSString stringWithFormat:@"tell application \"%@\"\n",appName];
- convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
- convertString = [convertString stringByAppendingFormat:@"set in_file to \"%@\"\n",filePath];
- convertString = [convertString stringByAppendingFormat:@"set out_file to \"%@\"\n",savePath];
- convertString = [convertString stringByAppendingFormat:@"set mydoc to open file in_file\n"];
- convertString = [convertString stringByAppendingFormat:@"export mydoc to file out_file as PDF\n"];
- convertString = [convertString stringByAppendingFormat:@"close mydoc saving no\n"];
- convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
- convertString = [convertString stringByAppendingFormat:@"end tell"];
- NSDictionary * dic = nil;
- NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
- [docScript executeAndReturnError:&dic];
- dispatch_async(dispatch_get_main_queue(), ^{
- complet(YES,dic);
- });
- });
- }
- #pragma mark - convert Keynote
- + (BOOL)isSupportConvertKeynote
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Keynote"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportKeynote
- {
- return [NSArray arrayWithObjects:@"ppt",@"pptx",@"key",nil];
- }
- #pragma mark - convert Pages
- + (BOOL)isSupportConvertPages
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Pages"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportPages
- {
- return [NSArray arrayWithObjects:@"pages",@"docx",@"doc",@"txt",@"rtf",nil];
- }
- #pragma mark - convert PPTX
- + (BOOL)isSupportConvertPPTX
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft PowerPoint"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportPPTX
- {
- return [NSArray arrayWithObjects:@"ppt",@"pptx",nil];
- }
- + (void)convertPPTXPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSString * convertString = [NSString stringWithFormat:@"tell application \"Microsoft PowerPoint\"\n"];
- convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
- convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
- convertString = [convertString stringByAppendingFormat:@"set filePath to \"%@\"\n",filePath];
- convertString = [convertString stringByAppendingFormat:@"open file filePath\n"];
- convertString = [convertString stringByAppendingFormat:@"save active presentation in savePath as save as PDF\n"];
- convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
- convertString = [convertString stringByAppendingFormat:@"close active presentation\n"];
- convertString = [convertString stringByAppendingFormat:@"end tell\n"];
-
- NSDictionary * dic = nil;
- NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
- [docScript executeAndReturnError:&dic];
- dispatch_async(dispatch_get_main_queue(), ^{
- complet(YES,dic);
- });
- });
- }
- #pragma mark - convert Excel
- + (BOOL)isSupportConvertExcel
- {
- NSString * fullPath = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"Microsoft Excel"];
- if (fullPath) {
- return YES;
- }
- return NO;
- }
- + (NSArray *)supportExcel
- {
- return [NSArray arrayWithObjects:@"xlsx",@"xls",nil];
- }
- + (void)convertExcelPath:(NSString *)filePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSString * convertString = [NSString stringWithFormat:@"set filePath to\"%@\"\n",filePath];
- convertString = [convertString stringByAppendingFormat:@"set savePath to \"%@\"\n",savePath];
- convertString = [convertString stringByAppendingFormat:@"set tFile to (POSIX path of filePath) as POSIX file\n"];
- convertString = [convertString stringByAppendingFormat:@"tell application \"Microsoft Excel\"\n"];
- convertString = [convertString stringByAppendingFormat:@"set isRun to running\n"];
- convertString = [convertString stringByAppendingFormat:@"set wkbk1 to open workbook workbook file name tFile\n"];
- convertString = [convertString stringByAppendingFormat:@"alias savePath\n"];
- convertString = [convertString stringByAppendingFormat:@"save workbook as wkbk1 filename savePath file format PDF file format with overwrite\n"];
- convertString = [convertString stringByAppendingFormat:@"close wkbk1 saving no\n"];
- convertString = [convertString stringByAppendingFormat:@"if not isRun then quit\n"];
- convertString = [convertString stringByAppendingFormat:@"end tell\n"];
-
- NSDictionary * dic = nil;
- NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString];
- [docScript executeAndReturnError:&dic];
- dispatch_async(dispatch_get_main_queue(), ^{
- complet(YES,dic);
- });
- });
- }
- #pragma mark - convert Image
- + (void)convertImagePath:(NSString *)imgaePath savePath:(NSString *)savePath completionHandler:(void (^)(BOOL success,NSDictionary *errorDic))complet
- {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- PDFDocument *pdf = [[PDFDocument alloc] init];
- NSImage *imag = [[NSImage alloc] initWithContentsOfFile:imgaePath];
- PDFPage *page = [[PDFPage alloc] initWithImage:imag];
- [pdf insertPage:page atIndex: [pdf pageCount]];
-
- BOOL isSucceed = [pdf writeToFile:savePath];
- dispatch_async(dispatch_get_main_queue(), ^{
- NSDictionary * dic = nil;
- complet(isSucceed,dic);
- });
- });
- }
- @end
|