ConverterHelper.cs 17 KB

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