CPDFLinkUI.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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.Controls.PDFControl;
  14. using ComPDFKit.Tool.Help;
  15. using ComPDFKit.Tool.UndoManger;
  16. using ComPDFKitViewer.Helper;
  17. using ComPDFKit.Import;
  18. namespace ComPDFKit.Controls.Annotation.PDFAnnotationUI
  19. {
  20. public partial class CPDFLinkUI : UserControl, INotifyPropertyChanged
  21. {
  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.DestinationPageIndex + 1).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. CSize pageSize = new CSize();
  159. if (viewControl != null)
  160. {
  161. if (LinkPage - 1 > viewControl.GetCPDFViewer().GetDocument().PageCount || LinkPage - 1 < 0)
  162. {
  163. return;
  164. }
  165. pageSize = viewControl.GetCPDFViewer().GetDocument().PageAtIndex(LinkPage - 1).PageSize;
  166. }
  167. linkParam.DestinationPosition = new CPoint(0, pageSize.height);
  168. break;
  169. case 2:
  170. linkParam.Action = C_ACTION_TYPE.ACTION_TYPE_URI;
  171. linkParam.Uri = "mailto:" + EmailText.Text.Trim();
  172. break;
  173. default:
  174. break;
  175. }
  176. if (viewControl != null)
  177. {
  178. LinkAnnotHistory history = new LinkAnnotHistory();
  179. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  180. history.Action = HistoryAction.Update;
  181. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, linkAnnot.Page.PageIndex, linkAnnot);
  182. if (ParamConverter.SetParamForPDFAnnot(viewControl.GetCPDFViewer().GetDocument(), linkAnnot, linkParam))
  183. {
  184. viewControl.UpdateAnnotFrame();
  185. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, linkAnnot.Page.PageIndex, linkAnnot);
  186. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  187. };
  188. }
  189. }
  190. else
  191. {
  192. if (linkAnnot != null && linkAnnot.IsValid())
  193. {
  194. switch (SelectedIndex)
  195. {
  196. case 0:
  197. {
  198. CPDFUriAction uriAction = new CPDFUriAction();
  199. string urlPath = UrlText.Text.Trim().ToLower();
  200. if (urlPath.StartsWith("http://") || urlPath.StartsWith("https://"))
  201. {
  202. uriAction.SetUri(urlPath);
  203. }
  204. else
  205. {
  206. uriAction.SetUri("http://" + UrlText.Text.Trim().ToLower());
  207. }
  208. linkAnnot.SetLinkAction(uriAction);
  209. }
  210. break;
  211. case 1:
  212. {
  213. CPDFGoToAction gotoAction = new CPDFGoToAction();
  214. CPDFDestination destination = new CPDFDestination();
  215. destination.PageIndex = LinkPage - 1;
  216. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  217. gotoAction.SetDestination(pdfViewer.GetDocument(), destination);
  218. linkAnnot.SetLinkAction(gotoAction);
  219. }
  220. break;
  221. case 2:
  222. {
  223. CPDFUriAction uriAction = new CPDFUriAction();
  224. string urlPath = "mailto:" + EmailText.Text.Trim();
  225. uriAction.SetUri(urlPath);
  226. linkAnnot.SetLinkAction(uriAction);
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. }
  234. DrawLink = false;
  235. SaveBtn.IsEnabled = false;
  236. }
  237. #region Data Verification
  238. private bool CheckPageNumVaild(out int pageNum, string text)
  239. {
  240. pageNum = -1;
  241. if (string.IsNullOrEmpty(text))
  242. {
  243. return false;
  244. }
  245. if (text.Trim() != string.Empty)
  246. {
  247. if (!int.TryParse(text.Trim(), out pageNum))
  248. {
  249. return false;
  250. }
  251. }
  252. if (pageNum < 1 || pageNum > totalPage)
  253. {
  254. return false;
  255. }
  256. return true;
  257. }
  258. private bool CheckPageWebVaild(string text)
  259. {
  260. if (string.IsNullOrEmpty(text))
  261. {
  262. return false;
  263. }
  264. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  265. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  266. {
  267. return false;
  268. }
  269. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  270. if (matchText.Length != checkUrl.Length)
  271. {
  272. return false;
  273. }
  274. return true;
  275. }
  276. private bool CheckPageMailVaild(string text)
  277. {
  278. if (string.IsNullOrEmpty(text))
  279. {
  280. return false;
  281. }
  282. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  283. {
  284. return false;
  285. }
  286. return true;
  287. }
  288. private void UrlText_TextChanged(object sender, TextChangedEventArgs e)
  289. {
  290. if (linkParam != null)
  291. {
  292. if (CheckPageWebVaild((sender as TextBox).Text) && DrawLink)
  293. {
  294. SaveBtn.IsEnabled = true;
  295. }
  296. else
  297. {
  298. SaveBtn.IsEnabled = false;
  299. }
  300. }
  301. else
  302. {
  303. if (CheckPageWebVaild((sender as TextBox).Text))
  304. {
  305. SaveBtn.IsEnabled = true;
  306. }
  307. else
  308. {
  309. SaveBtn.IsEnabled = false;
  310. }
  311. }
  312. }
  313. private void PageText_TextChanged(object sender, TextChangedEventArgs e)
  314. {
  315. if (linkParam != null)
  316. {
  317. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text) && DrawLink)
  318. {
  319. SaveBtn.IsEnabled = true;
  320. }
  321. else
  322. {
  323. SaveBtn.IsEnabled = false;
  324. }
  325. }
  326. else
  327. {
  328. if (CheckPageNumVaild(out LinkPage, (sender as TextBox).Text))
  329. {
  330. SaveBtn.IsEnabled = true;
  331. }
  332. else
  333. {
  334. SaveBtn.IsEnabled = false;
  335. }
  336. }
  337. }
  338. private void EmailText_TextChanged(object sender, TextChangedEventArgs e)
  339. {
  340. if (linkParam != null)
  341. {
  342. if (CheckPageMailVaild((sender as TextBox).Text) && DrawLink)
  343. {
  344. SaveBtn.IsEnabled = true;
  345. }
  346. else
  347. {
  348. SaveBtn.IsEnabled = false;
  349. }
  350. }
  351. else
  352. {
  353. if (CheckPageMailVaild((sender as TextBox).Text))
  354. {
  355. SaveBtn.IsEnabled = true;
  356. }
  357. else
  358. {
  359. SaveBtn.IsEnabled = false;
  360. }
  361. }
  362. }
  363. private void CheckingItem(int ItemIndex)
  364. {
  365. DrawLink = true;
  366. bool BtnIsEnabled = false;
  367. if (linkParam != null)
  368. {
  369. switch (ItemIndex)
  370. {
  371. case 0:
  372. BtnIsEnabled = CheckPageWebVaild(UrlText.Text) && DrawLink;
  373. break;
  374. case 1:
  375. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text) && DrawLink;
  376. break;
  377. case 2:
  378. BtnIsEnabled = CheckPageMailVaild(EmailText.Text) && DrawLink;
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. else
  385. {
  386. switch (ItemIndex)
  387. {
  388. case 0:
  389. BtnIsEnabled = CheckPageWebVaild(UrlText.Text);
  390. break;
  391. case 1:
  392. BtnIsEnabled = CheckPageNumVaild(out LinkPage, PageText.Text);
  393. break;
  394. case 2:
  395. BtnIsEnabled = CheckPageMailVaild(EmailText.Text);
  396. break;
  397. default:
  398. break;
  399. }
  400. }
  401. SaveBtn.IsEnabled = BtnIsEnabled;
  402. SaveBtn.IsEnabled = true;
  403. }
  404. #endregion
  405. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  406. {
  407. Binding Indexbinding = new Binding();
  408. Indexbinding.Source = this;
  409. Indexbinding.Path = new PropertyPath("SelectedIndex");
  410. Indexbinding.Mode = BindingMode.TwoWay;
  411. HeadTabControl.SetBinding(TabControl.SelectedIndexProperty, Indexbinding);
  412. }
  413. private void PART_BtnClear_Click(object sender, RoutedEventArgs e)
  414. {
  415. switch (SelectedIndex)
  416. {
  417. case 0:
  418. UrlText.Text = "";
  419. break;
  420. case 1:
  421. PageText.Text = "";
  422. break;
  423. case 2:
  424. EmailText.Text = "";
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  431. {
  432. UrlText.Text = string.Empty;
  433. PageText.Text = string.Empty;
  434. EmailText.Text = string.Empty;
  435. }
  436. }
  437. }