PDFToolsHelper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using PDF_Office.Model.PDFTool;
  2. using PDF_Office.Properties;
  3. using PDFSettings;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PDF_Office.Helper
  10. {
  11. /// <summary>
  12. /// 预设需求:远程控制对PDF工具进行排序
  13. /// </summary>
  14. public enum PDFToolType
  15. {
  16. /// <summary>
  17. /// 普通工具
  18. /// </summary>
  19. Normal,
  20. /// <summary>
  21. /// 常用工具
  22. /// </summary>
  23. Common,
  24. /// <summary>
  25. ///付费工具
  26. /// </summary>
  27. Payment,
  28. /// <summary>
  29. /// 优势工具
  30. /// </summary>
  31. Advantage,
  32. /// <summary>
  33. /// 无需打开文档
  34. /// </summary>
  35. NoNeedToOpenDoc
  36. }
  37. public enum PDFFnType
  38. {
  39. Split,
  40. Extract,
  41. Insert,
  42. Compress,
  43. Merge,
  44. Print,
  45. Security,
  46. ConvertPDF,
  47. PDFToWord,
  48. PDFToExcel,
  49. PDFToPPT,
  50. ImageToPDF,
  51. OCR,
  52. WaterMark,
  53. HeaderFooter,
  54. BatesNumbers,
  55. Batch,
  56. Background,
  57. CompareDoc,
  58. Redact,
  59. PageEdit,
  60. BatchRemove,
  61. FormFiledRecognition,
  62. PDFToImage,
  63. }
  64. /// <summary>
  65. /// PDF工具初始化类型
  66. /// </summary>
  67. internal class PDFToolsInit
  68. {
  69. public ToolItem toolItem { get; private set; }
  70. public PDFToolsInit()
  71. {
  72. toolItem = new ToolItem();
  73. }
  74. /// <summary>
  75. /// 图标
  76. /// </summary>
  77. public void SetImagePath(string path)
  78. {
  79. toolItem.FnImg = path;
  80. }
  81. /// <summary>
  82. /// 初始化UI显示内容
  83. /// </summary>
  84. public void SetContent(string fnName,string fnInfo)
  85. {
  86. toolItem.FnName = fnName;
  87. toolItem.FnInfo = fnInfo;
  88. }
  89. /// <summary>
  90. /// 初始化功能
  91. /// </summary>
  92. public void SetFnType(PDFFnType fnType)
  93. {
  94. toolItem.FnType = fnType;
  95. }
  96. }
  97. public class PDFToolsHelper
  98. {
  99. public List<ToolItem> AllTools = null;
  100. public List<ToolItem> QuickTools = null;
  101. public List<ToolItem> MoreTools = null;
  102. private static PDFToolsHelper pDFToolsHelper;
  103. public const int QuickToolCount = 8;//快捷工具显示的个数
  104. public static PDFToolsHelper GetInstance()
  105. {
  106. if (pDFToolsHelper == null)
  107. pDFToolsHelper = new PDFToolsHelper();
  108. return pDFToolsHelper;
  109. }
  110. private PDFToolsHelper()
  111. {
  112. InitPDFTools();
  113. }
  114. private void InitPDFTools()
  115. {
  116. AllTools = new List<ToolItem>();
  117. QuickTools = new List<ToolItem>();
  118. MoreTools = new List<ToolItem>();
  119. //测试代码
  120. //Settings.Default.AllPDFToolsList.Clear();
  121. //Settings.Default.QuickPDFToolsList.Clear();
  122. //Settings.Default.Save();
  123. InitAllTools();
  124. InitQuickTools();
  125. InitMoreTools();
  126. // ContianNewTools();
  127. }
  128. //用来测试的
  129. public void ClearData()
  130. {
  131. Settings.Default.AllPDFToolsList.Clear();
  132. Settings.Default.QuickPDFToolsList.Clear();
  133. Settings.Default.Save();
  134. AllTools.Clear();
  135. QuickTools.Clear();
  136. MoreTools.Clear();
  137. InitAllTools();
  138. InitQuickTools();
  139. InitMoreTools();
  140. }
  141. private void InitAllTools()
  142. {
  143. //批量处理
  144. var batch = new PDFToolsInit();
  145. batch.SetFnType(PDFFnType.Batch);
  146. batch.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch.png");
  147. batch.SetContent("批量处理", "Batch convert, compress, secure, watermark PDFs.");
  148. //OCR
  149. var oCR = new PDFToolsInit();
  150. oCR.SetFnType(PDFFnType.OCR);
  151. oCR.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/ocr.png");
  152. oCR.SetContent("OCR", "Batch convert, compress, secure, watermark PDFs.");
  153. //转档PDF
  154. var convertPDF = new PDFToolsInit();
  155. convertPDF.SetFnType(PDFFnType.ConvertPDF);
  156. convertPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/convert.png");
  157. convertPDF.SetContent("转档PDF", "Batch convert, compress, secure, watermark PDFs.");
  158. //图片转PDF
  159. var imageToPDF = new PDFToolsInit();
  160. imageToPDF.SetFnType(PDFFnType.ImageToPDF);
  161. imageToPDF.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/image.png");
  162. imageToPDF.SetContent("Image To PDF", "Batch convert, compress, secure, watermark PDFs.");
  163. //合并
  164. var merge = new PDFToolsInit();
  165. merge.SetFnType(PDFFnType.Merge);
  166. merge.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/merge.png");
  167. merge.SetContent("Merge", "Batch convert, compress, secure, watermark PDFs.");
  168. //压缩
  169. var compress = new PDFToolsInit();
  170. compress.SetFnType(PDFFnType.Compress);
  171. compress.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
  172. compress.SetContent("Compress", "Batch convert, compress, secure, watermark PDFs.");
  173. //安全
  174. var security = new PDFToolsInit();
  175. security.SetFnType(PDFFnType.Security);
  176. security.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/security.png");
  177. security.SetContent("Security", "Batch convert, compress, secure, watermark PDFs.");
  178. //文件对比
  179. var compareDoc = new PDFToolsInit();
  180. compareDoc.SetFnType(PDFFnType.CompareDoc);
  181. compareDoc.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/compress.png");
  182. compareDoc.SetContent("文件对比", "Batch convert, compress, secure, watermark PDFs.");
  183. //PDF转PPT
  184. var pDFToPPT = new PDFToolsInit();
  185. pDFToPPT.SetFnType(PDFFnType.PDFToPPT);
  186. pDFToPPT.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-ppt.png");
  187. pDFToPPT.SetContent("To PPt", "Batch convert, compress, secure, watermark PDFs.");
  188. //PDF转Excel
  189. var pDFToExcel = new PDFToolsInit();
  190. pDFToExcel.SetFnType(PDFFnType.PDFToExcel);
  191. pDFToExcel.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-excel.png");
  192. pDFToExcel.SetContent("To Excel", "Batch convert, compress, secure, watermark PDFs.");
  193. //PDF转Word
  194. var pDFToWord = new PDFToolsInit();
  195. pDFToWord.SetFnType(PDFFnType.PDFToWord);
  196. pDFToWord.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-word.png");
  197. pDFToWord.SetContent("To Word", "Batch convert, compress, secure, watermark PDFs.");
  198. //PDF转图片
  199. var pDFToImage = new PDFToolsInit();
  200. pDFToImage.SetFnType(PDFFnType.PDFToImage);
  201. pDFToImage.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/pdf-to-image.png");
  202. pDFToImage.SetContent("To Image", "Batch convert, compress, secure, watermark PDFs.");
  203. //水印
  204. var waterMark = new PDFToolsInit();
  205. waterMark.SetFnType(PDFFnType.WaterMark);
  206. waterMark.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/watermark.png");
  207. waterMark.SetContent("水印", "Batch convert, compress, secure, watermark PDFs.");
  208. //背景
  209. var background = new PDFToolsInit();
  210. background.SetFnType(PDFFnType.Background);
  211. background.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/background.png");
  212. background.SetContent("背景", "Batch convert, compress, secure, watermark PDFs.");
  213. //页眉页脚
  214. var headerFooter = new PDFToolsInit();
  215. headerFooter.SetFnType(PDFFnType.HeaderFooter);
  216. headerFooter.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/header&footer.png");
  217. headerFooter.SetContent("页眉页脚", "Batch convert, compress, secure, watermark PDFs.");
  218. //贝茨Bates码
  219. var batesNumbers = new PDFToolsInit();
  220. batesNumbers.SetFnType(PDFFnType.BatesNumbers);
  221. batesNumbers.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batesnumbers.png");
  222. batesNumbers.SetContent("贝茨Bates码", "Batch convert, compress, secure, watermark PDFs.");
  223. //批量移除
  224. var batchRemove = new PDFToolsInit();
  225. batchRemove.SetFnType(PDFFnType.BatchRemove);
  226. batchRemove.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/batch-remove.png");
  227. batchRemove.SetContent("批量移除", "Batch convert, compress, secure, watermark PDFs.");
  228. //编辑
  229. var redact = new PDFToolsInit();
  230. redact.SetFnType(PDFFnType.Redact);
  231. redact.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/reduct.png");
  232. redact.SetContent("编辑", "Batch convert, compress, secure, watermark PDFs.");
  233. //表单存档识别
  234. var formFiledRecognition = new PDFToolsInit();
  235. formFiledRecognition.SetFnType(PDFFnType.FormFiledRecognition);
  236. formFiledRecognition.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/form-filed-recognition.png");
  237. formFiledRecognition.SetContent("表单存档识别", "Batch convert, compress, secure, watermark PDFs.");
  238. //打印
  239. var print = new PDFToolsInit();
  240. print.SetFnType(PDFFnType.Print);
  241. print.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/print.png");
  242. print.SetContent("打印", "Batch convert, compress, secure, watermark PDFs.");
  243. //页面编辑
  244. var pageEdit = new PDFToolsInit();
  245. pageEdit.SetFnType(PDFFnType.PageEdit);
  246. pageEdit.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/page-edit.png");
  247. pageEdit.SetContent("页面编辑", "Batch convert, compress, secure, watermark PDFs.");
  248. //插入
  249. var insert = new PDFToolsInit();
  250. insert.SetFnType(PDFFnType.Insert);
  251. insert.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/insert.png");
  252. insert.SetContent("插入", "Batch convert, compress, secure, watermark PDFs.");
  253. //拆分
  254. var split = new PDFToolsInit();
  255. split.SetFnType(PDFFnType.Split);
  256. split.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/split.png");
  257. split.SetContent("拆分", "Batch convert, compress, secure, watermark PDFs.");
  258. //提取
  259. var extract = new PDFToolsInit();
  260. extract.SetFnType(PDFFnType.Extract);
  261. extract.SetImagePath(@"pack://application:,,,/Resources/HomeIcon/PDFTools/extract.png");
  262. extract.SetContent("提取", "Batch convert, compress, secure, watermark PDFs.");
  263. ///设置快捷工具默认初始顺序
  264. AllTools.Add(pDFToWord.toolItem);
  265. AllTools.Add(pDFToExcel.toolItem);
  266. AllTools.Add(pDFToPPT.toolItem);
  267. AllTools.Add(pDFToImage.toolItem);
  268. AllTools.Add(security.toolItem);
  269. AllTools.Add(imageToPDF.toolItem);
  270. AllTools.Add(merge.toolItem);
  271. AllTools.Add(compress.toolItem);
  272. //更多工具默认初始顺序
  273. //AllTools.Add(split.toolItem);
  274. //AllTools.Add(extract.toolItem);
  275. //AllTools.Add(insert.toolItem);
  276. //AllTools.Add(merge.toolItem);
  277. //AllTools.Add(print.toolItem);
  278. //AllTools.Add(convertPDF.toolItem);
  279. //AllTools.Add(imageToPDF.toolItem);
  280. //AllTools.Add(oCR.toolItem);
  281. //AllTools.Add(waterMark.toolItem);
  282. //AllTools.Add(headerFooter.toolItem);
  283. //AllTools.Add(batesNumbers.toolItem);
  284. //AllTools.Add(batch.toolItem);
  285. //AllTools.Add(background.toolItem);
  286. //AllTools.Add(compareDoc.toolItem);
  287. //AllTools.Add(redact.toolItem);
  288. //AllTools.Add(pageEdit.toolItem);
  289. //AllTools.Add(batchRemove.toolItem);
  290. //AllTools.Add(formFiledRecognition.toolItem);
  291. //AllTools.Add(pDFToImage.toolItem);
  292. }
  293. /// <summary>
  294. /// 检查是否为新增工具
  295. /// </summary>
  296. private void ContianNewTools()
  297. {
  298. var cacheList = Settings.Default.AllPDFToolsList;
  299. foreach (var item in AllTools)
  300. {
  301. ToolItem toolItem = null;
  302. foreach (var cacheItem in cacheList)
  303. {
  304. var strFnTye = Enum.GetName(typeof(PDFFnType), item.FnType);
  305. if (cacheItem.FnTypeStr != strFnTye && cacheItem.IsNewTool == true)
  306. {
  307. toolItem = item;
  308. break;
  309. }
  310. }
  311. if(toolItem != null && toolItem.IsNewTool == true)
  312. {
  313. CacheToolItem cacheTool = new CacheToolItem();
  314. cacheTool.IsNewTool = true;
  315. cacheTool.ToolTypeId = toolItem.ToolTypeId;
  316. cacheTool.ToolTypeStr = Enum.GetName(typeof(PDFToolType), toolItem.ToolType);
  317. cacheTool.FnTypeStr = Enum.GetName(typeof(PDFFnType), toolItem.FnType);
  318. Settings.Default.AllPDFToolsList.Add(cacheTool);
  319. }
  320. }
  321. Settings.Default.Save();
  322. }
  323. /// <summary>
  324. /// 快捷工具
  325. /// </summary>
  326. private void InitQuickTools()
  327. {
  328. //读取缓存的形式
  329. //var cacheList = Settings.Default.AllPDFToolsList;
  330. //if (cacheList == null)
  331. //暂时不用支持记录到缓存,且原方案有一定bug,先采用简单的形式加载处理
  332. var cacheList = new AllPDFToolsList();
  333. if(cacheList.Count > 0)
  334. {
  335. int currentCount = 0;
  336. foreach (var cacheItem in cacheList)
  337. {
  338. if (cacheItem.ToolLayOutType == 1 && currentCount <= QuickToolCount)
  339. {
  340. foreach (var allItem in AllTools)
  341. {
  342. if (cacheItem.FnTypeStr == Enum.GetName(typeof(PDFFnType), allItem.FnType))
  343. {
  344. allItem.IsQuickTool = true;
  345. var index = QuickTools.FindIndex(t => t.ToolTypeId > allItem.ToolTypeId);
  346. if (index == -1)
  347. {
  348. allItem.ToolTypeId = cacheItem.ToolTypeId;
  349. QuickTools.Add(allItem);
  350. }
  351. else
  352. {
  353. allItem.ToolTypeId = index;
  354. QuickTools.Insert(index, allItem);
  355. }
  356. currentCount++;
  357. break;
  358. }
  359. }
  360. }
  361. }
  362. }
  363. else
  364. {
  365. //当所有工具大于等于快捷工具显示的个数时
  366. if (AllTools.Count >= QuickToolCount)
  367. {
  368. for (int i = 0; i < QuickToolCount; i++)
  369. {
  370. AllTools[i].ToolTypeId = i;
  371. AllTools[i].IsQuickTool = true;
  372. QuickTools.Add(AllTools[i]);
  373. }
  374. }
  375. else
  376. {
  377. for (int i = 0; i < AllTools.Count; i++)
  378. {
  379. AllTools[i].ToolTypeId = i;
  380. AllTools[i].IsQuickTool = true;
  381. QuickTools.Add(AllTools[i]);
  382. }
  383. }
  384. SaveAllTools();
  385. }
  386. }
  387. private void SaveAllTools()
  388. {
  389. var cacheList = Settings.Default.AllPDFToolsList;
  390. foreach (var item in AllTools)
  391. {
  392. CacheToolItem cacheItem = new CacheToolItem();
  393. cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
  394. cacheItem.IsNewTool = item.IsNewTool;
  395. cacheItem.ToolTypeId = item.ToolTypeId;
  396. cacheItem.ToolTypeStr = Enum.GetName(typeof(PDFToolType), item.ToolType);
  397. cacheItem.FnTypeStr = Enum.GetName(typeof(PDFFnType), item.FnType);
  398. cacheList.Add(cacheItem);
  399. }
  400. Settings.Default.Save();
  401. }
  402. private void InitMoreTools()
  403. {
  404. int currentCount = 0;
  405. foreach (var allItem in AllTools)
  406. {
  407. if (allItem.IsQuickTool == false)
  408. {
  409. allItem.ToolTypeId = currentCount;
  410. MoreTools.Add(allItem);
  411. currentCount++;
  412. }
  413. }
  414. }
  415. public void SaveCacheList()
  416. {
  417. var cacheLists = Settings.Default.AllPDFToolsList;
  418. CacheToolItem cacheItem = null;
  419. foreach (var item in AllTools)
  420. {
  421. cacheItem = cacheLists.Find(t => t.FnTypeStr == Enum.GetName(typeof(PDFFnType), item.FnType));
  422. if (cacheItem != null)
  423. {
  424. cacheItem.ToolTypeId = item.ToolTypeId;
  425. cacheItem.ToolLayOutType = (item.IsQuickTool == true ? 1 : 0);
  426. }
  427. }
  428. Settings.Default.Save();
  429. }
  430. }
  431. }