CPDFLinkUI.xaml.cs 13 KB

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