CommonHelper.cs 11 KB

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