// // KMOCTool.m // PDF Reader Pro // // Created by liujiajie on 2023/11/15. // #import "KMOCTool.h" #import #import @implementation KMOCTool + (void)createPDFFile:(NSString *)filePath imagePaths:(NSArray *)paths results:(NSArray *)resultsArray scale:(CGFloat)scale { if (paths.count < 1) { return; } CFStringRef path = (__bridge CFStringRef)filePath; CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0); CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Kdan Mobile PDF Reader")); CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &CGRectZero, myDictionary); CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0); CGContextSetTextDrawingMode(pdfContext, kCGTextFill); CFRelease(myDictionary); CFRelease(url); for (int i=0; i *strings) { NSMutableData *pdfData = [[NSMutableData alloc] init]; CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData); CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("PDF Reader Pro")); CGRect mediaBox = CGRectMake(0, 0, 595, 842); CGContextRef pdfContext = CGPDFContextCreate(consumer, &mediaBox, myDictionary); CGContextSetRGBFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0); CGContextSetTextDrawingMode(pdfContext, kCGTextFill); CFRelease(myDictionary); for (NSAttributedString *attributeString in strings) { if (floor(NSAppKitVersionNumber) >= NSAppKitVersionNumber10_10) { NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributeString]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; while (YES) { NSSize contentSize = NSMakeSize(mediaBox.size.width-40, mediaBox.size.height-40); NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:contentSize]; [layoutManager addTextContainer:textContainer]; NSRange rang = [layoutManager glyphRangeForTextContainer:textContainer]; if (rang.length <= 0 || rang.length + rang.location > attributeString.length) { break; } NSAttributedString *attStr = [attributeString attributedSubstringFromRange:rang]; [NSGraphicsContext saveGraphicsState]; // [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:NO]]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithCGContext:pdfContext flipped:NO]]; CGContextBeginPage(pdfContext, &mediaBox); [attStr drawInRect:CGRectMake(20, 20, mediaBox.size.width-40, mediaBox.size.height-40)]; CGContextEndPage(pdfContext); [NSGraphicsContext restoreGraphicsState]; } } else { [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithCGContext:pdfContext flipped:NO]]; CGContextBeginPage(pdfContext, &mediaBox); [attributeString drawInRect:CGRectMake(20, 20, mediaBox.size.width-40, mediaBox.size.height-40)]; CGContextEndPage(pdfContext); [NSGraphicsContext restoreGraphicsState]; } } CGPDFContextClose(pdfContext); CGContextRelease (pdfContext); return pdfData; } + (NSDictionary *)convertExcelToPdfFilePath:(NSString *)filePath savePath:(NSString *)savePath{ 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]; return dic; } + (NSDictionary *)convertOfficeFileToPdf:(NSString *)convertString{ NSDictionary * dic = nil; NSAppleScript *docScript = [[NSAppleScript alloc] initWithSource:convertString]; [docScript executeAndReturnError:&dic]; return dic; } + (NSString *)getModificationDate:(NSDate *)modificationDate { if (!modificationDate) { return nil; } NSTimeZone *timeZone = [NSTimeZone systemTimeZone]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:timeZone]; [formatter setDateFormat:@"yyyyMMddHHmmssZ"]; NSString *dateString = [formatter stringFromDate:modificationDate]; /* 修改date字符串格式为PDF标准格式 */ NSRange tRange; if ([dateString rangeOfString:@"+"].length > 0) { tRange = [dateString rangeOfString:@"+"]; } else if ([dateString rangeOfString:@"-"].length > 0) { tRange = [dateString rangeOfString:@"-"]; } else if ([dateString rangeOfString:@"Z"].length > 0){ tRange = [dateString rangeOfString:@"Z"]; } NSMutableString *date = [NSMutableString string]; [date appendFormat:@"D:%@", [dateString substringToIndex:tRange.location]]; tRange.length = 3; [date appendFormat:@"%@", [dateString substringWithRange:tRange]]; tRange.location += 3; [date appendFormat:@"'%@", [dateString substringFromIndex:tRange.location]]; return date; } @end