LinkAnnotPropertyViewModel.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 annotArgs;
  41. public LinkAnnotArgs AnnotArgs
  42. {
  43. get
  44. {
  45. return annotArgs;
  46. }
  47. set
  48. {
  49. if (annotArgs != null)
  50. {
  51. annotArgs.LinkDrawFinished -= annotArgs_LinkDrawFinished;
  52. }
  53. annotArgs = value;
  54. if (annotArgs != null)
  55. {
  56. annotArgs.LinkDrawFinished += annotArgs_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 (AnnotArgs != null)
  77. {
  78. AnnotArgs.DestIndex = pageNum - 1;
  79. AnnotArgs.Content = string.Format($"To Page {AnnotArgs.DestIndex}");
  80. //isTextChanged = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] != AnnotArgs.DestIndex ? true : false;
  81. //SetImagePreview(AnnotArgs.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 (AnnotArgs != null)
  109. {
  110. AnnotArgs.URI = pageWebTextContent;
  111. AnnotArgs.Content = AnnotArgs.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) && AnnotArgs != null)
  130. {
  131. AnnotArgs.URI = "mailto:" + pageMailTextContent.Trim();
  132. AnnotArgs.Content = AnnotArgs.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. AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  400. //isTextChanged = false;
  401. SetImagePreview(AnnotArgs.DestIndex);
  402. }
  403. else
  404. {
  405. ImagePreviewVisibility = Visibility.Collapsed;
  406. BtnGOorBackVisibility = Visibility.Collapsed;
  407. }
  408. }
  409. }
  410. private void ToggleButtonTabSelected(object obj)
  411. {
  412. if (obj is RadioButton radioButton)
  413. {
  414. switch (radioButton.Tag)
  415. {
  416. case "Page":
  417. AnnotArgs.LinkType = LINK_TYPE.GOTO;
  418. AnnotArgs.URI = string.Empty;
  419. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  420. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  421. isMail = false;
  422. if (btnGOorBack != null)
  423. {
  424. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  425. {
  426. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  427. }
  428. else
  429. {
  430. ChangeBtnGOorBack("GO", btnGOorBack);
  431. }
  432. }
  433. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  434. break;
  435. case "Web":
  436. AnnotArgs.LinkType = LINK_TYPE.URI;
  437. AnnotArgs.URI = string.Empty;
  438. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  439. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  440. isMail = false;
  441. if (btnGOorBack != null)
  442. {
  443. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  444. ChangeBtnGOorBack("GO", btnGOorBack);
  445. }
  446. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  447. break;
  448. case "Mail":
  449. AnnotArgs.LinkType = LINK_TYPE.URI;
  450. AnnotArgs.URI = string.Empty;
  451. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  452. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  453. isMail = true;
  454. if (btnGOorBack != null)
  455. {
  456. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  457. ChangeBtnGOorBack("GO", btnGOorBack);
  458. }
  459. BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  460. break;
  461. default:
  462. AnnotArgs.LinkType = LINK_TYPE.GOTO;
  463. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  464. isMail = false;
  465. if (btnGOorBack != null)
  466. {
  467. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  468. {
  469. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  470. }
  471. else
  472. {
  473. ChangeBtnGOorBack("GO", btnGOorBack);
  474. }
  475. }
  476. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  477. break;
  478. }
  479. }
  480. }
  481. /// <summary>
  482. /// 定位
  483. /// </summary>
  484. /// <param name="obj"></param>
  485. private void LocationPage(object obj)
  486. {
  487. if (string.IsNullOrEmpty(PageNumTextContent))
  488. {
  489. //if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  490. //{
  491. // AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  492. // AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  493. // AnnotAttribEvent?.UpdateAnnot();
  494. // AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  495. // //isTextChanged = false;
  496. //}
  497. }
  498. }
  499. /// <summary>
  500. /// 前往/返回按钮
  501. /// </summary>
  502. /// <param name="obj"></param>
  503. private void BtnGOorBackPageEvent(object obj)
  504. {
  505. if (obj is object[] arry)
  506. {
  507. btnGOorBack = arry[0] as Button;
  508. RadioButton btnLinkPage = arry[1] as RadioButton;
  509. RadioButton btnLinkWeb = arry[2] as RadioButton;
  510. RadioButton btnLinkMail = arry[3] as RadioButton;
  511. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  512. {
  513. if (btnLinkPage.IsChecked == true)
  514. {
  515. LinkPageGoOrBack();
  516. }
  517. if (btnLinkWeb.IsChecked == true)
  518. {
  519. LinkWebSave();
  520. }
  521. if (btnLinkMail.IsChecked == true)
  522. {
  523. LinkMailSave();
  524. }
  525. }
  526. }
  527. }
  528. private void LinkMailSave()
  529. {
  530. if (CheckPageMailVaild(PageMailTextContent))
  531. {
  532. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  533. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  534. AnnotAttribEvent?.UpdateAnnot();
  535. AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  536. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  537. }
  538. }
  539. private void LinkWebSave()
  540. {
  541. if (CheckPageWebVaild(PageWebTextContent))
  542. {
  543. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  544. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  545. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  546. {
  547. PageWebTextContent = "http://" + PageWebTextContent;
  548. }
  549. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  550. AnnotAttribEvent?.UpdateAnnot();
  551. AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  552. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  553. {
  554. Process.Start(PageWebTextContent);
  555. }
  556. }
  557. else
  558. {
  559. BtnGOorBackVisibility = Visibility.Collapsed;
  560. }
  561. }
  562. private void LinkPageGoOrBack()
  563. {
  564. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  565. if (btnGOorBack.Tag.ToString() == "GO")
  566. {
  567. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  568. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  569. //AnnotAttribEvent?.UpdateAnnot();
  570. historyPageIndex = AnnotArgs != null ? AnnotArgs.PageIndex : backPageIndex;
  571. pdfViewer.GoToPage(pageIndex);
  572. ChangeBtnGOorBack("BACK", btnGOorBack);
  573. }
  574. else
  575. {
  576. pdfViewer.GoToPage(historyPageIndex);
  577. ChangeBtnGOorBack("GO", btnGOorBack);
  578. }
  579. }
  580. /// <summary>
  581. /// 设置前往/返回按钮的文本
  582. /// </summary>
  583. /// <param name="flag"></param>
  584. /// <param name="button"></param>
  585. private void ChangeBtnGOorBack(string flag, Button button)
  586. {
  587. switch (flag)
  588. {
  589. case "BACK":
  590. button.Tag = "BACK";
  591. button.Content = "BACK";
  592. break;
  593. case "GO":
  594. button.Tag = "GO";
  595. button.Content = "GO";
  596. break;
  597. default:
  598. break;
  599. }
  600. }
  601. private void PageNumTextChanged(object obj)
  602. {
  603. if (obj is TextChangedEventArgs textChangedEventArgs)
  604. {
  605. if (textChangedEventArgs.Source is TextBoxWithTip textBox)
  606. {
  607. int pageNum = -1;
  608. //if (CheckPageNumVaild(out pageNum))
  609. //{
  610. // if (AnnotArgs != null)
  611. // {
  612. // AnnotArgs.DestIndex = pageNum - 1;
  613. // SetImagePreview();
  614. // }
  615. //}
  616. }
  617. }
  618. }
  619. /// <summary>
  620. /// 页码格式验证
  621. /// </summary>
  622. /// <param name="pageNum"></param>
  623. /// <param name="text"></param>
  624. /// <returns></returns>
  625. private bool CheckPageNumVaild(out int pageNum, string text)
  626. {
  627. pageNum = -1;
  628. if (string.IsNullOrEmpty(text))
  629. {
  630. return false;
  631. }
  632. if (text.Trim() != string.Empty)
  633. {
  634. if (!int.TryParse(text.Trim(), out pageNum))
  635. {
  636. ShowPageNumTip = Visibility.Visible;
  637. PageNumTipText = " Page number error.";
  638. return false;
  639. }
  640. }
  641. if (pageNum < 1 || pageNum > totalPage)
  642. {
  643. ShowPageNumTip = Visibility.Visible;
  644. PageNumTipText = " Page number out of range.";
  645. return false;
  646. }
  647. else
  648. {
  649. ShowPageNumTip = Visibility.Collapsed;
  650. }
  651. //BtnGOorBackVisibility = Visibility.Visible;
  652. return true;
  653. }
  654. private bool CheckPageWebVaild(string text)
  655. {
  656. BtnGOorBackVisibility = Visibility.Collapsed;
  657. if (string.IsNullOrEmpty(text))
  658. {
  659. return false;
  660. }
  661. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  662. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  663. {
  664. ShowPageWebTip = Visibility.Visible;
  665. PageWebTipText = "Invalid link.";
  666. return false;
  667. }
  668. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  669. if (matchText.Length != checkUrl.Length)
  670. {
  671. ShowPageWebTip = Visibility.Visible;
  672. PageWebTipText = "Invalid link.";
  673. return false;
  674. }
  675. else
  676. {
  677. ShowPageWebTip = Visibility.Collapsed;
  678. }
  679. BtnGOorBackVisibility = Visibility.Visible;
  680. return true;
  681. }
  682. private bool CheckPageMailVaild(string text)
  683. {
  684. BtnGOorBackVisibility = Visibility.Collapsed;
  685. if (string.IsNullOrEmpty(text))
  686. {
  687. return false;
  688. }
  689. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  690. {
  691. ShowPageMailTip = Visibility.Visible;
  692. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  693. return false;
  694. }
  695. else
  696. {
  697. ShowPageMailTip = Visibility.Collapsed;
  698. }
  699. BtnGOorBackVisibility = Visibility.Visible;
  700. return true;
  701. }
  702. /// <summary>
  703. /// 设置图像
  704. /// </summary>
  705. /// <param name="pageIndex"></param>
  706. private void SetImagePreview(int pageIndex)
  707. {
  708. ImagePreviewVisibility = Visibility.Visible;
  709. BtnGOorBackVisibility = Visibility.Visible;
  710. int dpiHeight = (int)ImagePreviewHeight;
  711. int dpiWidth = (int)ImagePreviewWidth;
  712. if (dpiHeight <= 0 || dpiWidth <= 0)
  713. {
  714. return;
  715. }
  716. //int pageIndex = AnnotArgs.DestIndex;
  717. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  718. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  719. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  720. scale = Math.Min(scale, 1);
  721. int cropWidth = (int)(scale * pageSize.Width);
  722. int cropHeight = (int)(scale * pageSize.Height);
  723. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  724. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  725. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  726. PreviewImage = WirteBitmap;
  727. }
  728. /// <summary>
  729. /// 链接画框完成时触发事件
  730. /// </summary>
  731. /// <param name="sender"></param>
  732. /// <param name="e"></param>
  733. private void annotArgs_LinkDrawFinished(object sender, bool e)
  734. {
  735. //linkAnnot.SetTextBoxEnableOrNot(e, totalPage);
  736. //AnnotArgs.DestIndex = -1;
  737. //AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] =- 1;
  738. SetTextBoxEnableOrNot(e, totalPage);
  739. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  740. }
  741. private void Loaded(object obj)
  742. {
  743. if (obj is CompositeCommandParameter composite)
  744. {
  745. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  746. {
  747. linkAnnot = linkAnnotProperty;
  748. }
  749. }
  750. }
  751. public bool IsNavigationTarget(NavigationContext navigationContext)
  752. {
  753. return true;
  754. }
  755. public void OnNavigatedFrom(NavigationContext navigationContext)
  756. {
  757. }
  758. public void OnNavigatedTo(NavigationContext navigationContext)
  759. {
  760. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  761. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  762. if (PropertyPanel != null && pdfViewer != null)
  763. {
  764. AnnotAttribEvent = PropertyPanel.AnnotEvent;
  765. AnnotType = PropertyPanel.annot.EventType;
  766. document = pdfViewer.Document;
  767. totalPage = pdfViewer.Document.PageCount;
  768. SetTextBoxEnableOrNot(false, totalPage);
  769. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  770. {
  771. GetAnnotAttribute();
  772. }
  773. if (isLoaded)
  774. {
  775. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  776. {
  777. if (btnGOorBack != null)
  778. {
  779. ChangeBtnGOorBack("GO", btnGOorBack);
  780. }
  781. if (AnnotAttribEvent.IsAnnotCreateReset)
  782. {
  783. AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  784. if (AnnotArgs == null)
  785. {
  786. backPageIndex = pdfViewer.CurrentIndex;
  787. }
  788. else
  789. {
  790. AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  791. }
  792. }
  793. }
  794. }
  795. if (AnnotArgs == null)
  796. {
  797. AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  798. }
  799. }
  800. }
  801. private void GetAnnotAttribute()
  802. {
  803. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  804. {
  805. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  806. }
  807. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  808. {
  809. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  810. if (AnnotArgs == null)
  811. {
  812. backPageIndex = pdfViewer.CurrentIndex;
  813. }
  814. else
  815. {
  816. AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  817. }
  818. }
  819. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  820. {
  821. string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  822. if (!string.IsNullOrEmpty(linkUrl))
  823. {
  824. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  825. {
  826. SetLinkType(LINK_TYPE.URI, true);
  827. if (AnnotAttribEvent.IsAnnotCreateReset)
  828. {
  829. SetLinkEmail("");
  830. }
  831. else
  832. {
  833. SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  834. }
  835. }
  836. else
  837. {
  838. if (AnnotAttribEvent.IsAnnotCreateReset)
  839. {
  840. SetLinkUrl("");
  841. }
  842. else
  843. {
  844. SetLinkUrl(linkUrl);
  845. }
  846. }
  847. }
  848. }
  849. }
  850. private void SetLinkUrl(string url)
  851. {
  852. PageWebTextContent = url;
  853. PageWebTextIsEnabled = true;
  854. }
  855. private void SetLinkEmail(string email)
  856. {
  857. PageMailTextContent = email;
  858. PageMailTextIsEnabled = true;
  859. }
  860. /// <summary>
  861. /// 设置页码的数据
  862. /// </summary>
  863. /// <param name="pageNum"></param>
  864. private void SetLinkPageNum(int pageNum)
  865. {
  866. if (pageNum > 0 && pageNum <= totalPage)
  867. {
  868. PageNumTextContent = pageNum.ToString();
  869. BtnLocationIsEnabled = true;
  870. PageNumTextIsEnabled = true;
  871. ImagePreviewVisibility = Visibility.Visible;
  872. SetImagePreview(pageNum - 1);
  873. //if (btnGOorBack != null)
  874. //{
  875. // ChangeBtnGOorBack("GO", btnGOorBack);
  876. //}
  877. }
  878. }
  879. /// <summary>
  880. /// 根据链接类型显示模块
  881. /// </summary>
  882. /// <param name="linkType"></param>
  883. /// <param name="isMail"></param>
  884. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  885. {
  886. BtnLinkPageIsChecked = false;
  887. BtnLinkMailIsChecked = false;
  888. BtnLinkWebIsChecked = false;
  889. switch (linkType)
  890. {
  891. case LINK_TYPE.GOTO:
  892. BtnLinkPageIsChecked = true;
  893. break;
  894. case LINK_TYPE.URI:
  895. if (this.isMail || isMail)
  896. {
  897. BtnLinkMailIsChecked = true;
  898. }
  899. else
  900. {
  901. BtnLinkWebIsChecked = true;
  902. }
  903. break;
  904. default:
  905. BtnLinkPageIsChecked = true;
  906. break;
  907. }
  908. }
  909. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  910. {
  911. this.totalPage = totalPage;
  912. PageNumTextContent = string.Empty;
  913. PageWebTextContent = string.Empty;
  914. PageMailTextContent = string.Empty;
  915. PageNumTextIsEnabled = enable;
  916. PageWebTextIsEnabled = enable;
  917. PageMailTextIsEnabled = enable;
  918. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  919. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  920. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  921. ShowPageNumTip = Visibility.Collapsed;
  922. ShowPageWebTip = Visibility.Collapsed;
  923. ShowPageMailTip = Visibility.Collapsed;
  924. BtnLocationIsEnabled = enable;
  925. }
  926. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  927. {
  928. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  929. {
  930. return null;
  931. }
  932. try
  933. {
  934. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  935. currentZoom = currentZoom * 96 / 72;
  936. int flag = 0;
  937. if (renderAnnot)
  938. {
  939. flag = 1;
  940. }
  941. uint bgColor = 0xFFFFFFFF;
  942. CPDFPage page = document.PageAtIndex(pageIndex);
  943. if (page.IsValid())
  944. {
  945. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  946. }
  947. return bmpData;
  948. }
  949. catch (Exception ex)
  950. {
  951. return null;
  952. }
  953. }
  954. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  955. {
  956. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  957. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  958. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  959. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  960. return boundRect;
  961. }
  962. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  963. {
  964. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  965. pageSize.Width = pageSize.Width / 72.0 * 96;
  966. pageSize.Height = pageSize.Height / 72.0 * 96;
  967. return pageSize;
  968. }
  969. }
  970. }