CommonHelper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Media;
  10. namespace PDF_Office.Helper
  11. {
  12. /// <summary>
  13. /// 1 FindVisualParent 查找目标类型的父类控件
  14. /// 2 FindVisualChild 查找目标类型的子类控件
  15. /// 3 GetPageParmFromList 从页码集合获取页码字符串,如1,2,3 转换成1-3
  16. /// 4 GetPagesInRange 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  17. /// 5.ShowFileBrowser 显示系统文件浏览器,可以根据传入的路径参数,自动选中对应的文件
  18. /// 6.CreateFilePath 检查对应路径是否有重名,有重名的情况追加尾号
  19. /// 7.CreateFolder 检查对应文件夹是否有重名,有重名的情况追加尾号
  20. /// </summary>
  21. public static class CommonHelper
  22. {
  23. /// <summary>
  24. /// 查找对象目标类型的父类控件
  25. /// </summary>
  26. /// <typeparam name="T"></typeparam>
  27. /// <param name="obj"></param>
  28. /// <returns></returns>
  29. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  30. {
  31. try
  32. {
  33. while (obj != null)
  34. {
  35. if (obj is T)
  36. return obj as T;
  37. obj = VisualTreeHelper.GetParent(obj);
  38. }
  39. return null;
  40. }
  41. catch { return null; }
  42. }
  43. /// <summary>
  44. /// 根据对象查找目标类型的子类
  45. /// </summary>
  46. /// <typeparam name="childItem"></typeparam>
  47. /// <param name="obj"></param>
  48. /// <returns></returns>
  49. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  50. where childItem : DependencyObject
  51. {
  52. try
  53. {
  54. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  55. {
  56. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  57. if (child != null && child is childItem)
  58. return (childItem)child;
  59. else
  60. {
  61. childItem childOfChild = FindVisualChild<childItem>(child);
  62. if (childOfChild != null)
  63. return childOfChild;
  64. }
  65. }
  66. return null;
  67. }
  68. catch { return null; }
  69. }
  70. /// <summary>
  71. /// 从页码集合获取页码字符串,如1,2,3 转换成1-3
  72. /// </summary>
  73. /// <param name="pagesList"></param>
  74. /// <returns></returns>
  75. public static string GetPageParmFromList(List<int> pagesList)
  76. {
  77. string pageParam = "";
  78. if (pagesList.Count != 0)
  79. {
  80. pagesList.Sort();//先对页码排序
  81. for (int i = 0; i < pagesList.Count; i++)
  82. {
  83. if (i == 0)
  84. {
  85. pageParam += pagesList[0].ToString();
  86. }
  87. else
  88. {
  89. if (pagesList[i] == pagesList[i - 1] + 1)//页码连续
  90. {
  91. if (i >= 2)
  92. {
  93. if (pagesList[i - 1] != pagesList[i - 2] + 1)
  94. pageParam += "-";
  95. }
  96. else
  97. pageParam += "-";
  98. if (i == pagesList.Count - 1)
  99. {
  100. pageParam += pagesList[i].ToString();
  101. }
  102. }
  103. else//页码不连续时
  104. {
  105. if (i >= 2)
  106. {
  107. if (pagesList[i - 1] == pagesList[i - 2] + 1)
  108. pageParam += pagesList[i - 1].ToString();
  109. }
  110. pageParam += "," + pagesList[i].ToString();
  111. }
  112. }
  113. }
  114. }
  115. return pageParam;
  116. }
  117. /// <summary>
  118. /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  119. /// </summary>
  120. /// <param name="pageIndexList">返回的页面索引集合</param>
  121. /// <param name="pageRange">需要判断的文本</param>
  122. /// <param name="count">页面总数</param>
  123. /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
  124. /// <param name="rangeSeparator">例 new char[] { '-' }</param>
  125. /// <param name="inittag"></param>
  126. /// <returns></returns>
  127. public static bool GetPagesInRange(ref List<int> pageIndexList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  128. {
  129. string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
  130. pageIndexList.Clear();
  131. foreach (string range in rangeSplit)
  132. {
  133. int starttag = 1;
  134. if (inittag)
  135. {
  136. starttag = 0;
  137. }
  138. if (range.Contains("-"))//连续页
  139. {
  140. try
  141. {
  142. string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
  143. if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
  144. {
  145. int start = int.Parse(limits[0]);
  146. int end = int.Parse(limits[1]);
  147. if ((start < starttag) || (end > count) || (start > end))
  148. {
  149. return false;
  150. }
  151. for (int i = start; i <= end; ++i)
  152. {
  153. if (pageIndexList.Contains(i))
  154. {
  155. return false;
  156. }
  157. pageIndexList.Add(i - 1);
  158. }
  159. continue;
  160. }
  161. }
  162. catch
  163. {
  164. return false;
  165. }
  166. }
  167. int pageNr;
  168. try
  169. {
  170. // Single page
  171. pageNr = int.Parse(range);//单页
  172. }
  173. catch//格式不正确时
  174. {
  175. return false;
  176. }
  177. if (pageNr < starttag || pageNr > count)
  178. {
  179. return false;
  180. }
  181. if (pageIndexList.Contains(pageNr))
  182. {
  183. return false;
  184. }
  185. pageIndexList.Add(pageNr - 1);
  186. }
  187. return true;
  188. }
  189. /// <summary>
  190. /// 显示系统文件浏览器
  191. /// </summary>
  192. /// <param name="selectedFile">要选中的文件路径</param>
  193. public static void ShowFileBrowser(string selectedFile=null)
  194. {
  195. if(string.IsNullOrEmpty(selectedFile))
  196. {
  197. Process.Start(@"explorer.exe");
  198. }
  199. else
  200. {
  201. Process.Start(@"explorer.exe", "/select,\"" + selectedFile + "\"");
  202. }
  203. }
  204. /// <summary>
  205. /// 检测文件是否重复 追加尾号
  206. /// </summary>
  207. /// <param name="path"></param>
  208. /// <returns></returns>
  209. public static string CreateFilePath(string path)
  210. {
  211. int i = 1;
  212. string oldDestName = path;
  213. do
  214. {
  215. if (File.Exists(path))
  216. {
  217. int lastDot = oldDestName.LastIndexOf('.');
  218. string fileExtension = string.Empty;
  219. string fileName = oldDestName;
  220. if (lastDot > 0)
  221. {
  222. fileExtension = fileName.Substring(lastDot);
  223. fileName = fileName.Substring(0, lastDot);
  224. }
  225. path = fileName + string.Format(@"({0})", i) + fileExtension;
  226. }
  227. ++i;
  228. } while (File.Exists(path));
  229. return path;
  230. }
  231. /// <summary>
  232. /// 检查文件夹是否重复 追加尾号
  233. /// </summary>
  234. /// <param name="path"></param>
  235. /// <returns></returns>
  236. public static string CreateFolder(string folder)
  237. {
  238. int i = 1;
  239. string oldDestName = folder;
  240. DirectoryInfo info = new DirectoryInfo(folder);
  241. do
  242. {
  243. info = new DirectoryInfo(folder);
  244. if (info.Exists)
  245. {
  246. string fileName = oldDestName;
  247. folder = fileName + string.Format(@"({0})", i);
  248. }
  249. ++i;
  250. } while (info.Exists);
  251. info.Create();
  252. return folder;
  253. }
  254. }
  255. }