ConverterHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. //pdf整体转为gif,将.gifoff改为.gif
  155. Trace.WriteLine(file);
  156. if (imageType == ".gifoff") {
  157. images.Add(file);
  158. images[i].AnimationDelay = 100; // in this example delay is 1000ms/1sec
  159. images[i].Flip();
  160. }
  161. using (var image = new MagickImage(file))
  162. {
  163. if (imageType != ".gifoff")
  164. {
  165. //Save frame as jpg
  166. //image.Format = MagickFormat.Jp2;
  167. image.Write(file.Remove(file.LastIndexOf(".png"), 4) + imageType);
  168. }
  169. // 删除该文件
  170. System.IO.File.Delete(file);
  171. }
  172. }
  173. if (imageType == ".gifoff")
  174. {
  175. // Optionally reduce colors
  176. var settings = new QuantizeSettings();
  177. settings.Colors = 256;
  178. images.Quantize(settings);
  179. // Optionally optimize the images (images should have the same size).
  180. images.Optimize();
  181. // Save gif
  182. int fileIndex = 1;
  183. if (File.Exists(outputFolder + "\\" + outputFileName + ".gif"))
  184. {
  185. while (fileIndex >= 1)
  186. {
  187. if (!File.Exists(outputFolder + "\\" + outputFileName + "_" + fileIndex + ".gif"))
  188. {
  189. images.Write(outputFolder + "\\" + outputFileName + ".gif");
  190. fileIndex = -1;
  191. }
  192. fileIndex++;
  193. }
  194. }
  195. else { images.Write(outputFolder + "\\" + outputFileName + ".gif"); }
  196. }
  197. else {
  198. int fileIndex = 1;
  199. if (File.Exists(outputFolder + "\\" + outputFileName + ".zip"))
  200. {
  201. while (fileIndex>=1) {
  202. if (!File.Exists(outputFolder + "\\" + outputFileName + "_" + fileIndex + ".zip"))
  203. {
  204. ZipFile.CreateFromDirectory(outputFolder + "\\" + outputFileName, outputFolder + "\\" + outputFileName + "_" + fileIndex + ".zip");
  205. fileIndex=-1;
  206. }
  207. fileIndex++;
  208. }
  209. }
  210. else { ZipFile.CreateFromDirectory(outputFolder + "\\" + outputFileName, outputFolder + "\\" + outputFileName + ".zip"); }
  211. }
  212. DirectoryInfo di = new DirectoryInfo(outputFolder + "\\" + outputFileName);
  213. di.Delete(true);
  214. }
  215. public static async Task<bool> ImgConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists,string pawssword, ImgType type,string imageType=".png")
  216. {
  217. bool result = false;
  218. try
  219. {
  220. int[] pageArray = pageIndexLists.ToArray();
  221. imgConverter = new CPDFConverterImg(inputpath, pawssword);
  222. string outputFolder = outputpath;
  223. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  224. ConvertError error = ConvertError.ERR_UNKNOWN;
  225. if (imageType == ".png" || imageType == ".jpg")
  226. {
  227. result = await Task.Run(() => imgConverter.Convert(outputFolder, ref outputFileName, pageArray, type, ref error, getProgress));
  228. }
  229. else
  230. { //创建缓存文件夹
  231. string folderPath = Path.Combine(App.CurrentPath, "ConverterImg");
  232. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  233. //保险措施(猜测)
  234. if (File.Exists(folderPath))
  235. {
  236. File.Delete(folderPath);
  237. }
  238. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  239. if (!tempfolder.Exists)
  240. {
  241. tempfolder.Create();
  242. }
  243. //预览图缓存
  244. string saveName = Guid.NewGuid().ToString();
  245. string savePath = Path.Combine(folderPath, saveName);
  246. result = await Task.Run(() => imgConverter.Convert(folderPath, ref saveName, pageArray, type, ref error, getProgress));
  247. ImageMagickPDFToImage(imageType, saveName, outputFolder, outputFileName);
  248. DirectoryInfo di = new DirectoryInfo(folderPath);
  249. di.Delete(true);
  250. }
  251. if (result)
  252. {
  253. if (File.Exists(outputFileName))
  254. Process.Start(outputFileName);
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. }
  260. return result;
  261. }
  262. public static async Task<bool> RTFConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword)
  263. {
  264. bool result = false;
  265. try
  266. {
  267. int[] pageArray = pageIndexLists.ToArray();
  268. rtfConverter = new CPDFConverterRTF(inputpath, pawssword);
  269. string outputFolder = outputpath;
  270. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  271. ConvertError error = ConvertError.ERR_UNKNOWN;
  272. result = await Task.Run(() => rtfConverter.Convert(outputFolder, ref outputFileName, pageArray, ref error, getProgress));
  273. }
  274. catch (Exception ex)
  275. {
  276. }
  277. return result;
  278. }
  279. public static async Task<bool> HTMLConvert(string inputpath, string outputpath, OnProgress getProgress, List<int> pageIndexLists, string pawssword, CPDFConvertHTMLOptions HtmlOption)
  280. {
  281. bool result = false;
  282. try
  283. {
  284. htmlConverter = new CPDFConverterHTML(inputpath, pawssword);
  285. int[] pageArray = pageIndexLists.ToArray();
  286. string outputFolder = outputpath;
  287. string outputFileName = Path.GetFileNameWithoutExtension(inputpath);
  288. ConvertError error = ConvertError.ERR_UNKNOWN;
  289. result = await Task.Run(() => htmlConverter.Convert(outputFolder, ref outputFileName, HtmlOption, pageArray, ref error, getProgress));
  290. if (result)
  291. {
  292. }
  293. }
  294. catch (Exception ex)
  295. {
  296. }
  297. return result;
  298. }
  299. public static void Clear()
  300. {
  301. if (wordConverter != null)
  302. {
  303. wordConverter.Cancel();
  304. }
  305. if (excelConverter != null)
  306. {
  307. excelConverter.Cancel();
  308. }
  309. if (pptConverter != null)
  310. {
  311. pptConverter.Cancel();
  312. }
  313. if (txtConverter != null)
  314. {
  315. txtConverter.Cancel();
  316. }
  317. if (csvConverter != null)
  318. {
  319. csvConverter.Cancel();
  320. }
  321. if (rtfConverter != null)
  322. {
  323. rtfConverter.Cancel();
  324. }
  325. if (htmlConverter != null)
  326. {
  327. htmlConverter.Cancel();
  328. }
  329. Console.WriteLine("killed");
  330. }
  331. private static void StartFile(string path)
  332. {
  333. //Process.Start(path);
  334. }
  335. public static string GetFileNameAddSuffix(string path, string filename, string suffix)
  336. {
  337. int i = 1;
  338. string outname = filename;
  339. while (File.Exists(path + @"\" + outname + suffix))
  340. {
  341. outname = filename + $"({i.ToString()})";
  342. i++;
  343. }
  344. return outname;
  345. }
  346. }
  347. }