HomePageEditHelper.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using ComPDFKitViewer.PdfViewer;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Documents;
  10. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  11. using ComPDFKit.PDFDocument;
  12. using PDF_Master.Model.EditTools.Bates;
  13. namespace PDF_Master.Helper
  14. {
  15. public static class HomePageEditHelper
  16. {
  17. /// <summary>
  18. /// 获取所需要分文档份数
  19. /// </summary>
  20. /// <param name="pagecontent">文档总页数</param>
  21. /// <param name="averagepage">平均几页分为一个文档</param>
  22. /// <param name="remainder">最后文档的页数的引用 例如11页文档每3页为一份PDF,3,3,3,2,remainder=2</param>
  23. /// <returns></returns>
  24. public static int AverageSplitPages(int pagecontent, int averagepage, ref int remainder)
  25. {
  26. if (pagecontent <= 0)
  27. {
  28. remainder = 0;
  29. return 0;
  30. }
  31. if (pagecontent <= averagepage)
  32. {
  33. remainder = pagecontent;
  34. return 1;
  35. }
  36. if (pagecontent > averagepage)
  37. {
  38. if (pagecontent % averagepage == 0)
  39. {
  40. remainder = 0;
  41. return pagecontent / averagepage;
  42. }
  43. remainder = pagecontent % averagepage;
  44. return (pagecontent / averagepage) + 1;
  45. }
  46. remainder = 0;
  47. return -1;
  48. }
  49. /// <summary>
  50. /// 获取平均每个文档的页数
  51. /// </summary>
  52. /// <param name="pagecontent">文档总页数</param>
  53. /// <param name="averagedoc">平均几页分为一个文档</param>
  54. /// <param name="remainder">引用,截至第几位文档比后面文档多一 例如11页文档 分为3份 4,4,3 remainder为2 第一个文档和第二文档比后续文档多一</param>
  55. /// <returns></returns>
  56. public static int AverageSplitDocs(int pagecontent, int averagedoc, ref int remainder)
  57. {
  58. if (pagecontent <= 0)
  59. {
  60. remainder = 0;
  61. return 0;
  62. }
  63. if (pagecontent <= averagedoc)
  64. {
  65. remainder = 0;
  66. return 1;
  67. }
  68. if (pagecontent > averagedoc)
  69. {
  70. remainder = pagecontent % averagedoc;
  71. return pagecontent / averagedoc;
  72. }
  73. return 0;
  74. }
  75. public static void GetPagerange(string PageRangeSelectIndex, CPDFViewer currentViewer, ref string pageRange, string pageRangeText, bool currentpage = false, string currentPageRange="1")
  76. {
  77. if (!currentpage)
  78. {
  79. if (PageRangeSelectIndex == "0")
  80. {
  81. pageRange = "1-" + currentViewer.Document.PageCount.ToString();
  82. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  83. }
  84. else if (PageRangeSelectIndex == "1")
  85. {
  86. pageRange = "1";
  87. for (int i = 3; i <= currentViewer.Document.PageCount; i += 2)
  88. pageRange = pageRange + "," + i;
  89. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  90. }
  91. else if (PageRangeSelectIndex == "2")
  92. {
  93. pageRange = "2";
  94. for (int i = 4; i <= currentViewer.Document.PageCount; i += 2)
  95. pageRange = pageRange + "," + i;
  96. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  97. }
  98. else if (PageRangeSelectIndex == "3")
  99. {
  100. if (pageRangeText != "")
  101. {
  102. pageRange = pageRangeText;
  103. }
  104. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  105. }
  106. else { return; }
  107. }
  108. else
  109. {
  110. if (PageRangeSelectIndex == "0")
  111. {
  112. pageRange = "1-" + currentViewer.Document.PageCount.ToString();
  113. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  114. }
  115. if (PageRangeSelectIndex == "1")
  116. {
  117. pageRange = currentPageRange;
  118. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  119. }
  120. else if (PageRangeSelectIndex == "2")
  121. {
  122. pageRange = "1";
  123. for (int i = 3; i <= currentViewer.Document.PageCount; i += 2)
  124. pageRange = pageRange + "," + i;
  125. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  126. }
  127. else if (PageRangeSelectIndex == "3")
  128. {
  129. pageRange = "2";
  130. for (int i = 4; i <= currentViewer.Document.PageCount; i += 2)
  131. pageRange = pageRange + "," + i;
  132. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  133. }
  134. else if (PageRangeSelectIndex == "4")
  135. {
  136. if (pageRangeText != "")
  137. {
  138. pageRange = pageRangeText;
  139. }
  140. Trace.WriteLine(" extractModel.PageRange" + pageRange);
  141. }
  142. else { return; }
  143. }
  144. }
  145. public static string UpDataPageRange(int pagecount,string pagerange) {
  146. List<int> PageIndexLists = new List<int>();
  147. string str = "";
  148. if(CommonHelper.GetPagesInRange(ref PageIndexLists, pagerange, 9999, new char[] { ',' }, new char[] { '-' }))
  149. {
  150. foreach (var PageIndex in PageIndexLists) {
  151. int pageindex = PageIndex + 1;
  152. if (pageindex<= pagecount)
  153. {
  154. str = str + pageindex.ToString();
  155. str = str + ",";
  156. }
  157. }
  158. if(str=="")
  159. return str;
  160. return str.Remove(str.LastIndexOf(","),1);
  161. }
  162. return str;
  163. }
  164. public static string ReverseDate(string str,CPDFDocument Document)
  165. {
  166. string yyyy = DateTime.Now.ToString("yyyy");
  167. string yy = DateTime.Now.ToString("yy");
  168. string mm = DateTime.Now.ToString("MM");
  169. string m = DateTime.Now.ToString("%M");
  170. string dd = DateTime.Now.ToString("dd");
  171. string d = DateTime.Now.ToString("%d");
  172. str = str.Replace("<<m/d>>", m + "/" + d)
  173. .Replace("<<m/d/yy>>", m + "/" + d + "/" + yy)
  174. .Replace("<<m/d/yyyy>>", m + "/" + d + "/" + yyyy)
  175. .Replace("<<mm/dd/yy>>", mm + "/" + dd + "/" + yy)
  176. .Replace("<<mm/dd/yyyy>>", mm + "/" + dd + "/" + yyyy)
  177. .Replace("<<d/m/yy>>", d + "/" + m + "/" + yy)
  178. .Replace("<<d/m/yyyy>>", d + "/" + m + "/" + yyyy)
  179. .Replace("<<dd/mm/yy>>", dd + "/" + mm + "/" + yy)
  180. .Replace("<<dd/mm/yyyy>>", dd + "/" + mm + "/" + yyyy)
  181. .Replace("<<mm/yy>>", mm + "/" + yy)
  182. .Replace("<<mm/yyyy>>", mm + "/" + yyyy)
  183. .Replace("<<m.d.yy>>", m + "." + d + "." + yy)
  184. .Replace("<<m.d.yyyy>>", m + "." + d + "." + yyyy)
  185. .Replace("<<mm.dd.yy>>", mm + "." + dd + "." + yy)
  186. .Replace("<<mm.dd.yyyy>>", mm + "." + dd + "." + yyyy)
  187. .Replace("<<mm.yy>>", mm + "." + yy)
  188. .Replace("<<mm.yyyy>>", mm + "." + yyyy)
  189. .Replace("<<d.m.yy>>", d + "." + m + "." + yy)
  190. .Replace("<<d.m.yyyy>>", d + "." + m + "." + yyyy)
  191. .Replace("<<dd.mm.yy>>", dd + "." + mm + "." + yy)
  192. .Replace("<<dd.mm.yyyy>>", dd + "." + mm + "." + yyyy)
  193. .Replace("<<yy-mm-dd>>", yy + "-" + mm + "-" + dd)
  194. .Replace("<<yyyy-mm-dd>>", yyyy + "-" + mm + "-" + dd)
  195. .Replace("<<1 of n>>", "<<1>>" + " of " + Document.PageCount)
  196. .Replace("<<1/n>>", "<<1>>" + "/" + Document.PageCount)
  197. .Replace("<<Page 1>>", "Page" + "<<1>>")
  198. .Replace("<<Page 1 of n>>", "Page " + "<<1>>" + " of " + Document.PageCount);
  199. return str;
  200. }
  201. }
  202. }