ConverterHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using ComPDFKit_Conversion.Converter;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using Microsoft.AppCenter.Crashes.Ingestion.Models;
  10. using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  11. using ComPDFKit_Conversion.Options;
  12. using PDF_Office.CustomControl;
  13. using Exception = System.Exception;
  14. using ImageMagick;
  15. using System.IO.Compression;
  16. namespace PDF_Office.Helper
  17. {
  18. public static class ConverterHelper
  19. {
  20. public static CPDFConverterWord wordConverter = null;
  21. public static CPDFConverterExcel excelConverter = null;
  22. public static CPDFConverterPPT pptConverter = null;
  23. public static CPDFConverterTxt txtConverter = null;
  24. public static CPDFConverterCsv csvConverter = null;
  25. public static CPDFConverterImg imgConverter = null;
  26. public static CPDFConverterRTF rtfConverter = null;
  27. public static CPDFConverterHTML htmlConverter = null;
  28. public static async Task<bool> WordConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertWordOptions wordOptions = null)
  29. {
  30. bool result = false;
  31. try
  32. {
  33. int[] pageArray = pageIndexLists.ToArray();
  34. wordConverter = new CPDFConverterWord(inputpath, pawssword);
  35. string outputFolder = outputpath;
  36. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  37. ConvertError error = ConvertError.ERR_UNKNOWN;
  38. string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".docx");
  39. result = await Task.Run(() => wordConverter.Convert(outputFolder, ref outputFileName, wordOptions, pageArray, ref error, getProgress));
  40. if (result)
  41. {
  42. CommonHelper.ShowFileBrowser(outputFileName.Replace("/","\\"));
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. }
  48. return result;
  49. }
  50. public static async Task<bool> PPTConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertPPTOptions pptOptions=null)
  51. {
  52. bool result = false;
  53. try
  54. {
  55. int[] pageArray = pageIndexLists.ToArray();
  56. pptConverter = new CPDFConverterPPT(inputpath, pawssword);
  57. string outputFolder = outputpath;
  58. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  59. ConvertError error = ConvertError.ERR_UNKNOWN;
  60. string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".ppt");
  61. result = await Task.Run(() => pptConverter.Convert(outputFolder, ref outputFileName, pptOptions,pageArray, ref error, getProgress));
  62. if (result)
  63. {
  64. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. }
  70. return result;
  71. }
  72. public static async Task<bool> ExcelConvert(string inputpath, string outputpath,OnProgress getProgress , List<int> pageIndexLists, string pawssword,CPDFConvertExcelOptions ExcelOption)
  73. {
  74. bool result = false;
  75. try
  76. {
  77. excelConverter = new CPDFConverterExcel(inputpath,pawssword);
  78. int[] pageArray = pageIndexLists.ToArray();
  79. string outputFolder = outputpath;
  80. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  81. ConvertError error = ConvertError.ERR_UNKNOWN;
  82. result = await Task.Run(() => excelConverter.Convert(outputFolder, ref outputFileName, ExcelOption, pageArray, ref error, getProgress));
  83. if (result)
  84. {
  85. if (File.Exists(outputFileName))
  86. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  87. else if (outputFileName == "NoTable")
  88. MessageBoxEx.Show("No Table");
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. }
  94. return result;
  95. }
  96. //public static async Task<bool> TableConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
  97. //{
  98. // bool result = false;
  99. // try
  100. // {
  101. // int[] pageArray = pageIndexLists.ToArray();
  102. // tableConverter = new CPDFConverterTable(inputpath, pawssword);
  103. // string outputFolder = outputpath;
  104. // string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  105. // ConvertError error = ConvertError.ERR_UNKNOWN;
  106. // string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".xlsx");
  107. // result = await Task.Run(() => tableConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));
  108. // }
  109. // catch (Exception ex)
  110. // {
  111. // }
  112. // return result;
  113. //}
  114. public static async Task<bool> CSVConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertCsvOptions csvOptions =null)
  115. {
  116. bool result = false;
  117. try
  118. {
  119. int[] pageArray = pageIndexLists.ToArray();
  120. csvConverter = new CPDFConverterCsv(inputpath, pawssword);
  121. string outputFolder = outputpath;
  122. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  123. ConvertError error = ConvertError.ERR_UNKNOWN;
  124. string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".csv");
  125. result = await Task.Run(() => csvConverter.Convert(outputFolder, ref outputFileName, csvOptions, pageArray, ref error, getProgress));
  126. if (result)
  127. {
  128. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. }
  134. return result;
  135. }
  136. public static async Task<bool> TxtConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword,CPDFConvertTxtOptions txtOptions =null)
  137. {
  138. bool result = false;
  139. try
  140. {
  141. int[] pageArray = pageIndexLists.ToArray();
  142. txtConverter = new CPDFConverterTxt(inputpath, pawssword);
  143. string outputFolder = outputpath;
  144. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  145. ConvertError error = ConvertError.ERR_UNKNOWN;
  146. string filename = GetFileNameAddSuffix(outputFolder, outputFileName, ".txt");
  147. result = await Task.Run(() => txtConverter.Convert(outputFolder, ref outputFileName, txtOptions, pageArray, ref error, getProgress));
  148. if (result)
  149. {
  150. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. }
  156. return result;
  157. }
  158. public static void ImageMagickPDFToImage(string imageType,string filePath,string outputFolder,string outputFileName) {
  159. ZipFile.ExtractToDirectory(filePath, outputFolder + "\\" + outputFileName);
  160. var files = Directory.GetFiles(outputFolder + "\\" + outputFileName, "*.png");
  161. int i=0;
  162. var images = new MagickImageCollection();
  163. foreach (var file in files)
  164. {
  165. //pdf整体转为gif,将.gifoff改为.gif
  166. Trace.WriteLine(file);
  167. if (imageType == ".gifoff") {
  168. images.Add(file);
  169. images[i].AnimationDelay = 100; // in this example delay is 1000ms/1sec
  170. images[i].Flip();
  171. }
  172. using (var image = new MagickImage(file))
  173. {
  174. if (imageType != ".gifoff")
  175. {
  176. //Save frame as jpg
  177. //image.Format = MagickFormat.Jp2;
  178. image.Write(file.Remove(file.LastIndexOf(".png"), 4) + imageType);
  179. }
  180. // 删除该文件
  181. System.IO.File.Delete(file);
  182. }
  183. }
  184. if (imageType == ".gifoff")
  185. {
  186. // Optionally reduce colors
  187. var settings = new QuantizeSettings();
  188. settings.Colors = 256;
  189. images.Quantize(settings);
  190. // Optionally optimize the images (images should have the same size).
  191. images.Optimize();
  192. // Save gif
  193. int fileIndex = 1;
  194. if (File.Exists(outputFolder + "\\" + outputFileName + ".gif"))
  195. {
  196. while (fileIndex >= 1)
  197. {
  198. if (!File.Exists(outputFolder + "\\" + outputFileName + "_" + fileIndex + ".gif"))
  199. {
  200. images.Write(outputFolder + "\\" + outputFileName + ".gif");
  201. fileIndex = -1;
  202. }
  203. fileIndex++;
  204. }
  205. }
  206. else { images.Write(outputFolder + "\\" + outputFileName + ".gif"); }
  207. }
  208. else {
  209. int fileIndex = 1;
  210. if (File.Exists(outputFolder + "\\" + outputFileName + ".zip"))
  211. {
  212. while (fileIndex>=1) {
  213. if (!File.Exists(outputFolder + "\\" + outputFileName + "_" + fileIndex + ".zip"))
  214. {
  215. ZipFile.CreateFromDirectory(outputFolder + "\\" + outputFileName, outputFolder + "\\" + outputFileName + "_" + fileIndex + ".zip");
  216. fileIndex=-1;
  217. }
  218. fileIndex++;
  219. }
  220. }
  221. else { ZipFile.CreateFromDirectory(outputFolder + "\\" + outputFileName, outputFolder + "\\" + outputFileName + ".zip"); }
  222. }
  223. DirectoryInfo di = new DirectoryInfo(outputFolder + "\\" + outputFileName);
  224. di.Delete(true);
  225. }
  226. public static async Task<bool> ImgConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists,string pawssword,string imageType=".png", CPDFConvertImgOptions imgOptions = null)
  227. {
  228. bool result = false;
  229. try
  230. {
  231. int[] pageArray = pageIndexLists.ToArray();
  232. imgConverter = new CPDFConverterImg(inputpath, pawssword);
  233. string outputFolder = outputpath;
  234. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  235. ConvertError error = ConvertError.ERR_UNKNOWN;
  236. if (imageType == ".png" || imageType == ".jpg")
  237. {
  238. result = await Task.Run(() => imgConverter.Convert(outputFolder, ref outputFileName, imgOptions, pageArray, ref error, getProgress));
  239. }
  240. else
  241. { //创建缓存文件夹
  242. string folderPath = Path.Combine(App.CurrentPath, "ConverterImg");
  243. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  244. //保险措施(猜测)
  245. if (File.Exists(folderPath))
  246. {
  247. File.Delete(folderPath);
  248. }
  249. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  250. if (!tempfolder.Exists)
  251. {
  252. tempfolder.Create();
  253. }
  254. //预览图缓存
  255. string saveName = Guid.NewGuid().ToString();
  256. string savePath = Path.Combine(folderPath, saveName);
  257. result = await Task.Run(() => imgConverter.Convert(folderPath, ref saveName, imgOptions, pageArray, ref error, getProgress));
  258. ImageMagickPDFToImage(imageType, saveName, outputFolder, outputFileName);
  259. DirectoryInfo di = new DirectoryInfo(folderPath);
  260. di.Delete(true);
  261. }
  262. if (result)
  263. {
  264. Process.Start(outputFolder.Replace("/", "\\"));
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. }
  270. return result;
  271. }
  272. public static async Task<bool> RTFConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertRTFOptions rtfOptions=null)
  273. {
  274. bool result = false;
  275. try
  276. {
  277. int[] pageArray = pageIndexLists.ToArray();
  278. rtfConverter = new CPDFConverterRTF(inputpath, pawssword);
  279. string outputFolder = outputpath;
  280. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  281. ConvertError error = ConvertError.ERR_UNKNOWN;
  282. result = await Task.Run(() => rtfConverter.Convert(outputFolder, ref outputFileName, rtfOptions,pageArray, ref error, getProgress));
  283. if (result)
  284. {
  285. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. }
  291. return result;
  292. }
  293. public static async Task<bool> HTMLConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertHTMLOptions HtmlOption)
  294. {
  295. bool result = false;
  296. try
  297. {
  298. htmlConverter = new CPDFConverterHTML(inputpath, pawssword);
  299. int[] pageArray = pageIndexLists.ToArray();
  300. string outputFolder = outputpath;
  301. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  302. ConvertError error = ConvertError.ERR_UNKNOWN;
  303. result = await Task.Run(() => htmlConverter.Convert(outputFolder, ref outputFileName, HtmlOption, pageArray, ref error, getProgress));
  304. if (result)
  305. {
  306. CommonHelper.ShowFileBrowser(outputFileName.Replace("/", "\\"));
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. }
  312. return result;
  313. }
  314. public static void Clear()
  315. {
  316. if (wordConverter != null)
  317. {
  318. wordConverter.Cancel();
  319. }
  320. if (excelConverter != null)
  321. {
  322. excelConverter.Cancel();
  323. }
  324. if (pptConverter != null)
  325. {
  326. pptConverter.Cancel();
  327. }
  328. if (txtConverter != null)
  329. {
  330. txtConverter.Cancel();
  331. }
  332. if (csvConverter != null)
  333. {
  334. csvConverter.Cancel();
  335. }
  336. if (rtfConverter != null)
  337. {
  338. rtfConverter.Cancel();
  339. }
  340. if (htmlConverter != null)
  341. {
  342. htmlConverter.Cancel();
  343. }
  344. Console.WriteLine("killed");
  345. }
  346. private static void StartFile(string path)
  347. {
  348. //Process.Start(path);
  349. }
  350. public static string GetFileNameAddSuffix(string path, string filename, string suffix)
  351. {
  352. int i = 1;
  353. string outname = filename;
  354. while (File.Exists(path + @"\" + outname + suffix))
  355. {
  356. outname = filename + $"({i.ToString()})";
  357. i++;
  358. }
  359. return outname;
  360. }
  361. }
  362. }