CPDFLinkUI.xaml.cs 14 KB

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