|
@@ -0,0 +1,198 @@
|
|
|
+//
|
|
|
+// 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: Any];
|
|
|
+ let isContainImages = Bool((convertData[params_contain_images] as? String) ?? "true") ?? true
|
|
|
+ let isContainAnnotation = Bool((convertData[params_contain_annotations] as? String) ?? "true") ?? true;
|
|
|
+ let filePath = convertData[params_file_path] as! String;
|
|
|
+ let fileName = (convertData[params_file_name] as? String) ?? "";
|
|
|
+ let convertType = convertData[params_convert_type] as! String;
|
|
|
+ let workSheetOptions = (convertData[params_worksheet_options] as? String) ?? "";
|
|
|
+ let contentOptions = (convertData[params_content_options] as? String) ?? "";
|
|
|
+ let imageDpi = (convertData[params_image_dpi] as? String) ?? "300";
|
|
|
+ let imagePageOptions = (convertData[params_image_page_options] as? String) ?? "";
|
|
|
+ let mergeCSV = Bool((convertData[params_merge_csv] as? String) ?? "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, task_start, mPath)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func converter(_ converter: CPDFConverter!, pageIndex index: UInt, pageCount count: UInt) {
|
|
|
+ let progress = Double(index) / Double(count)
|
|
|
+ result?(Int(progress*100), task_converting, mPath)
|
|
|
+ }
|
|
|
+
|
|
|
+ @objc func converter(_ converter: CPDFConverter!, didEndConvert error: Error!) {
|
|
|
+ result?(100, task_success, mPath)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|