CPDFLinkUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using compdfkit_tools.PDFControl;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. namespace compdfkit_tools.Annotation.PDFAnnotationUI
  22. {
  23. /// <summary>
  24. /// CPDFLinkUI.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class CPDFLinkUI : UserControl, INotifyPropertyChanged
  27. {
  28. bool OpenPDF = false;
  29. int totalPage = 0;
  30. int LinkPage = 0;
  31. bool DrawLink = false;
  32. private LinkAnnotArgs LinkAnnot;
  33. private AnnotAttribEvent annotAttribEvent;
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. protected virtual void OnPropertyChanged(string propertyName)
  36. {
  37. if (this.PropertyChanged != null)
  38. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  39. }
  40. private int selectedIndex = 0;
  41. public int SelectedIndex
  42. {
  43. get { return selectedIndex; }
  44. set
  45. {
  46. selectedIndex = value;
  47. CheckingItem(selectedIndex);
  48. OnPropertyChanged("SelectedIndex");
  49. }
  50. }
  51. public CPDFLinkUI()
  52. {
  53. InitializeComponent();
  54. }
  55. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent, int PageCount)
  56. {
  57. UrlText.Text = "";
  58. PageText.Text = "";
  59. EmailText.Text = "";
  60. SaveBtn.IsEnabled = true;
  61. totalPage = PageCount;
  62. if (annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx))
  63. {
  64. int pageNum = (int)annotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1;
  65. if (pageNum > 0 && pageNum <= totalPage)
  66. {
  67. PageText.Text = pageNum.ToString();
  68. SelectedIndex = 1;
  69. }
  70. }
  71. if (annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  72. {
  73. string linkUrl = (string)annotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  74. if (!string.IsNullOrEmpty(linkUrl))
  75. {
  76. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  77. {
  78. EmailText.Text = linkUrl.ToLower().TrimStart("mailto:".ToCharArray());
  79. SelectedIndex = 2;
  80. }
  81. else
  82. {
  83. UrlText.Text = linkUrl;
  84. SelectedIndex = 1;
  85. }
  86. }
  87. }
  88. }
  89. public void InitLinkAnnotArgs(LinkAnnotArgs linkAnnotArgs, int PageCount)
  90. {
  91. LinkAnnot = linkAnnotArgs;
  92. LinkAnnot.LinkDrawFinished += LinkAnnot_LinkDrawFinished;
  93. totalPage = PageCount;
  94. }
  95. private void LinkAnnot_LinkDrawFinished(object sender, bool e)
  96. {
  97. DrawLink = e;
  98. UrlText.Text = "";
  99. PageText.Text = "";
  100. EmailText.Text = "";
  101. }
  102. private void Save_Click(object sender, RoutedEventArgs e)
  103. {
  104. if (LinkAnnot != null)
  105. {
  106. switch (SelectedIndex)
  107. {
  108. case 0:
  109. LinkAnnot.LinkType = LINK_TYPE.URI;
  110. LinkAnnot.URI = "http://" + UrlText.Text.Trim().ToLower();
  111. break;
  112. case 1:
  113. LinkAnnot.LinkType = LINK_TYPE.GOTO;
  114. LinkAnnot.DestIndex = LinkPage - 1;
  115. break;
  116. case 2:
  117. LinkAnnot.LinkType = LINK_TYPE.URI;
  118. LinkAnnot.URI = "mailto:" + EmailText.Text.Trim();
  119. break;
  120. default:
  121. break;
  122. }
  123. LinkAnnot.InvokeLinkSaveCalled(null, null);
  124. }
  125. else
  126. {
  127. switch (SelectedIndex)
  128. {
  129. case 0:
  130. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  131. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "http://" + UrlText.Text.Trim().ToLower());
  132. break;
  133. case 1:
  134. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  135. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkDestIndx, LinkPage - 1);
  136. break;
  137. case 2:
  138. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  139. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + EmailText.Text.Trim());
  140. break;
  141. default:
  142. break;
  143. }
  144. annotAttribEvent.UpdateAnnot();
  145. }
  146. DrawLink = false;
  147. SaveBtn.IsEnabled = false;
  148. }
  149. #region Data Verification
  150. private bool CheckPageNumVaild(out int pageNum, string text)
  151. {
  152. pageNum = -1;
  153. if (string.IsNullOrEmpty(text))
  154. {
  155. return false;
  156. }
  157. if (text.Trim() != string.Empty)
  158. {
  159. if (!int.TryParse(text.Trim(), out pageNum))
  160. {
  161. return false;
  162. }
  163. }
  164. if (pageNum < 1 || pageNum > totalPage)
  165. {
  166. return false;
  167. }
  168. return true;
  169. }
  170. private bool CheckPageWebVaild(string text)
  171. {
  172. if (string.IsNullOrEmpty(text))
  173. {
  174. return false;
  175. }
  176. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  177. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  178. {
  179. return false;
  180. }
  181. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  182. if (matchText.Length != checkUrl.Length)
  183. {
  184. return false;
  185. }
  186. return true;
  187. }
  188. private bool CheckPageMailVaild(string text)
  189. {
  190. if (string.IsNullOrEmpty(text))
  191. {
  192. return false;
  193. }
  194. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  195. {
  196. return false;
  197. }
  198. return true;
  199. }
  200. private void UrlText_TextChanged(object sender, TextChangedEventArgs e)
  201. {
  202. if (LinkAnnot != null)
  203. {
  204. if (CheckPageWebVaild((sender as TextBox).Text) && DrawLink)
  205. {
  206. SaveBtn.IsEnabled = true;
  207. }
  208. else
  209. {
  210. SaveBtn.IsEnabled = false;
  211. }
  212. }
  213. else
  214. {
  215. if (CheckPageWebVaild((sender as TextBox).Text))
  216. {
  217. SaveBtn.IsEnabled = true;
  218. }
  219. else
  220. {
  221. SaveBtn.IsEnabled = false;
  222. }
  223. }
  224. }
  225. private void PageText_TextChanged(object sender, TextChangedEventArgs e)
  226. {
  227. if (LinkAnnot != null)
  228. {
  229. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text) && DrawLink)
  230. {
  231. SaveBtn.IsEnabled = true;
  232. }
  233. else
  234. {
  235. SaveBtn.IsEnabled = false;
  236. }
  237. }
  238. else
  239. {
  240. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text))
  241. {
  242. SaveBtn.IsEnabled = true;
  243. }
  244. else
  245. {
  246. SaveBtn.IsEnabled = false;
  247. }
  248. }
  249. }
  250. private void EmailText_TextChanged(object sender, TextChangedEventArgs e)
  251. {
  252. if (LinkAnnot != null)
  253. {
  254. if (CheckPageMailVaild((sender as TextBox).Text) && DrawLink)
  255. {
  256. SaveBtn.IsEnabled = true;
  257. }
  258. else
  259. {
  260. SaveBtn.IsEnabled = false;
  261. }
  262. }
  263. else
  264. {
  265. if (CheckPageMailVaild((sender as TextBox).Text))
  266. {
  267. SaveBtn.IsEnabled = true;
  268. }
  269. else
  270. {
  271. SaveBtn.IsEnabled = false;
  272. }
  273. }
  274. }
  275. private void CheckingItem(int ItemIndex)
  276. {
  277. bool BtnIsEnabled = false;
  278. if (LinkAnnot != null)
  279. {
  280. switch (ItemIndex)
  281. {
  282. case 0:
  283. BtnIsEnabled = CheckPageWebVaild(UrlText.Text) && DrawLink;
  284. break;
  285. case 1:
  286. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text) && DrawLink;
  287. break;
  288. case 2:
  289. BtnIsEnabled = CheckPageMailVaild(EmailText.Text) && DrawLink;
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. else
  296. {
  297. switch (ItemIndex)
  298. {
  299. case 0:
  300. BtnIsEnabled = CheckPageWebVaild(UrlText.Text);
  301. break;
  302. case 1:
  303. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text);
  304. break;
  305. case 2:
  306. BtnIsEnabled = CheckPageMailVaild(EmailText.Text);
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. SaveBtn.IsEnabled = BtnIsEnabled;
  313. }
  314. #endregion
  315. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  316. {
  317. Binding Indexbinding = new Binding();
  318. Indexbinding.Source = this;
  319. Indexbinding.Path = new System.Windows.PropertyPath("SelectedIndex");
  320. HeadTabControl.SetBinding(TabControl.SelectedIndexProperty, Indexbinding);
  321. }
  322. private void PART_BtnClear_Click(object sender, RoutedEventArgs e)
  323. {
  324. switch (SelectedIndex)
  325. {
  326. case 0:
  327. UrlText.Text = "";
  328. break;
  329. case 1:
  330. PageText.Text = "";
  331. break;
  332. case 2:
  333. EmailText.Text = "";
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. }
  340. }