CPDFLinkUI.xaml.cs 15 KB

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