LinkAnnotPropertyViewModel.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.Model;
  6. using PDF_Office.ViewModels.Tools;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media;
  16. using System.Windows;
  17. using System.Windows.Media.Imaging;
  18. using Prism.Commands;
  19. using PDF_Office.Helper;
  20. using Microsoft.Office.Interop.Word;
  21. using static Dropbox.Api.Files.FileCategory;
  22. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  23. using System.Windows.Controls;
  24. using PDF_Office.CustomControl;
  25. using System.Windows.Markup;
  26. using ComPDFKitViewer;
  27. using System.Runtime.InteropServices;
  28. using System.Text.RegularExpressions;
  29. using System.Net;
  30. using System.Diagnostics;
  31. using static Dropbox.Api.TeamLog.TrustedTeamsRequestState;
  32. using System.Windows.Input;
  33. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  34. {
  35. internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
  36. {
  37. public AnnotAttribEvent AnnotAttribEvent { get; set; }
  38. private AnnotPropertyPanel PropertyPanel;
  39. private AnnotArgsType annotType;
  40. private LinkAnnotArgs linkAnnotArgs;
  41. public LinkAnnotArgs LinkAnnotArgs
  42. {
  43. get
  44. {
  45. return linkAnnotArgs;
  46. }
  47. set
  48. {
  49. if (linkAnnotArgs != null)
  50. {
  51. linkAnnotArgs.LinkDrawFinished -= linkAnnotArgs_LinkDrawFinished;
  52. }
  53. linkAnnotArgs = value;
  54. if (linkAnnotArgs != null)
  55. {
  56. linkAnnotArgs.LinkDrawFinished += linkAnnotArgs_LinkDrawFinished;
  57. }
  58. }
  59. }
  60. public AnnotArgsType AnnotType
  61. {
  62. get { return annotType; }
  63. set
  64. {
  65. SetProperty(ref annotType, value);
  66. }
  67. }
  68. #region 输入框的验证、设置
  69. private string pageNumTextContent;
  70. public string PageNumTextContent
  71. {
  72. get
  73. {
  74. if (CheckPageNumVaild(out int pageNum, pageNumTextContent))
  75. {
  76. if (LinkAnnotArgs != null)
  77. {
  78. LinkAnnotArgs.DestIndex = pageNum - 1;
  79. LinkAnnotArgs.Content = string.Format($"To Page {LinkAnnotArgs.DestIndex}");
  80. //isTextChanged = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] != AnnotArgs.DestIndex ? true : false;
  81. SetImagePreview(LinkAnnotArgs.DestIndex);
  82. }
  83. }
  84. else
  85. {
  86. ImagePreviewVisibility = Visibility.Collapsed;
  87. BtnGOorBackVisibility = Visibility.Collapsed;
  88. }
  89. return pageNumTextContent;
  90. }
  91. set
  92. {
  93. SetProperty(ref pageNumTextContent, value);
  94. }
  95. }
  96. private string pageWebTextContent;
  97. public string PageWebTextContent
  98. {
  99. get
  100. {
  101. if (CheckPageWebVaild(pageWebTextContent))
  102. {
  103. pageWebTextContent = pageWebTextContent.Trim().ToLower();
  104. if (!pageWebTextContent.StartsWith("http://") && !pageWebTextContent.StartsWith("https://"))
  105. {
  106. pageWebTextContent = "http://" + pageWebTextContent;
  107. }
  108. if (LinkAnnotArgs != null)
  109. {
  110. LinkAnnotArgs.URI = pageWebTextContent;
  111. LinkAnnotArgs.Content = LinkAnnotArgs.URI;
  112. //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
  113. }
  114. }
  115. return pageWebTextContent;
  116. }
  117. set
  118. {
  119. SetProperty(ref pageWebTextContent, value);
  120. }
  121. }
  122. private string pageMailTextContent;
  123. public string PageMailTextContent
  124. {
  125. get
  126. {
  127. if (CheckPageMailVaild(pageMailTextContent))
  128. {
  129. if (!string.IsNullOrEmpty(pageMailTextContent) && LinkAnnotArgs != null)
  130. {
  131. LinkAnnotArgs.URI = "mailto:" + pageMailTextContent.Trim();
  132. LinkAnnotArgs.Content = LinkAnnotArgs.URI;
  133. //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
  134. }
  135. }
  136. return pageMailTextContent;
  137. }
  138. set
  139. {
  140. SetProperty(ref pageMailTextContent, value);
  141. }
  142. }
  143. /// <summary>
  144. /// 设置预文本内容
  145. /// </summary>
  146. private string pageNumPlaceHoldText = "输入目标页面";
  147. public string PageNumPlaceHoldText
  148. {
  149. get { return pageNumPlaceHoldText; }
  150. set
  151. {
  152. SetProperty(ref pageNumPlaceHoldText, value);
  153. }
  154. }
  155. /// <summary>
  156. /// 是否显示输入框下方提示文案
  157. /// </summary>
  158. private Visibility showPageNumTip = Visibility.Collapsed;
  159. public Visibility ShowPageNumTip
  160. {
  161. get { return showPageNumTip; }
  162. set
  163. {
  164. SetProperty(ref showPageNumTip, value);
  165. }
  166. }
  167. /// <summary>
  168. /// 输入框下方提示文案
  169. /// </summary>
  170. private string pageNumTipText = "Page number error.";
  171. public string PageNumTipText
  172. {
  173. get { return pageNumTipText; }
  174. set
  175. {
  176. SetProperty(ref pageNumTipText, value);
  177. }
  178. }
  179. private bool pageNumTextIsEnabled = false;
  180. public bool PageNumTextIsEnabled
  181. {
  182. get { return pageNumTextIsEnabled; }
  183. set
  184. {
  185. SetProperty(ref pageNumTextIsEnabled, value);
  186. }
  187. }
  188. private bool pageWebTextIsEnabled = false;
  189. public bool PageWebTextIsEnabled
  190. {
  191. get { return pageWebTextIsEnabled; }
  192. set
  193. {
  194. SetProperty(ref pageWebTextIsEnabled, value);
  195. }
  196. }
  197. private string pageWebPlaceHoldText = "输入您要跳转的链接";
  198. public string PageWebPlaceHoldText
  199. {
  200. get { return pageWebPlaceHoldText; }
  201. set
  202. {
  203. SetProperty(ref pageWebPlaceHoldText, value);
  204. }
  205. }
  206. private Visibility showPageWebTip = Visibility.Collapsed;
  207. public Visibility ShowPageWebTip
  208. {
  209. get { return showPageWebTip; }
  210. set
  211. {
  212. SetProperty(ref showPageWebTip, value);
  213. }
  214. }
  215. private string pageWebTipText = "Invalid link.";
  216. public string PageWebTipText
  217. {
  218. get { return pageWebTipText; }
  219. set
  220. {
  221. SetProperty(ref pageWebTipText, value);
  222. }
  223. }
  224. private bool pageMailTextIsEnabled = false;
  225. public bool PageMailTextIsEnabled
  226. {
  227. get { return pageMailTextIsEnabled; }
  228. set
  229. {
  230. SetProperty(ref pageMailTextIsEnabled, value);
  231. }
  232. }
  233. private string pageMailPlaceHoldText = "输入您要跳转邮件地址";
  234. public string PageMailPlaceHoldText
  235. {
  236. get { return pageMailPlaceHoldText; }
  237. set
  238. {
  239. SetProperty(ref pageMailPlaceHoldText, value);
  240. }
  241. }
  242. private Visibility showPageMailTip = Visibility.Collapsed;
  243. public Visibility ShowPageMailTip
  244. {
  245. get { return showPageMailTip; }
  246. set
  247. {
  248. SetProperty(ref showPageMailTip, value);
  249. }
  250. }
  251. private string pageMailTipText = "The email is invalid.Please enter a valid email address.";
  252. public string PageMailTipText
  253. {
  254. get { return pageMailTipText; }
  255. set
  256. {
  257. SetProperty(ref pageMailTipText, value);
  258. }
  259. }
  260. #endregion 输入框的验证、设置
  261. #region 按钮、图片的设置
  262. private bool btnLocationIsEnabled = false;
  263. public bool BtnLocationIsEnabled
  264. {
  265. get { return btnLocationIsEnabled; }
  266. set
  267. {
  268. SetProperty(ref btnLocationIsEnabled, value);
  269. }
  270. }
  271. private WriteableBitmap previewImage;
  272. public WriteableBitmap PreviewImage
  273. {
  274. get { return previewImage; }
  275. set
  276. {
  277. SetProperty(ref previewImage, value);
  278. }
  279. }
  280. private int imagePreviewWidth = 196;
  281. public int ImagePreviewWidth
  282. {
  283. get { return imagePreviewWidth; }
  284. set
  285. {
  286. SetProperty(ref imagePreviewWidth, value);
  287. }
  288. }
  289. private int imagePreviewHeight = 256;
  290. public int ImagePreviewHeight
  291. {
  292. get { return imagePreviewHeight; }
  293. set
  294. {
  295. SetProperty(ref imagePreviewHeight, value);
  296. }
  297. }
  298. private Visibility imagePreviewVisibility = Visibility.Collapsed;
  299. public Visibility ImagePreviewVisibility
  300. {
  301. get { return imagePreviewVisibility; }
  302. set
  303. {
  304. SetProperty(ref imagePreviewVisibility, value);
  305. }
  306. }
  307. private Visibility btnGOorBackVisibility = Visibility.Collapsed;
  308. public Visibility BtnGOorBackVisibility
  309. {
  310. get { return btnGOorBackVisibility; }
  311. set
  312. {
  313. SetProperty(ref btnGOorBackVisibility, value);
  314. }
  315. }
  316. private string btnGOorBackContent = "GO";
  317. public string BtnGOorBackContent
  318. {
  319. get { return btnGOorBackContent; }
  320. set
  321. {
  322. SetProperty(ref btnGOorBackContent, value);
  323. }
  324. }
  325. private bool btnLinkPageIsChecked = true;
  326. public bool BtnLinkPageIsChecked
  327. {
  328. get { return btnLinkPageIsChecked; }
  329. set
  330. {
  331. SetProperty(ref btnLinkPageIsChecked, value);
  332. }
  333. }
  334. private bool btnLinkWebIsChecked = false;
  335. public bool BtnLinkWebIsChecked
  336. {
  337. get { return btnLinkWebIsChecked; }
  338. set
  339. {
  340. SetProperty(ref btnLinkWebIsChecked, value);
  341. }
  342. }
  343. private bool btnLinkMailIsChecked = false;
  344. public bool BtnLinkMailIsChecked
  345. {
  346. get { return btnLinkMailIsChecked; }
  347. set
  348. {
  349. SetProperty(ref btnLinkMailIsChecked, value);
  350. }
  351. }
  352. #endregion 按钮、图片的设置
  353. private bool isLoaded = false;
  354. private bool isMail = false;
  355. private bool isTextChanged = false;
  356. private string historyBtnGOorBackTag = string.Empty;
  357. private int totalPage = 0;
  358. private int backPageIndex = 0;
  359. private int historyPageIndex = 0;
  360. private CPDFViewer pdfViewer;
  361. private CPDFDocument document;
  362. private Button btnGOorBack = null;
  363. private LinkAnnotProperty linkAnnot;
  364. public DelegateCommand<object> LoadedCommand { get; set; }
  365. public DelegateCommand<object> PageNumTextLostFocusCommand { get; set; }
  366. public DelegateCommand<object> PageNumTextKeyDownCommand { get; set; }
  367. public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
  368. public DelegateCommand<object> LocationCommand { get; set; }
  369. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  370. public LinkAnnotPropertyViewModel()
  371. {
  372. LoadedCommand = new DelegateCommand<object>(Loaded);
  373. PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
  374. PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
  375. BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
  376. LocationCommand = new DelegateCommand<object>(LocationPage);
  377. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  378. isLoaded = true;
  379. }
  380. private void PageNumTextKeyDown(object obj)
  381. {
  382. if (obj is KeyEventArgs keyEventArgs)
  383. {
  384. if (keyEventArgs.Key == Key.Enter)
  385. {
  386. PageNumTextLostFocus(obj);
  387. }
  388. }
  389. }
  390. private void PageNumTextLostFocus(object obj)
  391. {
  392. if (!string.IsNullOrEmpty(PageNumTextContent))
  393. {
  394. if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  395. {
  396. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  397. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  398. AnnotAttribEvent?.UpdateAnnot();
  399. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  400. //isTextChanged = false;
  401. SetImagePreview(LinkAnnotArgs.DestIndex);
  402. BtnLocationIsEnabled = false;
  403. }
  404. else
  405. {
  406. ImagePreviewVisibility = Visibility.Collapsed;
  407. BtnGOorBackVisibility = Visibility.Collapsed;
  408. }
  409. }
  410. }
  411. private void ToggleButtonTabSelected(object obj)
  412. {
  413. if (obj is RadioButton radioButton)
  414. {
  415. switch (radioButton.Tag)
  416. {
  417. case "Page":
  418. LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
  419. LinkAnnotArgs.URI = string.Empty;
  420. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  421. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  422. isMail = false;
  423. if (btnGOorBack != null)
  424. {
  425. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  426. {
  427. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  428. }
  429. else
  430. {
  431. ChangeBtnGOorBack("GO", btnGOorBack);
  432. }
  433. }
  434. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  435. break;
  436. case "Web":
  437. LinkAnnotArgs.LinkType = LINK_TYPE.URI;
  438. LinkAnnotArgs.URI = string.Empty;
  439. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  440. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  441. isMail = false;
  442. if (btnGOorBack != null)
  443. {
  444. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  445. ChangeBtnGOorBack("GO", btnGOorBack);
  446. }
  447. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  448. break;
  449. case "Mail":
  450. LinkAnnotArgs.LinkType = LINK_TYPE.URI;
  451. LinkAnnotArgs.URI = string.Empty;
  452. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  453. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  454. isMail = true;
  455. if (btnGOorBack != null)
  456. {
  457. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  458. ChangeBtnGOorBack("GO", btnGOorBack);
  459. }
  460. BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  461. break;
  462. default:
  463. LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
  464. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  465. isMail = false;
  466. if (btnGOorBack != null)
  467. {
  468. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  469. {
  470. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  471. }
  472. else
  473. {
  474. ChangeBtnGOorBack("GO", btnGOorBack);
  475. }
  476. }
  477. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  478. break;
  479. }
  480. }
  481. }
  482. /// <summary>
  483. /// 定位
  484. /// </summary>
  485. /// <param name="obj"></param>
  486. private void LocationPage(object obj)
  487. {
  488. historyPageIndex = pdfViewer.CurrentIndex;
  489. PageNumTextIsEnabled = false;
  490. pdfViewer.EnableDrawSelectArea(true);
  491. }
  492. /// <summary>
  493. /// 前往/返回按钮
  494. /// </summary>
  495. /// <param name="obj"></param>
  496. private void BtnGOorBackPageEvent(object obj)
  497. {
  498. if (obj is object[] arry)
  499. {
  500. btnGOorBack = arry[0] as Button;
  501. RadioButton btnLinkPage = arry[1] as RadioButton;
  502. RadioButton btnLinkWeb = arry[2] as RadioButton;
  503. RadioButton btnLinkMail = arry[3] as RadioButton;
  504. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  505. {
  506. if (btnLinkPage.IsChecked == true)
  507. {
  508. LinkPageGoOrBack();
  509. }
  510. if (btnLinkWeb.IsChecked == true)
  511. {
  512. LinkWebSave();
  513. }
  514. if (btnLinkMail.IsChecked == true)
  515. {
  516. LinkMailSave();
  517. }
  518. }
  519. }
  520. }
  521. private void LinkMailSave()
  522. {
  523. if (CheckPageMailVaild(PageMailTextContent))
  524. {
  525. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  526. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  527. AnnotAttribEvent?.UpdateAnnot();
  528. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  529. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  530. }
  531. }
  532. private void LinkWebSave()
  533. {
  534. if (CheckPageWebVaild(PageWebTextContent))
  535. {
  536. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  537. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  538. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  539. {
  540. PageWebTextContent = "http://" + PageWebTextContent;
  541. }
  542. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  543. AnnotAttribEvent?.UpdateAnnot();
  544. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  545. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  546. {
  547. Process.Start(PageWebTextContent);
  548. }
  549. }
  550. else
  551. {
  552. BtnGOorBackVisibility = Visibility.Collapsed;
  553. }
  554. }
  555. private void LinkPageGoOrBack()
  556. {
  557. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  558. if (btnGOorBack.Tag.ToString() == "GO")
  559. {
  560. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  561. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  562. //AnnotAttribEvent?.UpdateAnnot();
  563. historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
  564. pdfViewer.GoToPage(pageIndex);
  565. ChangeBtnGOorBack("BACK", btnGOorBack);
  566. }
  567. else
  568. {
  569. pdfViewer.GoToPage(historyPageIndex);
  570. ChangeBtnGOorBack("GO", btnGOorBack);
  571. }
  572. }
  573. /// <summary>
  574. /// 设置前往/返回按钮的文本
  575. /// </summary>
  576. /// <param name="flag"></param>
  577. /// <param name="button"></param>
  578. private void ChangeBtnGOorBack(string flag, Button button)
  579. {
  580. switch (flag)
  581. {
  582. case "BACK":
  583. button.Tag = "BACK";
  584. button.Content = "BACK";
  585. break;
  586. case "GO":
  587. button.Tag = "GO";
  588. button.Content = "GO";
  589. break;
  590. default:
  591. break;
  592. }
  593. }
  594. private void PageNumTextChanged(object obj)
  595. {
  596. if (obj is TextChangedEventArgs textChangedEventArgs)
  597. {
  598. if (textChangedEventArgs.Source is TextBoxWithTip textBox)
  599. {
  600. int pageNum = -1;
  601. //if (CheckPageNumVaild(out pageNum))
  602. //{
  603. // if (AnnotArgs != null)
  604. // {
  605. // AnnotArgs.DestIndex = pageNum - 1;
  606. // SetImagePreview();
  607. // }
  608. //}
  609. }
  610. }
  611. }
  612. /// <summary>
  613. /// 页码格式验证
  614. /// </summary>
  615. /// <param name="pageNum"></param>
  616. /// <param name="text"></param>
  617. /// <returns></returns>
  618. private bool CheckPageNumVaild(out int pageNum, string text)
  619. {
  620. pageNum = -1;
  621. if (string.IsNullOrEmpty(text))
  622. {
  623. return false;
  624. }
  625. if (text.Trim() != string.Empty)
  626. {
  627. if (!int.TryParse(text.Trim(), out pageNum))
  628. {
  629. ShowPageNumTip = Visibility.Visible;
  630. PageNumTipText = " Page number error.";
  631. return false;
  632. }
  633. }
  634. if (pageNum < 1 || pageNum > totalPage)
  635. {
  636. ShowPageNumTip = Visibility.Visible;
  637. PageNumTipText = " Page number out of range.";
  638. return false;
  639. }
  640. else
  641. {
  642. ShowPageNumTip = Visibility.Collapsed;
  643. }
  644. //BtnGOorBackVisibility = Visibility.Visible;
  645. return true;
  646. }
  647. private bool CheckPageWebVaild(string text)
  648. {
  649. BtnGOorBackVisibility = Visibility.Collapsed;
  650. if (string.IsNullOrEmpty(text))
  651. {
  652. return false;
  653. }
  654. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  655. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  656. {
  657. ShowPageWebTip = Visibility.Visible;
  658. PageWebTipText = "Invalid link.";
  659. return false;
  660. }
  661. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  662. if (matchText.Length != checkUrl.Length)
  663. {
  664. ShowPageWebTip = Visibility.Visible;
  665. PageWebTipText = "Invalid link.";
  666. return false;
  667. }
  668. else
  669. {
  670. ShowPageWebTip = Visibility.Collapsed;
  671. }
  672. BtnGOorBackVisibility = Visibility.Visible;
  673. return true;
  674. }
  675. private bool CheckPageMailVaild(string text)
  676. {
  677. BtnGOorBackVisibility = Visibility.Collapsed;
  678. if (string.IsNullOrEmpty(text))
  679. {
  680. return false;
  681. }
  682. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  683. {
  684. ShowPageMailTip = Visibility.Visible;
  685. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  686. return false;
  687. }
  688. else
  689. {
  690. ShowPageMailTip = Visibility.Collapsed;
  691. }
  692. BtnGOorBackVisibility = Visibility.Visible;
  693. return true;
  694. }
  695. /// <summary>
  696. /// 设置图像
  697. /// </summary>
  698. /// <param name="pageIndex"></param>
  699. private void SetImagePreview(int pageIndex)
  700. {
  701. ImagePreviewVisibility = Visibility.Visible;
  702. BtnGOorBackVisibility = Visibility.Visible;
  703. int dpiHeight = (int)ImagePreviewHeight;
  704. int dpiWidth = (int)ImagePreviewWidth;
  705. if (dpiHeight <= 0 || dpiWidth <= 0)
  706. {
  707. return;
  708. }
  709. //int pageIndex = AnnotArgs.DestIndex;
  710. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  711. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  712. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  713. scale = Math.Min(scale, 1);
  714. int cropWidth = (int)(scale * pageSize.Width);
  715. int cropHeight = (int)(scale * pageSize.Height);
  716. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  717. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  718. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  719. PreviewImage = WirteBitmap;
  720. }
  721. /// <summary>
  722. /// 链接画框完成时触发事件
  723. /// </summary>
  724. /// <param name="sender"></param>
  725. /// <param name="e"></param>
  726. private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
  727. {
  728. //linkAnnot.SetTextBoxEnableOrNot(e, totalPage);
  729. //AnnotArgs.DestIndex = -1;
  730. //AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] =- 1;
  731. SetTextBoxEnableOrNot(e, totalPage);
  732. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  733. }
  734. private void Loaded(object obj)
  735. {
  736. if (obj is CompositeCommandParameter composite)
  737. {
  738. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  739. {
  740. linkAnnot = linkAnnotProperty;
  741. }
  742. }
  743. }
  744. public bool IsNavigationTarget(NavigationContext navigationContext)
  745. {
  746. return true;
  747. }
  748. public void OnNavigatedFrom(NavigationContext navigationContext)
  749. {
  750. }
  751. public void OnNavigatedTo(NavigationContext navigationContext)
  752. {
  753. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  754. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  755. if (PropertyPanel != null && pdfViewer != null)
  756. {
  757. pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  758. pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  759. AnnotAttribEvent = PropertyPanel.AnnotEvent;
  760. AnnotType = PropertyPanel.annot.EventType;
  761. document = pdfViewer.Document;
  762. totalPage = pdfViewer.Document.PageCount;
  763. SetTextBoxEnableOrNot(false, totalPage);
  764. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  765. {
  766. GetAnnotAttribute();
  767. }
  768. if (isLoaded)
  769. {
  770. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  771. {
  772. if (btnGOorBack != null)
  773. {
  774. ChangeBtnGOorBack("GO", btnGOorBack);
  775. }
  776. if (AnnotAttribEvent.IsAnnotCreateReset)
  777. {
  778. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  779. if (LinkAnnotArgs == null)
  780. {
  781. backPageIndex = pdfViewer.CurrentIndex;
  782. }
  783. else
  784. {
  785. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  786. }
  787. }
  788. }
  789. }
  790. if (LinkAnnotArgs == null)
  791. {
  792. LinkAnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  793. }
  794. }
  795. }
  796. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  797. {
  798. if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
  799. {
  800. if (e.NotifyData is AreaLocate areaInfo)
  801. {
  802. if (pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  803. {
  804. LinkAnnotArgs linkArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  805. System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
  806. linkArgs.IsPositionSet = true;
  807. linkArgs.DestIndex = areaInfo.PageIndex;
  808. linkArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  809. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  810. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
  811. AnnotAttribEvent?.UpdateAnnot();
  812. linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  813. pdfViewer.GoToPage(historyPageIndex);
  814. LinkAnnotArgs = linkArgs;
  815. LinkAnnotArgs.PageIndex = historyPageIndex;
  816. PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
  817. SetImagePreview(areaInfo.PageIndex);
  818. }
  819. }
  820. }
  821. }
  822. private void GetAnnotAttribute()
  823. {
  824. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  825. {
  826. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  827. }
  828. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  829. {
  830. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  831. if (LinkAnnotArgs == null)
  832. {
  833. backPageIndex = pdfViewer.CurrentIndex;
  834. }
  835. else
  836. {
  837. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  838. }
  839. }
  840. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  841. {
  842. string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  843. if (!string.IsNullOrEmpty(linkUrl))
  844. {
  845. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  846. {
  847. SetLinkType(LINK_TYPE.URI, true);
  848. if (AnnotAttribEvent.IsAnnotCreateReset)
  849. {
  850. SetLinkEmail("");
  851. }
  852. else
  853. {
  854. SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  855. }
  856. }
  857. else
  858. {
  859. if (AnnotAttribEvent.IsAnnotCreateReset)
  860. {
  861. SetLinkUrl("");
  862. }
  863. else
  864. {
  865. SetLinkUrl(linkUrl);
  866. }
  867. }
  868. }
  869. }
  870. }
  871. private void SetLinkUrl(string url)
  872. {
  873. PageWebTextContent = url;
  874. PageWebTextIsEnabled = true;
  875. }
  876. private void SetLinkEmail(string email)
  877. {
  878. PageMailTextContent = email;
  879. PageMailTextIsEnabled = true;
  880. }
  881. /// <summary>
  882. /// 设置页码的数据
  883. /// </summary>
  884. /// <param name="pageNum"></param>
  885. private void SetLinkPageNum(int pageNum)
  886. {
  887. if (pageNum > 0 && pageNum <= totalPage)
  888. {
  889. PageNumTextContent = pageNum.ToString();
  890. BtnLocationIsEnabled = true;
  891. PageNumTextIsEnabled = true;
  892. ImagePreviewVisibility = Visibility.Visible;
  893. SetImagePreview(pageNum - 1);
  894. BtnLocationIsEnabled = true;
  895. //if (btnGOorBack != null)
  896. //{
  897. // ChangeBtnGOorBack("GO", btnGOorBack);
  898. //}
  899. }
  900. }
  901. /// <summary>
  902. /// 根据链接类型显示模块
  903. /// </summary>
  904. /// <param name="linkType"></param>
  905. /// <param name="isMail"></param>
  906. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  907. {
  908. BtnLinkPageIsChecked = false;
  909. BtnLinkMailIsChecked = false;
  910. BtnLinkWebIsChecked = false;
  911. switch (linkType)
  912. {
  913. case LINK_TYPE.GOTO:
  914. BtnLinkPageIsChecked = true;
  915. break;
  916. case LINK_TYPE.URI:
  917. if (this.isMail || isMail)
  918. {
  919. BtnLinkMailIsChecked = true;
  920. }
  921. else
  922. {
  923. BtnLinkWebIsChecked = true;
  924. }
  925. break;
  926. default:
  927. BtnLinkPageIsChecked = true;
  928. break;
  929. }
  930. }
  931. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  932. {
  933. this.totalPage = totalPage;
  934. PageNumTextContent = string.Empty;
  935. PageWebTextContent = string.Empty;
  936. PageMailTextContent = string.Empty;
  937. PageNumTextIsEnabled = enable;
  938. PageWebTextIsEnabled = enable;
  939. PageMailTextIsEnabled = enable;
  940. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  941. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  942. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  943. ShowPageNumTip = Visibility.Collapsed;
  944. ShowPageWebTip = Visibility.Collapsed;
  945. ShowPageMailTip = Visibility.Collapsed;
  946. BtnLocationIsEnabled = enable;
  947. }
  948. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  949. {
  950. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  951. {
  952. return null;
  953. }
  954. try
  955. {
  956. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  957. currentZoom = currentZoom * 96 / 72;
  958. int flag = 0;
  959. if (renderAnnot)
  960. {
  961. flag = 1;
  962. }
  963. uint bgColor = 0xFFFFFFFF;
  964. CPDFPage page = document.PageAtIndex(pageIndex);
  965. if (page.IsValid())
  966. {
  967. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  968. }
  969. return bmpData;
  970. }
  971. catch (Exception ex)
  972. {
  973. return null;
  974. }
  975. }
  976. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  977. {
  978. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  979. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  980. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  981. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  982. return boundRect;
  983. }
  984. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  985. {
  986. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  987. pageSize.Width = pageSize.Width / 72.0 * 96;
  988. pageSize.Height = pageSize.Height / 72.0 * 96;
  989. return pageSize;
  990. }
  991. }
  992. }