CommonHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using PDF_Office.Model.PageEdit;
  2. using PDF_Office.ViewModels.Dialog.BOTA;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Media;
  13. namespace PDF_Office.Helper
  14. {
  15. public class Compare<T, C> : IEqualityComparer<T>
  16. {
  17. private Func<T, C> _getField;
  18. public Compare(Func<T, C> getfield)
  19. {
  20. this._getField = getfield;
  21. }
  22. public bool Equals(T x, T y)
  23. {
  24. return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
  25. }
  26. public int GetHashCode(T obj)
  27. {
  28. return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
  29. }
  30. }
  31. /// <summary>
  32. /// 1 FindVisualParent 查找目标类型的父类控件
  33. /// 2 FindVisualChild 查找目标类型的子类控件
  34. /// 3 GetPageParmFromList 从页码集合获取页码字符串,如1,2,3 转换成1-3
  35. /// 4 GetPagesInRange 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  36. /// 5.ShowFileBrowser 显示系统文件浏览器,可以根据传入的路径参数,自动选中对应的文件
  37. /// 6.CreateFilePath 检查对应路径是否有重名,有重名的情况追加尾号
  38. /// 7.CreateFolder 检查对应文件夹是否有重名,有重名的情况追加尾号
  39. /// 8.GetUnitsFromPageSize 将PDF页面宽高转换成对应的单位
  40. /// 9.GetDpi() 返回当前设备DPI
  41. /// </summary>
  42. public static class CommonHelper
  43. {
  44. /// <summary>
  45. /// 自定义Distinct扩展方法
  46. /// </summary>
  47. /// <typeparam name="T">要去重的对象类</typeparam>
  48. /// <typeparam name="C">自定义去重的字段类型</typeparam>
  49. /// <param name="source">要去重的对象</param>
  50. /// <param name="getfield">获取自定义去重字段的委托</param>
  51. /// <returns></returns>
  52. public static IEnumerable<T> DistinctHelper<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
  53. {
  54. return source.Distinct(new Compare<T, C>(getfield));
  55. }
  56. /// <summary>
  57. /// 查找对象目标类型的父类控件
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <param name="obj"></param>
  61. /// <returns></returns>
  62. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  63. {
  64. try
  65. {
  66. while (obj != null)
  67. {
  68. if (obj is T)
  69. return obj as T;
  70. obj = VisualTreeHelper.GetParent(obj);
  71. }
  72. return null;
  73. }
  74. catch { return null; }
  75. }
  76. /// <summary>
  77. /// 根据对象查找目标类型的子类
  78. /// </summary>
  79. /// <typeparam name="childItem"></typeparam>
  80. /// <param name="obj"></param>
  81. /// <returns></returns>
  82. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  83. where childItem : DependencyObject
  84. {
  85. try
  86. {
  87. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  88. {
  89. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  90. if (child != null && child is childItem)
  91. return (childItem)child;
  92. else
  93. {
  94. childItem childOfChild = FindVisualChild<childItem>(child);
  95. if (childOfChild != null)
  96. return childOfChild;
  97. }
  98. }
  99. return null;
  100. }
  101. catch { return null; }
  102. }
  103. /// <summary>
  104. /// 从页码集合获取页码字符串,如1,2,3 转换成1-3
  105. /// </summary>
  106. /// <param name="pagesList"></param>
  107. /// <returns></returns>
  108. public static string GetPageParmFromList(List<int> pagesList)
  109. {
  110. string pageParam = "";
  111. if (pagesList.Count != 0)
  112. {
  113. pagesList.Sort();//先对页码排序
  114. for (int i = 0; i < pagesList.Count; i++)
  115. {
  116. if (i == 0)
  117. {
  118. pageParam += pagesList[0].ToString();
  119. }
  120. else
  121. {
  122. if (pagesList[i] == pagesList[i - 1] + 1)//页码连续
  123. {
  124. if (i >= 2)
  125. {
  126. if (pagesList[i - 1] != pagesList[i - 2] + 1)
  127. pageParam += "-";
  128. }
  129. else
  130. pageParam += "-";
  131. if (i == pagesList.Count - 1)
  132. {
  133. pageParam += pagesList[i].ToString();
  134. }
  135. }
  136. else//页码不连续时
  137. {
  138. if (i >= 2)
  139. {
  140. if (pagesList[i - 1] == pagesList[i - 2] + 1)
  141. pageParam += pagesList[i - 1].ToString();
  142. }
  143. pageParam += "," + pagesList[i].ToString();
  144. }
  145. }
  146. }
  147. }
  148. return pageParam;
  149. }
  150. /// <summary>
  151. /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  152. /// </summary>
  153. /// <param name="pageIndexList">返回的页面索引集合</param>
  154. /// <param name="pageRange">需要判断的文本</param>
  155. /// <param name="count">页面总数</param>
  156. /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
  157. /// <param name="rangeSeparator">例 new char[] { '-' }</param>
  158. /// <param name="inittag"></param>
  159. /// <returns></returns>
  160. public static bool GetPagesInRange(ref List<int> pageIndexList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  161. {
  162. string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
  163. pageIndexList.Clear();
  164. foreach (string range in rangeSplit)
  165. {
  166. int starttag = 1;
  167. if (inittag)
  168. {
  169. starttag = 0;
  170. }
  171. if (range.Contains("-"))//连续页
  172. {
  173. try
  174. {
  175. string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
  176. if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
  177. {
  178. int start = int.Parse(limits[0]);
  179. int end = int.Parse(limits[1]);
  180. if ((start < starttag) || (end > count) || (start > end))
  181. {
  182. return false;
  183. }
  184. for (int i = start; i <= end; ++i)
  185. {
  186. if (pageIndexList.Contains(i))
  187. {
  188. return false;
  189. }
  190. pageIndexList.Add(i - 1);
  191. }
  192. continue;
  193. }
  194. }
  195. catch
  196. {
  197. return false;
  198. }
  199. }
  200. int pageNr;
  201. try
  202. {
  203. // Single page
  204. pageNr = int.Parse(range);//单页
  205. }
  206. catch//格式不正确时
  207. {
  208. return false;
  209. }
  210. if (pageNr < starttag || pageNr > count)
  211. {
  212. return false;
  213. }
  214. if (pageIndexList.Contains(pageNr))
  215. {
  216. return false;
  217. }
  218. pageIndexList.Add(pageNr - 1);
  219. }
  220. return true;
  221. }
  222. /// <summary>
  223. /// 显示系统文件浏览器
  224. /// </summary>
  225. /// <param name="selectedFile">要选中的文件路径</param>
  226. public static void ShowFileBrowser(string selectedFile = null)
  227. {
  228. if (string.IsNullOrEmpty(selectedFile))
  229. {
  230. Process.Start(@"explorer.exe");
  231. }
  232. else
  233. {
  234. Process.Start(@"explorer.exe", "/select,\"" + selectedFile + "\"");
  235. }
  236. }
  237. /// <summary>
  238. /// 检测文件是否重复 追加尾号
  239. /// </summary>
  240. /// <param name="path"></param>
  241. /// <returns></returns>
  242. public static string CreateFilePath(string path)
  243. {
  244. int i = 1;
  245. string oldDestName = path;
  246. do
  247. {
  248. if (File.Exists(path))
  249. {
  250. int lastDot = oldDestName.LastIndexOf('.');
  251. string fileExtension = string.Empty;
  252. string fileName = oldDestName;
  253. if (lastDot > 0)
  254. {
  255. fileExtension = fileName.Substring(lastDot);
  256. fileName = fileName.Substring(0, lastDot);
  257. }
  258. path = fileName + string.Format(@"({0})", i) + fileExtension;
  259. }
  260. ++i;
  261. } while (File.Exists(path));
  262. return path;
  263. }
  264. /// <summary>
  265. /// 检查文件夹是否重复 追加尾号
  266. /// </summary>
  267. /// <param name="path"></param>
  268. /// <returns></returns>
  269. public static string CreateFolder(string folder)
  270. {
  271. int i = 1;
  272. string oldDestName = folder;
  273. DirectoryInfo info = new DirectoryInfo(folder);
  274. do
  275. {
  276. info = new DirectoryInfo(folder);
  277. if (info.Exists)
  278. {
  279. string fileName = oldDestName;
  280. folder = fileName + string.Format(@"({0})", i);
  281. }
  282. ++i;
  283. } while (info.Exists);
  284. info.Create();
  285. return folder;
  286. }
  287. /// <summary>
  288. /// 将Document 返回的PageSize 转换成对应单位
  289. /// </summary>
  290. /// <param name="size"></param>
  291. /// <param name="unit"></param>
  292. /// <returns></returns>
  293. public static double GetUnitsFromPageSize(double size, PageItemUnits unit = PageItemUnits.MM)
  294. {
  295. double sizeWithUnit = 0;
  296. switch (unit)
  297. {
  298. case PageItemUnits.MM:
  299. sizeWithUnit = (size * 96 / 72.0) / GetDpi() * 25.4;
  300. break;
  301. case PageItemUnits.CM:
  302. sizeWithUnit = (size * 96 / 72.0) / GetDpi() * 25.4 / 10.0;
  303. break;
  304. case PageItemUnits.IN:
  305. sizeWithUnit = (size * 96 / 72.0) / GetDpi();
  306. break;
  307. default:
  308. break;
  309. }
  310. return sizeWithUnit;
  311. }
  312. /// <summary>
  313. /// 返回设备DPI
  314. /// </summary>
  315. /// <returns></returns>
  316. public static double GetDpi()
  317. {
  318. BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.NonPublic;
  319. PropertyInfo property = typeof(SystemParameters).GetProperty("Dpi", bindingAttr);
  320. return (int)property.GetValue(null, null);
  321. }
  322. }
  323. }