ConverterHelper.cs 16 KB

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