CPDFLinkUI.xaml.cs 14 KB

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