LinkAnnotPropertyViewModel.cs 43 KB

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