CPDFLinkUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 AttribEvent, int PageCount)
  56. {
  57. annotAttribEvent = AttribEvent;
  58. UrlText.Text = "";
  59. PageText.Text = "";
  60. EmailText.Text = "";
  61. SaveBtn.IsEnabled = true;
  62. totalPage = PageCount;
  63. if (AttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx))
  64. {
  65. int pageNum = (int)AttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1;
  66. if (pageNum > 0 && pageNum <= totalPage)
  67. {
  68. PageText.Text = pageNum.ToString();
  69. SelectedIndex = 1;
  70. }
  71. }
  72. if (AttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  73. {
  74. string linkUrl = (string)AttribEvent.Attribs[AnnotAttrib.LinkUri];
  75. if (!string.IsNullOrEmpty(linkUrl))
  76. {
  77. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  78. {
  79. EmailText.Text = linkUrl.ToLower().TrimStart("mailto:".ToCharArray());
  80. SelectedIndex = 2;
  81. }
  82. else
  83. {
  84. UrlText.Text = linkUrl;
  85. SelectedIndex = 0;
  86. }
  87. }
  88. }
  89. }
  90. public void InitLinkAnnotArgs(LinkAnnotArgs linkAnnotArgs, int PageCount)
  91. {
  92. LinkAnnot = linkAnnotArgs;
  93. LinkAnnot.LinkDrawFinished += LinkAnnot_LinkDrawFinished;
  94. totalPage = PageCount;
  95. }
  96. private void LinkAnnot_LinkDrawFinished(object sender, bool e)
  97. {
  98. DrawLink = e;
  99. UrlText.Text = "";
  100. PageText.Text = "";
  101. EmailText.Text = "";
  102. }
  103. private void Save_Click(object sender, RoutedEventArgs e)
  104. {
  105. if (LinkAnnot != null)
  106. {
  107. switch (SelectedIndex)
  108. {
  109. case 0:
  110. LinkAnnot.LinkType = LINK_TYPE.URI;
  111. LinkAnnot.URI = "http://" + UrlText.Text.Trim().ToLower();
  112. break;
  113. case 1:
  114. LinkAnnot.LinkType = LINK_TYPE.GOTO;
  115. LinkAnnot.DestIndex = LinkPage - 1;
  116. break;
  117. case 2:
  118. LinkAnnot.LinkType = LINK_TYPE.URI;
  119. LinkAnnot.URI = "mailto:" + EmailText.Text.Trim();
  120. break;
  121. default:
  122. break;
  123. }
  124. LinkAnnot.InvokeLinkSaveCalled(null, null);
  125. }
  126. else
  127. {
  128. switch (SelectedIndex)
  129. {
  130. case 0:
  131. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  132. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "http://" + UrlText.Text.Trim().ToLower());
  133. break;
  134. case 1:
  135. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  136. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkDestIndx, LinkPage - 1);
  137. break;
  138. case 2:
  139. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  140. annotAttribEvent.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + EmailText.Text.Trim());
  141. break;
  142. default:
  143. break;
  144. }
  145. annotAttribEvent.UpdateAnnot();
  146. }
  147. DrawLink = false;
  148. SaveBtn.IsEnabled = false;
  149. }
  150. #region Data Verification
  151. private bool CheckPageNumVaild(out int pageNum, string text)
  152. {
  153. pageNum = -1;
  154. if (string.IsNullOrEmpty(text))
  155. {
  156. return false;
  157. }
  158. if (text.Trim() != string.Empty)
  159. {
  160. if (!int.TryParse(text.Trim(), out pageNum))
  161. {
  162. return false;
  163. }
  164. }
  165. if (pageNum < 1 || pageNum > totalPage)
  166. {
  167. return false;
  168. }
  169. return true;
  170. }
  171. private bool CheckPageWebVaild(string text)
  172. {
  173. if (string.IsNullOrEmpty(text))
  174. {
  175. return false;
  176. }
  177. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  178. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  179. {
  180. return false;
  181. }
  182. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  183. if (matchText.Length != checkUrl.Length)
  184. {
  185. return false;
  186. }
  187. return true;
  188. }
  189. private bool CheckPageMailVaild(string text)
  190. {
  191. if (string.IsNullOrEmpty(text))
  192. {
  193. return false;
  194. }
  195. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  196. {
  197. return false;
  198. }
  199. return true;
  200. }
  201. private void UrlText_TextChanged(object sender, TextChangedEventArgs e)
  202. {
  203. if (LinkAnnot != null)
  204. {
  205. if (CheckPageWebVaild((sender as TextBox).Text) && DrawLink)
  206. {
  207. SaveBtn.IsEnabled = true;
  208. }
  209. else
  210. {
  211. SaveBtn.IsEnabled = false;
  212. }
  213. }
  214. else
  215. {
  216. if (CheckPageWebVaild((sender as TextBox).Text))
  217. {
  218. SaveBtn.IsEnabled = true;
  219. }
  220. else
  221. {
  222. SaveBtn.IsEnabled = false;
  223. }
  224. }
  225. }
  226. private void PageText_TextChanged(object sender, TextChangedEventArgs e)
  227. {
  228. if (LinkAnnot != null)
  229. {
  230. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text) && DrawLink)
  231. {
  232. SaveBtn.IsEnabled = true;
  233. }
  234. else
  235. {
  236. SaveBtn.IsEnabled = false;
  237. }
  238. }
  239. else
  240. {
  241. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text))
  242. {
  243. SaveBtn.IsEnabled = true;
  244. }
  245. else
  246. {
  247. SaveBtn.IsEnabled = false;
  248. }
  249. }
  250. }
  251. private void EmailText_TextChanged(object sender, TextChangedEventArgs e)
  252. {
  253. if (LinkAnnot != null)
  254. {
  255. if (CheckPageMailVaild((sender as TextBox).Text) && DrawLink)
  256. {
  257. SaveBtn.IsEnabled = true;
  258. }
  259. else
  260. {
  261. SaveBtn.IsEnabled = false;
  262. }
  263. }
  264. else
  265. {
  266. if (CheckPageMailVaild((sender as TextBox).Text))
  267. {
  268. SaveBtn.IsEnabled = true;
  269. }
  270. else
  271. {
  272. SaveBtn.IsEnabled = false;
  273. }
  274. }
  275. }
  276. private void CheckingItem(int ItemIndex)
  277. {
  278. bool BtnIsEnabled = false;
  279. if (LinkAnnot != null)
  280. {
  281. switch (ItemIndex)
  282. {
  283. case 0:
  284. BtnIsEnabled = CheckPageWebVaild(UrlText.Text) && DrawLink;
  285. break;
  286. case 1:
  287. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text) && DrawLink;
  288. break;
  289. case 2:
  290. BtnIsEnabled = CheckPageMailVaild(EmailText.Text) && DrawLink;
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. else
  297. {
  298. switch (ItemIndex)
  299. {
  300. case 0:
  301. BtnIsEnabled = CheckPageWebVaild(UrlText.Text);
  302. break;
  303. case 1:
  304. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text);
  305. break;
  306. case 2:
  307. BtnIsEnabled = CheckPageMailVaild(EmailText.Text);
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. SaveBtn.IsEnabled = BtnIsEnabled;
  314. }
  315. #endregion
  316. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  317. {
  318. Binding Indexbinding = new Binding();
  319. Indexbinding.Source = this;
  320. Indexbinding.Path = new System.Windows.PropertyPath("SelectedIndex");
  321. Indexbinding.Mode=BindingMode.TwoWay;
  322. HeadTabControl.SetBinding(TabControl.SelectedIndexProperty, Indexbinding);
  323. }
  324. private void PART_BtnClear_Click(object sender, RoutedEventArgs e)
  325. {
  326. switch (SelectedIndex)
  327. {
  328. case 0:
  329. UrlText.Text = "";
  330. break;
  331. case 1:
  332. PageText.Text = "";
  333. break;
  334. case 2:
  335. EmailText.Text = "";
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. }
  342. }