CPDFLinkUI.xaml.cs 15 KB

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