123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- //
- // ConvertUtil.swift
- // Runner
- //
- //
- // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- import Foundation
- import ComPDFKit_Conversion
- class ConvertUtil : NSObject,CPDFConverterDelegate{
-
-
- var pdfConvert : CPDFConverter?
- var mPath:String?
- var result: ((Int, Int, String?) -> Void)?
-
- func getConvertOptions(withArguments arguments: Any?) -> CPDFConvertOptions {
-
- let convertData = arguments as! [String: String];
- let isContainImages = Bool(convertData[params_contain_images] ?? "true") ?? true
- let isContainAnnotation = Bool(convertData[params_contain_annotations] ?? "true") ?? true;
- let filePath = convertData[params_file_path];
- let fileName = convertData[params_file_name];
- let convertType = convertData[params_convert_type];
- let workSheetOptions = convertData[params_worksheet_options];
- let contentOptions = convertData[params_content_options];
- let imageDpi = convertData[params_image_dpi] ?? "300";
- let imagePageOptions = convertData[params_image_page_options];
- let mergeCSV = Bool(convertData[params_merge_csv] ?? "false") ?? false;
- let htmlPageOptions = convertData[params_html_page_options];
- print("convertType:\(convertType)")
- print("isContainImages:\(isContainImages)")
- print("isContainAnnotations:\(isContainAnnotation)")
-
- switch convertType!.uppercased() {
- case ConvertType.PPT.rawValue:
- let pptOptions = CPDFConvertPPTOptions()
- pptOptions.isContainImages = isContainImages
- pptOptions.isContainAnnotations = isContainAnnotation
- return pptOptions
- case ConvertType.WORD.rawValue:
- let wordOptions = CPDFConvertWordOptions()
- wordOptions.isContainImages = isContainImages
- wordOptions.isContainAnnotations = isContainAnnotation
- return wordOptions
- case ConvertType.EXCEL.rawValue:
- let excelOptions = CPDFConvertExcelOptions()
- excelOptions.isContainImages = isContainImages
- excelOptions.isContainAnnotations = isContainAnnotation
-
- switch workSheetOptions {
- case "ForEachTable":
- excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forEachTable
- case "ForEachPage":
- excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forEachPage
- default :
- excelOptions.worksheetOptions = CPDFConvertExcelWorksheetOptions.forTheDocument
- }
-
- switch contentOptions {
- case "OnlyText":
- excelOptions.contentOptions = CPDFConvertExcelContentOptions.onlyText
- case "OnlyTable":
- excelOptions.contentOptions = CPDFConvertExcelContentOptions.onlyTable
- default :
- excelOptions.contentOptions = CPDFConvertExcelContentOptions.allContent
- }
- return excelOptions
-
- case ConvertType.TXT.rawValue:
- return CPDFConvertTxtOptions()
-
- case ConvertType.IMAGE.rawValue:
- let imageOptions = CPDFConvertImgOptions()
-
- imageOptions.imageDpi = Int32(imageDpi) ?? 300
- imageOptions.isContainAnnotations = isContainAnnotation
- switch imagePageOptions {
- case "JPEG":
- imageOptions.type = CPDFConvertImgType.JPEG
- case "PNG":
- imageOptions.type = CPDFConvertImgType.PNG
- default:
- imageOptions.type = CPDFConvertImgType.PNG
- }
- return imageOptions
-
- case ConvertType.CSV.rawValue:
- let csvOptions = CPDFConvertCsvOptions()
- csvOptions.isMergeCSV = mergeCSV
- return csvOptions
-
- case ConvertType.RTF.rawValue:
- let rtfOptions = CPDFConvertRtfOptions()
- rtfOptions.isContainImages = isContainImages
- rtfOptions.isContainAnnotations = isContainAnnotation
- return rtfOptions
-
- case ConvertType.HTML.rawValue:
- let htmlOptions = CPDFConvertHtmlOptions()
- htmlOptions.isContainImages = isContainImages
- htmlOptions.isContainAnnotations = isContainAnnotation
- return htmlOptions
- default:
- return CPDFConvertOptions()
-
- }
-
- }
-
-
- func convert(options :CPDFConvertOptions, filePath:String,fileName : String, convertType : String, onProgress : @escaping (_ progress : Int, _ status : Int, _ outputPath : String?) -> Void) {
-
- result = onProgress
-
- var outPutPath = NSHomeDirectory() + "/Documents/ConversionDemo"
- if !FileManager.default.fileExists(atPath: outPutPath) {
- try! FileManager.default.createDirectory(atPath: outPutPath, withIntermediateDirectories: true)
- }
- let encodeString = filePath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
- let fileURL = URL(string:encodeString ?? "")
-
- switch convertType.uppercased() {
- case ConvertType.PPT.rawValue:
-
- pdfConvert = CPDFConverterPPT(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSince1970, dateFormat:nil)).pptx")
-
- case ConvertType.WORD.rawValue:
-
- pdfConvert = CPDFConverterWord(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).docx")
-
- case ConvertType.EXCEL.rawValue:
- pdfConvert = CPDFConverterExcel(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).xlsx")
- case ConvertType.TXT.rawValue:
- pdfConvert = CPDFConverterTxt(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).txt")
-
- case ConvertType.IMAGE.rawValue:
- pdfConvert = CPDFConverterImg(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
-
- case ConvertType.CSV.rawValue:
- pdfConvert = CPDFConverterCsv(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
-
- case ConvertType.RTF.rawValue:
- pdfConvert = CPDFConverterRtf(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).rtf")
-
- case ConvertType.HTML.rawValue:
- pdfConvert = CPDFConverterHtml(url: fileURL, password: "")
- outPutPath = outPutPath.appending("/converted_\(fileName)_\(self.timeIntervalChangetoTimeStr(timeInterval: NSDate().timeIntervalSinceNow, dateFormat:nil)).zip")
-
- default:
- pdfConvert = CPDFConverter()
- }
- pdfConvert?.delegate = self
- mPath = outPutPath;
- pdfConvert?.convert(toFilePath: outPutPath, pageIndexs: nil, options: nil)
- }
-
- func timeIntervalChangetoTimeStr(timeInterval:TimeInterval, dateFormat:String?) -> String {
- let date:NSDate = NSDate.init(timeIntervalSince1970: timeInterval/1000)
- let formatter = DateFormatter.init()
- if dateFormat == nil {
- formatter.dateFormat = "yyyy-MM-dd_HH:mm:ss"
- }else{
- formatter.dateFormat = dateFormat
- }
- return formatter.string(from: date as Date)
- }
-
- @objc func converter(_ converter: CPDFConverter!, didStartConvert error: Error!) {
- result?(0, converting, mPath)
- }
-
- @objc func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
- let progress = Double(index) / Double(count)
- result?(Int(progress*100), converting, mPath)
- }
-
- @objc func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
- result?(100, convert_success, mPath)
- }
-
-
- }
|