LinkAnnotPropertyViewModel.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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> BtnGOorBackPageCommand { get; set; }
  370. public DelegateCommand<object> LocationCommand { get; set; }
  371. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  372. #endregion 命令
  373. public LinkAnnotPropertyViewModel()
  374. {
  375. LoadedCommand = new DelegateCommand<object>(Loaded);
  376. PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
  377. PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
  378. BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
  379. LocationCommand = new DelegateCommand<object>(LocationPage);
  380. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  381. isLoaded = true;
  382. }
  383. /// <summary>
  384. /// 页码文本框 Enter键
  385. /// </summary>
  386. /// <param name="obj"></param>
  387. private void PageNumTextKeyDown(object obj)
  388. {
  389. if (obj is KeyEventArgs keyEventArgs)
  390. {
  391. if (keyEventArgs.Key == Key.Enter)
  392. {
  393. PageNumTextLostFocus(obj);
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// 页码文本框失去焦点
  399. /// </summary>
  400. /// <param name="obj"></param>
  401. private void PageNumTextLostFocus(object obj)
  402. {
  403. if (!string.IsNullOrEmpty(PageNumTextContent))
  404. {
  405. if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  406. {
  407. //isSelected = true;
  408. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  409. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  410. AnnotAttribEvent?.UpdateAnnot();
  411. //isTextChanged = false;
  412. SetImagePreview(LinkAnnotArgs.DestIndex);
  413. //ImagePreviewVisibility = Visibility.Collapsed;
  414. //BtnGOorBackVisibility= Visibility.Collapsed;
  415. BtnLocationIsEnabled = false;
  416. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  417. SelectAnnotation();
  418. }
  419. else
  420. {
  421. ImagePreviewVisibility = Visibility.Collapsed;
  422. BtnGOorBackVisibility = Visibility.Collapsed;
  423. isSelected = false;
  424. }
  425. }
  426. }
  427. /// <summary>
  428. /// 模块选择
  429. /// </summary>
  430. /// <param name="obj"></param>
  431. private void ToggleButtonTabSelected(object obj)
  432. {
  433. if (obj is RadioButton radioButton)
  434. {
  435. switch (radioButton.Tag)
  436. {
  437. case "Page":
  438. LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
  439. LinkAnnotArgs.URI = string.Empty;
  440. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  441. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  442. isMail = false;
  443. if (btnGOorBack != null)
  444. {
  445. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  446. {
  447. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  448. }
  449. else
  450. {
  451. ChangeBtnGOorBack("GO", btnGOorBack);
  452. }
  453. }
  454. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  455. break;
  456. case "Web":
  457. isMail = false;
  458. SetLinkType_URIState("Web");
  459. break;
  460. case "Mail":
  461. isMail = true;
  462. SetLinkType_URIState("Mail");
  463. break;
  464. }
  465. }
  466. }
  467. /// <summary>
  468. /// 设置BtnGOorBack状态
  469. /// </summary>
  470. /// <param name="uri"></param>
  471. private void SetLinkType_URIState(string uri)
  472. {
  473. LinkAnnotArgs.LinkType = LINK_TYPE.URI;
  474. LinkAnnotArgs.URI = string.Empty;
  475. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  476. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  477. if (btnGOorBack != null)
  478. {
  479. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  480. //ChangeBtnGOorBack("GO", btnGOorBack);
  481. switch (uri)
  482. {
  483. case "Web":
  484. if (!string.IsNullOrEmpty(PageWebTextContent))
  485. {
  486. ChangeBtnGOorBack("GO", btnGOorBack);
  487. }
  488. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  489. break;
  490. case "Mail":
  491. if (!string.IsNullOrEmpty(PageMailTextContent))
  492. {
  493. ChangeBtnGOorBack("GO", btnGOorBack);
  494. }
  495. BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  496. break;
  497. }
  498. }
  499. }
  500. /// <summary>
  501. /// 定位目标范围
  502. /// </summary>
  503. /// <param name="obj"></param>
  504. private void LocationPage(object obj)
  505. {
  506. pdfViewer.EnableDrawSelectArea(true);
  507. historyPageIndex = pdfViewer.CurrentIndex;
  508. PageNumTextIsEnabled = false;
  509. isSelected = false;
  510. viewContentViewModel.LinkAnnotTipVisibility=Visibility.Visible;
  511. }
  512. /// <summary>
  513. /// 前往/返回按钮
  514. /// </summary>
  515. /// <param name="obj"></param>
  516. private void BtnGOorBackPageEvent(object obj)
  517. {
  518. if (obj is object[] arry)
  519. {
  520. btnGOorBack = arry[0] as Button;
  521. RadioButton btnLinkPage = arry[1] as RadioButton;
  522. RadioButton btnLinkWeb = arry[2] as RadioButton;
  523. RadioButton btnLinkMail = arry[3] as RadioButton;
  524. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  525. {
  526. if (btnLinkPage.IsChecked == true)
  527. {
  528. LinkPageGoOrBack();
  529. }
  530. if (btnLinkWeb.IsChecked == true)
  531. {
  532. LinkWebSave();
  533. }
  534. if (btnLinkMail.IsChecked == true)
  535. {
  536. LinkMailSave();
  537. }
  538. }
  539. }
  540. }
  541. /// <summary>
  542. /// 保存邮件链接
  543. /// </summary>
  544. private void LinkMailSave()
  545. {
  546. if (CheckPageMailVaild(PageMailTextContent))
  547. {
  548. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  549. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  550. AnnotAttribEvent?.UpdateAnnot();
  551. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  552. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  553. }
  554. }
  555. /// <summary>
  556. /// 保存web链接
  557. /// </summary>
  558. private void LinkWebSave()
  559. {
  560. if (CheckPageWebVaild(PageWebTextContent))
  561. {
  562. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  563. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  564. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  565. {
  566. PageWebTextContent = "http://" + PageWebTextContent;
  567. }
  568. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  569. AnnotAttribEvent?.UpdateAnnot();
  570. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  571. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  572. {
  573. Process.Start(PageWebTextContent);
  574. }
  575. }
  576. else
  577. {
  578. BtnGOorBackVisibility = Visibility.Collapsed;
  579. }
  580. }
  581. /// <summary>
  582. /// 页码链接 GoOrBack
  583. /// </summary>
  584. private void LinkPageGoOrBack()
  585. {
  586. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  587. if (btnGOorBack.Tag.ToString() == "GO")
  588. {
  589. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  590. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  591. //AnnotAttribEvent?.UpdateAnnot();
  592. historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
  593. pdfViewer.GoToPage(pageIndex);
  594. ChangeBtnGOorBack("BACK", btnGOorBack);
  595. }
  596. else
  597. {
  598. if (historyPageIndex != -1)
  599. {
  600. SelectAnnotation();
  601. pdfViewer.GoToPage(historyPageIndex);
  602. ChangeBtnGOorBack("GO", btnGOorBack);
  603. }
  604. }
  605. }
  606. /// <summary>
  607. /// 选择注释
  608. /// </summary>
  609. private void SelectAnnotation()
  610. {
  611. if (linkAnnotArgs != null && !isSelected && linkAnnotArgs.AnnotIndex >= 0)
  612. {
  613. pdfViewer.SelectAnnotation(linkAnnotArgs.PageIndex, linkAnnotArgs.AnnotIndex);
  614. isSelected = true;
  615. }
  616. else
  617. {
  618. isSelected = false;
  619. }
  620. }
  621. /// <summary>
  622. /// 设置前往/返回按钮的文本
  623. /// </summary>
  624. /// <param name="flag"></param>
  625. /// <param name="button"></param>
  626. private void ChangeBtnGOorBack(string flag, Button button)
  627. {
  628. switch (flag)
  629. {
  630. case "BACK":
  631. button.Tag = "BACK";
  632. button.Content = "BACK";
  633. break;
  634. case "GO":
  635. button.Tag = "GO";
  636. button.Content = "GO";
  637. break;
  638. default:
  639. break;
  640. }
  641. }
  642. /// <summary>
  643. /// 页码格式验证
  644. /// </summary>
  645. /// <param name="pageNum"></param>
  646. /// <param name="text"></param>
  647. /// <returns></returns>
  648. private bool CheckPageNumVaild(out int pageNum, string text)
  649. {
  650. pageNum = -1;
  651. if (string.IsNullOrEmpty(text))
  652. {
  653. return false;
  654. }
  655. if (text.Trim() != string.Empty)
  656. {
  657. if (!int.TryParse(text.Trim(), out pageNum))
  658. {
  659. ShowPageNumTip = Visibility.Visible;
  660. PageNumTipText = " Page number error.";
  661. return false;
  662. }
  663. }
  664. if (pageNum < 1 || pageNum > totalPage)
  665. {
  666. ShowPageNumTip = Visibility.Visible;
  667. PageNumTipText = " Page number out of range.";
  668. return false;
  669. }
  670. else
  671. {
  672. ShowPageNumTip = Visibility.Collapsed;
  673. }
  674. //BtnGOorBackVisibility = Visibility.Visible;
  675. return true;
  676. }
  677. /// <summary>
  678. /// web链接格式验证
  679. /// </summary>
  680. /// <param name="text"></param>
  681. /// <returns></returns>
  682. private bool CheckPageWebVaild(string text)
  683. {
  684. BtnGOorBackVisibility = Visibility.Collapsed;
  685. if (string.IsNullOrEmpty(text))
  686. {
  687. return false;
  688. }
  689. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  690. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  691. {
  692. ShowPageWebTip = Visibility.Visible;
  693. PageWebTipText = "Invalid link.";
  694. return false;
  695. }
  696. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  697. if (matchText.Length != checkUrl.Length)
  698. {
  699. ShowPageWebTip = Visibility.Visible;
  700. PageWebTipText = "Invalid link.";
  701. return false;
  702. }
  703. else
  704. {
  705. ShowPageWebTip = Visibility.Collapsed;
  706. }
  707. BtnGOorBackVisibility = Visibility.Visible;
  708. return true;
  709. }
  710. /// <summary>
  711. /// 邮箱链接格式验证
  712. /// </summary>
  713. /// <param name="text"></param>
  714. /// <returns></returns>
  715. private bool CheckPageMailVaild(string text)
  716. {
  717. BtnGOorBackVisibility = Visibility.Collapsed;
  718. if (string.IsNullOrEmpty(text))
  719. {
  720. return false;
  721. }
  722. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  723. {
  724. ShowPageMailTip = Visibility.Visible;
  725. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  726. return false;
  727. }
  728. else
  729. {
  730. ShowPageMailTip = Visibility.Collapsed;
  731. }
  732. BtnGOorBackVisibility = Visibility.Visible;
  733. return true;
  734. }
  735. /// <summary>
  736. /// 设置图像
  737. /// </summary>
  738. /// <param name="pageIndex"></param>
  739. private void SetImagePreview(int pageIndex)
  740. {
  741. ImagePreviewVisibility = Visibility.Visible;
  742. BtnGOorBackVisibility = Visibility.Visible;
  743. int dpiHeight = (int)ImagePreviewHeight;
  744. int dpiWidth = (int)ImagePreviewWidth;
  745. if (dpiHeight <= 0 || dpiWidth <= 0)
  746. {
  747. return;
  748. }
  749. //int pageIndex = AnnotArgs.DestIndex;
  750. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  751. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  752. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  753. scale = Math.Min(scale, 1);
  754. int cropWidth = (int)(scale * pageSize.Width);
  755. int cropHeight = (int)(scale * pageSize.Height);
  756. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  757. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  758. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  759. PreviewImage = WirteBitmap;
  760. }
  761. /// <summary>
  762. /// 链接画框完成时触发事件
  763. /// </summary>
  764. /// <param name="sender"></param>
  765. /// <param name="e"></param>
  766. private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
  767. {
  768. SetTextBoxEnableOrNot(e, totalPage);
  769. }
  770. private void Loaded(object obj)
  771. {
  772. if (obj is CompositeCommandParameter composite)
  773. {
  774. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  775. {
  776. linkAnnot = linkAnnotProperty;
  777. }
  778. }
  779. }
  780. public bool IsNavigationTarget(NavigationContext navigationContext)
  781. {
  782. return true;
  783. }
  784. public void OnNavigatedFrom(NavigationContext navigationContext)
  785. {
  786. }
  787. public void OnNavigatedTo(NavigationContext navigationContext)
  788. {
  789. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanel);
  790. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  791. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  792. if (propertyPanel != null && pdfViewer != null)
  793. {
  794. pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  795. pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  796. AnnotAttribEvent = propertyPanel.AnnotEvent;
  797. AnnotType = propertyPanel.annot.EventType;
  798. document = pdfViewer.Document;
  799. totalPage = pdfViewer.Document.PageCount;
  800. SetTextBoxEnableOrNot(false, totalPage);
  801. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  802. {
  803. GetAnnotAttribute();
  804. }
  805. if (isLoaded)
  806. {
  807. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  808. {
  809. if (btnGOorBack != null)
  810. {
  811. ChangeBtnGOorBack("GO", btnGOorBack);
  812. }
  813. if (AnnotAttribEvent.IsAnnotCreateReset)
  814. {
  815. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  816. if (LinkAnnotArgs == null)
  817. {
  818. backPageIndex = pdfViewer.CurrentIndex;
  819. }
  820. else
  821. {
  822. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  823. }
  824. }
  825. }
  826. }
  827. if (LinkAnnotArgs == null)
  828. {
  829. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  830. }
  831. }
  832. }
  833. /// <summary>
  834. /// 定位目标范围完成时,触发
  835. /// </summary>
  836. /// <param name="sender"></param>
  837. /// <param name="e"></param>
  838. private async void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  839. {
  840. if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
  841. {
  842. if (e.NotifyData is AreaLocate areaInfo)
  843. {
  844. if (pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  845. {
  846. LinkAnnotArgs linkArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  847. System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
  848. linkArgs.IsPositionSet = true;
  849. linkArgs.DestIndex = areaInfo.PageIndex;
  850. linkArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  851. LinkAnnotArgs = linkArgs;
  852. LinkAnnotArgs.PageIndex = historyPageIndex;
  853. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  854. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
  855. AnnotAttribEvent?.UpdateAnnot();
  856. LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  857. PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
  858. SetImagePreview(areaInfo.PageIndex);
  859. await System.Threading.Tasks.Task.Delay(2);
  860. SelectAnnotation();
  861. pdfViewer.GoToPage(historyPageIndex);
  862. viewContentViewModel.LinkAnnotTipVisibility = Visibility.Collapsed;
  863. }
  864. }
  865. }
  866. }
  867. /// <summary>
  868. /// 选择链接后,显示参数
  869. /// </summary>
  870. private void GetAnnotAttribute()
  871. {
  872. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  873. {
  874. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  875. }
  876. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  877. {
  878. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  879. if (LinkAnnotArgs == null)
  880. {
  881. backPageIndex = pdfViewer.CurrentIndex;
  882. }
  883. else
  884. {
  885. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  886. }
  887. }
  888. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  889. {
  890. string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  891. if (!string.IsNullOrEmpty(linkUrl))
  892. {
  893. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  894. {
  895. SetLinkType(LINK_TYPE.URI, true);
  896. if (AnnotAttribEvent.IsAnnotCreateReset)
  897. {
  898. SetLinkEmail("");
  899. }
  900. else
  901. {
  902. SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  903. }
  904. }
  905. else
  906. {
  907. if (AnnotAttribEvent.IsAnnotCreateReset)
  908. {
  909. SetLinkUrl("");
  910. }
  911. else
  912. {
  913. SetLinkUrl(linkUrl);
  914. }
  915. }
  916. }
  917. }
  918. }
  919. private void SetLinkUrl(string url)
  920. {
  921. PageWebTextContent = url;
  922. PageWebTextIsEnabled = true;
  923. }
  924. private void SetLinkEmail(string email)
  925. {
  926. PageMailTextContent = email;
  927. PageMailTextIsEnabled = true;
  928. }
  929. /// <summary>
  930. /// 设置页码的数据
  931. /// </summary>
  932. /// <param name="pageNum"></param>
  933. private void SetLinkPageNum(int pageNum)
  934. {
  935. if (pageNum > 0 && pageNum <= totalPage)
  936. {
  937. PageNumTextContent = pageNum.ToString();
  938. BtnLocationIsEnabled = true;
  939. PageNumTextIsEnabled = true;
  940. ImagePreviewVisibility = Visibility.Visible;
  941. SetImagePreview(pageNum - 1);
  942. BtnLocationIsEnabled = true;
  943. //if (btnGOorBack != null)
  944. //{
  945. // ChangeBtnGOorBack("GO", btnGOorBack);
  946. //}
  947. }
  948. }
  949. /// <summary>
  950. /// 根据链接类型显示模块
  951. /// </summary>
  952. /// <param name="linkType"></param>
  953. /// <param name="isMail"></param>
  954. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  955. {
  956. BtnLinkPageIsChecked = false;
  957. BtnLinkMailIsChecked = false;
  958. BtnLinkWebIsChecked = false;
  959. switch (linkType)
  960. {
  961. case LINK_TYPE.GOTO:
  962. BtnLinkPageIsChecked = true;
  963. break;
  964. case LINK_TYPE.URI:
  965. if (this.isMail || isMail)
  966. {
  967. BtnLinkMailIsChecked = true;
  968. }
  969. else
  970. {
  971. BtnLinkWebIsChecked = true;
  972. }
  973. break;
  974. default:
  975. BtnLinkPageIsChecked = true;
  976. break;
  977. }
  978. }
  979. /// <summary>
  980. /// 设置文本框和按钮的属性
  981. /// </summary>
  982. /// <param name="enable"></param>
  983. /// <param name="totalPage"></param>
  984. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  985. {
  986. this.totalPage = totalPage;
  987. PageNumTextContent = string.Empty;
  988. PageWebTextContent = string.Empty;
  989. PageMailTextContent = string.Empty;
  990. PageNumTextIsEnabled = enable;
  991. PageWebTextIsEnabled = enable;
  992. PageMailTextIsEnabled = enable;
  993. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  994. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  995. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  996. ShowPageNumTip = Visibility.Collapsed;
  997. ShowPageWebTip = Visibility.Collapsed;
  998. ShowPageMailTip = Visibility.Collapsed;
  999. BtnLocationIsEnabled = enable;
  1000. }
  1001. /// <summary>
  1002. /// 将PDF的页面转换成数据流
  1003. /// </summary>
  1004. /// <param name="document"></param>
  1005. /// <param name="pageIndex"></param>
  1006. /// <param name="renderRect"></param>
  1007. /// <param name="currentZoom"></param>
  1008. /// <param name="renderAnnot"></param>
  1009. /// <param name="renderForm"></param>
  1010. /// <returns></returns>
  1011. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  1012. {
  1013. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  1014. {
  1015. return null;
  1016. }
  1017. try
  1018. {
  1019. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  1020. currentZoom = currentZoom * 96 / 72;
  1021. int flag = 0;
  1022. if (renderAnnot)
  1023. {
  1024. flag = 1;
  1025. }
  1026. uint bgColor = 0xFFFFFFFF;
  1027. CPDFPage page = document.PageAtIndex(pageIndex);
  1028. if (page.IsValid())
  1029. {
  1030. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  1031. }
  1032. return bmpData;
  1033. }
  1034. catch (Exception ex)
  1035. {
  1036. return null;
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// 获取页面的矩形
  1041. /// </summary>
  1042. /// <param name="document"></param>
  1043. /// <param name="pageIndex"></param>
  1044. /// <param name="displayBox"></param>
  1045. /// <returns></returns>
  1046. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  1047. {
  1048. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  1049. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  1050. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  1051. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  1052. return boundRect;
  1053. }
  1054. /// <summary>
  1055. /// 获取页面的尺寸
  1056. /// </summary>
  1057. /// <param name="document"></param>
  1058. /// <param name="pageIndex"></param>
  1059. /// <returns></returns>
  1060. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  1061. {
  1062. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  1063. pageSize.Width = pageSize.Width / 72.0 * 96;
  1064. pageSize.Height = pageSize.Height / 72.0 * 96;
  1065. return pageSize;
  1066. }
  1067. }
  1068. }