ConverterHelper.cs 17 KB

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