CommonHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Media;
  8. namespace PDF_Office.Helper
  9. {
  10. /// <summary>
  11. /// 1 FindVisualParent 查找目标类型的父类控件
  12. /// 2 FindVisualChild 查找目标类型的子类控件
  13. /// 3 GetPageParmFromList 从页码集合获取页码字符串,如1,2,3 转换成1-3
  14. /// 4 GetPagesInRange 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  15. /// </summary>
  16. public static class CommonHelper
  17. {
  18. /// <summary>
  19. /// 查找对象目标类型的父类控件
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="obj"></param>
  23. /// <returns></returns>
  24. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  25. {
  26. try
  27. {
  28. while (obj != null)
  29. {
  30. if (obj is T)
  31. return obj as T;
  32. obj = VisualTreeHelper.GetParent(obj);
  33. }
  34. return null;
  35. }
  36. catch { return null; }
  37. }
  38. /// <summary>
  39. /// 根据对象查找目标类型的子类
  40. /// </summary>
  41. /// <typeparam name="childItem"></typeparam>
  42. /// <param name="obj"></param>
  43. /// <returns></returns>
  44. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  45. where childItem : DependencyObject
  46. {
  47. try
  48. {
  49. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  50. {
  51. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  52. if (child != null && child is childItem)
  53. return (childItem)child;
  54. else
  55. {
  56. childItem childOfChild = FindVisualChild<childItem>(child);
  57. if (childOfChild != null)
  58. return childOfChild;
  59. }
  60. }
  61. return null;
  62. }
  63. catch { return null; }
  64. }
  65. /// <summary>
  66. /// 从页码集合获取页码字符串,如1,2,3 转换成1-3
  67. /// </summary>
  68. /// <param name="pagesList"></param>
  69. /// <returns></returns>
  70. public static string GetPageParmFromList(List<int> pagesList)
  71. {
  72. string pageParam = "";
  73. if (pagesList.Count != 0)
  74. {
  75. pagesList.Sort();//先对页码排序
  76. for (int i = 0; i < pagesList.Count; i++)
  77. {
  78. if (i == 0)
  79. {
  80. pageParam += pagesList[0].ToString();
  81. }
  82. else
  83. {
  84. if (pagesList[i] == pagesList[i - 1] + 1)//页码连续
  85. {
  86. if (i >= 2)
  87. {
  88. if (pagesList[i - 1] != pagesList[i - 2] + 1)
  89. pageParam += "-";
  90. }
  91. else
  92. pageParam += "-";
  93. if (i == pagesList.Count - 1)
  94. {
  95. pageParam += pagesList[i].ToString();
  96. }
  97. }
  98. else//页码不连续时
  99. {
  100. if (i >= 2)
  101. {
  102. if (pagesList[i - 1] == pagesList[i - 2] + 1)
  103. pageParam += pagesList[i - 1].ToString();
  104. }
  105. pageParam += "," + pagesList[i].ToString();
  106. }
  107. }
  108. }
  109. }
  110. return pageParam;
  111. }
  112. /// <summary>
  113. /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  114. /// </summary>
  115. /// <param name="pageIndexList">返回的页面索引集合</param>
  116. /// <param name="pageRange">需要判断的文本</param>
  117. /// <param name="count">页面总数</param>
  118. /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
  119. /// <param name="rangeSeparator">例 new char[] { '-' }</param>
  120. /// <param name="inittag"></param>
  121. /// <returns></returns>
  122. public static bool GetPagesInRange(ref List<int> pageIndexList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  123. {
  124. string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
  125. pageIndexList.Clear();
  126. foreach (string range in rangeSplit)
  127. {
  128. int starttag = 1;
  129. if (inittag)
  130. {
  131. starttag = 0;
  132. }
  133. if (range.Contains("-"))//连续页
  134. {
  135. try
  136. {
  137. string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
  138. if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
  139. {
  140. int start = int.Parse(limits[0]);
  141. int end = int.Parse(limits[1]);
  142. if ((start < starttag) || (end > count) || (start > end))
  143. {
  144. return false;
  145. }
  146. for (int i = start; i <= end; ++i)
  147. {
  148. if (pageIndexList.Contains(i))
  149. {
  150. return false;
  151. }
  152. pageIndexList.Add(i - 1);
  153. }
  154. continue;
  155. }
  156. }
  157. catch
  158. {
  159. return false;
  160. }
  161. }
  162. int pageNr;
  163. try
  164. {
  165. // Single page
  166. pageNr = int.Parse(range);//单页
  167. }
  168. catch//格式不正确时
  169. {
  170. return false;
  171. }
  172. if (pageNr < starttag || pageNr > count)
  173. {
  174. return false;
  175. }
  176. if (pageIndexList.Contains(pageNr))
  177. {
  178. return false;
  179. }
  180. pageIndexList.Add(pageNr - 1);
  181. }
  182. return true;
  183. }
  184. }
  185. }