LinkAnnotPropertyViewModel.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Master.Model;
  6. using PDF_Master.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_Master.Helper;
  20. using Microsoft.Office.Interop.Word;
  21. using static Dropbox.Api.Files.FileCategory;
  22. using PDF_Master.Views.PropertyPanel.AnnotPanel;
  23. using System.Windows.Controls;
  24. using PDF_Master.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. using Prism.Events;
  34. using Prism.Services.Dialogs;
  35. using PDF_Master.Views.BOTA;
  36. using ImTools;
  37. using PDF_Master.ViewModels.Tools.AnnotManager;
  38. using PDF_Master.EventAggregators;
  39. using DryIoc;
  40. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  41. using ComPDFKit.Import;
  42. using ComPDFKit.PDFAnnotation;
  43. namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
  44. {
  45. internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
  46. {
  47. #region 文案
  48. private string T_title;
  49. public string T_Title
  50. {
  51. get { return T_title; }
  52. set
  53. {
  54. SetProperty(ref T_title, value);
  55. }
  56. }
  57. private string T_page;
  58. public string T_Page
  59. {
  60. get { return T_page; }
  61. set
  62. {
  63. SetProperty(ref T_page, value);
  64. }
  65. }
  66. private void InitString()
  67. {
  68. T_Title = App.MainPageLoader.GetString("Link_Title");
  69. T_Page = App.MainPageLoader.GetString("Link_Page");
  70. }
  71. #endregion 文案
  72. public AnnotAttribEvent AnnotAttribEvent { get; set; }
  73. private LinkAnnotArgs linkAnnotArgs;
  74. public LinkAnnotArgs LinkAnnotArgs
  75. {
  76. get
  77. {
  78. return linkAnnotArgs;
  79. }
  80. set
  81. {
  82. if (linkAnnotArgs != null)
  83. {
  84. linkAnnotArgs.LinkDrawFinished -= linkAnnotArgs_LinkDrawFinished;
  85. }
  86. linkAnnotArgs = value;
  87. if (linkAnnotArgs != null)
  88. {
  89. linkAnnotArgs.LinkDrawFinished += linkAnnotArgs_LinkDrawFinished;
  90. }
  91. }
  92. }
  93. private AnnotArgsType annotType;
  94. public AnnotArgsType AnnotType
  95. {
  96. get { return annotType; }
  97. set
  98. {
  99. SetProperty(ref annotType, value);
  100. }
  101. }
  102. #region 输入框的验证、设置
  103. private string pageNumTextContent;
  104. public string PageNumTextContent
  105. {
  106. get
  107. {
  108. if (CheckPageNumVaild(out int pageNum, pageNumTextContent))
  109. {
  110. if (LinkAnnotArgs != null)
  111. {
  112. LinkAnnotArgs.DestIndex = pageNum - 1;
  113. LinkAnnotArgs.Content = string.Format($"To Page {LinkAnnotArgs.DestIndex}");
  114. //isTextChanged = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] != AnnotArgs.DestIndex ? true : false;
  115. SetImagePreview(LinkAnnotArgs.DestIndex);
  116. }
  117. }
  118. else
  119. {
  120. ImagePreviewVisibility = Visibility.Collapsed;
  121. BtnGOorBackVisibility = Visibility.Collapsed;
  122. BtnLocationIsEnabled = false;
  123. }
  124. return pageNumTextContent;
  125. }
  126. set
  127. {
  128. SetProperty(ref pageNumTextContent, value);
  129. }
  130. }
  131. private string pageWebTextContent;
  132. public string PageWebTextContent
  133. {
  134. get
  135. {
  136. if (CheckPageWebVaild(pageWebTextContent))
  137. {
  138. pageWebTextContent = pageWebTextContent.Trim().ToLower();
  139. if (!pageWebTextContent.StartsWith("http://") && !pageWebTextContent.StartsWith("https://"))
  140. {
  141. pageWebTextContent = "http://" + pageWebTextContent;
  142. }
  143. if (LinkAnnotArgs != null)
  144. {
  145. LinkAnnotArgs.URI = pageWebTextContent;
  146. LinkAnnotArgs.Content = LinkAnnotArgs.URI;
  147. //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
  148. }
  149. }
  150. return pageWebTextContent;
  151. }
  152. set
  153. {
  154. SetProperty(ref pageWebTextContent, value);
  155. }
  156. }
  157. private string pageMailTextContent;
  158. public string PageMailTextContent
  159. {
  160. get
  161. {
  162. if (CheckPageMailVaild(pageMailTextContent))
  163. {
  164. if (!string.IsNullOrEmpty(pageMailTextContent) && LinkAnnotArgs != null)
  165. {
  166. LinkAnnotArgs.URI = "mailto:" + pageMailTextContent.Trim();
  167. LinkAnnotArgs.Content = LinkAnnotArgs.URI;
  168. //isTextChanged = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] != AnnotArgs.URI ? true : false;
  169. }
  170. }
  171. return pageMailTextContent;
  172. }
  173. set
  174. {
  175. SetProperty(ref pageMailTextContent, value);
  176. }
  177. }
  178. /// <summary>
  179. /// 设置预文本内容
  180. /// </summary>
  181. private string pageNumPlaceHoldText = "输入目标页面";
  182. public string PageNumPlaceHoldText
  183. {
  184. get { return pageNumPlaceHoldText; }
  185. set
  186. {
  187. SetProperty(ref pageNumPlaceHoldText, value);
  188. }
  189. }
  190. /// <summary>
  191. /// 是否显示输入框下方提示文案
  192. /// </summary>
  193. private Visibility showPageNumTip = Visibility.Collapsed;
  194. public Visibility ShowPageNumTip
  195. {
  196. get { return showPageNumTip; }
  197. set
  198. {
  199. SetProperty(ref showPageNumTip, value);
  200. }
  201. }
  202. /// <summary>
  203. /// 输入框下方提示文案
  204. /// </summary>
  205. private string pageNumTipText = "Page number error.";
  206. public string PageNumTipText
  207. {
  208. get { return pageNumTipText; }
  209. set
  210. {
  211. SetProperty(ref pageNumTipText, value);
  212. }
  213. }
  214. private bool pageNumTextIsEnabled = false;
  215. public bool PageNumTextIsEnabled
  216. {
  217. get { return pageNumTextIsEnabled; }
  218. set
  219. {
  220. SetProperty(ref pageNumTextIsEnabled, value);
  221. }
  222. }
  223. private bool pageWebTextIsEnabled = false;
  224. public bool PageWebTextIsEnabled
  225. {
  226. get { return pageWebTextIsEnabled; }
  227. set
  228. {
  229. SetProperty(ref pageWebTextIsEnabled, value);
  230. }
  231. }
  232. private string pageWebPlaceHoldText = "输入您要跳转的链接";
  233. public string PageWebPlaceHoldText
  234. {
  235. get { return pageWebPlaceHoldText; }
  236. set
  237. {
  238. SetProperty(ref pageWebPlaceHoldText, value);
  239. }
  240. }
  241. private Visibility showPageWebTip = Visibility.Collapsed;
  242. public Visibility ShowPageWebTip
  243. {
  244. get { return showPageWebTip; }
  245. set
  246. {
  247. SetProperty(ref showPageWebTip, value);
  248. }
  249. }
  250. private string pageWebTipText = "Invalid link.";
  251. public string PageWebTipText
  252. {
  253. get { return pageWebTipText; }
  254. set
  255. {
  256. SetProperty(ref pageWebTipText, value);
  257. }
  258. }
  259. private bool pageMailTextIsEnabled = false;
  260. public bool PageMailTextIsEnabled
  261. {
  262. get { return pageMailTextIsEnabled; }
  263. set
  264. {
  265. SetProperty(ref pageMailTextIsEnabled, value);
  266. }
  267. }
  268. private string pageMailPlaceHoldText = "输入您要跳转邮件地址";
  269. public string PageMailPlaceHoldText
  270. {
  271. get { return pageMailPlaceHoldText; }
  272. set
  273. {
  274. SetProperty(ref pageMailPlaceHoldText, value);
  275. }
  276. }
  277. private Visibility showPageMailTip = Visibility.Collapsed;
  278. public Visibility ShowPageMailTip
  279. {
  280. get { return showPageMailTip; }
  281. set
  282. {
  283. SetProperty(ref showPageMailTip, value);
  284. }
  285. }
  286. private string pageMailTipText = "The email is invalid.Please enter a valid email address.";
  287. public string PageMailTipText
  288. {
  289. get { return pageMailTipText; }
  290. set
  291. {
  292. SetProperty(ref pageMailTipText, value);
  293. }
  294. }
  295. #endregion 输入框的验证、设置
  296. #region 按钮、图片的设置
  297. private bool btnLocationIsEnabled = false;
  298. public bool BtnLocationIsEnabled
  299. {
  300. get { return btnLocationIsEnabled; }
  301. set
  302. {
  303. SetProperty(ref btnLocationIsEnabled, value);
  304. }
  305. }
  306. private bool btnLocationIsChecked = false;
  307. public bool BtnLocationIsChecked
  308. {
  309. get { return btnLocationIsChecked; }
  310. set
  311. {
  312. SetProperty(ref btnLocationIsChecked, value);
  313. }
  314. }
  315. private WriteableBitmap previewImage;
  316. public WriteableBitmap PreviewImage
  317. {
  318. get { return previewImage; }
  319. set
  320. {
  321. SetProperty(ref previewImage, value);
  322. }
  323. }
  324. private int imagePreviewWidth = 196;
  325. public int ImagePreviewWidth
  326. {
  327. get { return imagePreviewWidth; }
  328. set
  329. {
  330. SetProperty(ref imagePreviewWidth, value);
  331. }
  332. }
  333. private int imagePreviewHeight = 256;
  334. public int ImagePreviewHeight
  335. {
  336. get { return imagePreviewHeight; }
  337. set
  338. {
  339. SetProperty(ref imagePreviewHeight, value);
  340. }
  341. }
  342. private Visibility imagePreviewVisibility = Visibility.Collapsed;
  343. public Visibility ImagePreviewVisibility
  344. {
  345. get { return imagePreviewVisibility; }
  346. set
  347. {
  348. SetProperty(ref imagePreviewVisibility, value);
  349. }
  350. }
  351. private Visibility btnGOorBackVisibility = Visibility.Collapsed;
  352. public Visibility BtnGOorBackVisibility
  353. {
  354. get { return btnGOorBackVisibility; }
  355. set
  356. {
  357. SetProperty(ref btnGOorBackVisibility, value);
  358. }
  359. }
  360. private string btnGOorBackContent = "GO";
  361. public string BtnGOorBackContent
  362. {
  363. get { return btnGOorBackContent; }
  364. set
  365. {
  366. SetProperty(ref btnGOorBackContent, value);
  367. }
  368. }
  369. private bool btnLinkPageIsChecked = true;
  370. public bool BtnLinkPageIsChecked
  371. {
  372. get { return btnLinkPageIsChecked; }
  373. set
  374. {
  375. SetProperty(ref btnLinkPageIsChecked, value);
  376. }
  377. }
  378. private bool btnLinkWebIsChecked = false;
  379. public bool BtnLinkWebIsChecked
  380. {
  381. get { return btnLinkWebIsChecked; }
  382. set
  383. {
  384. SetProperty(ref btnLinkWebIsChecked, value);
  385. }
  386. }
  387. private bool btnLinkMailIsChecked = false;
  388. public bool BtnLinkMailIsChecked
  389. {
  390. get { return btnLinkMailIsChecked; }
  391. set
  392. {
  393. SetProperty(ref btnLinkMailIsChecked, value);
  394. }
  395. }
  396. #endregion 按钮、图片的设置
  397. private bool isLoaded = false;
  398. private bool isMail = false;
  399. private bool isSelected = false;
  400. private bool IsAnnotSelect = true;
  401. private string historyBtnGOorBackTag = string.Empty;
  402. private int totalPage = 0;
  403. private int backPageIndex = 0;
  404. private int historyPageIndex = 0;
  405. private CPDFViewer pdfViewer;
  406. private CPDFDocument document;
  407. private Button btnGOorBack = null;
  408. private LinkAnnotProperty linkAnnot;
  409. private AnnotTransfer propertyPanel;
  410. private ViewContentViewModel viewContentViewModel;
  411. public IRegionManager region;
  412. public IDialogService dialogs;
  413. public IEventAggregator events;
  414. #region 命令
  415. public DelegateCommand<object> LoadedCommand { get; set; }
  416. public DelegateCommand<object> PageNumTextLostFocusCommand { get; set; }
  417. public DelegateCommand<object> PageNumTextKeyDownCommand { get; set; }
  418. public DelegateCommand<object> PageMailTextLostFocusCommand { get; set; }
  419. public DelegateCommand<object> PageMailTextKeyDownCommand { get; set; }
  420. public DelegateCommand<object> PageWebTextLostFocusCommand { get; set; }
  421. public DelegateCommand<object> PageWebTextKeyDownCommand { get; set; }
  422. public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
  423. public DelegateCommand<object> LocationCommand { get; set; }
  424. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  425. #endregion 命令
  426. public LinkAnnotPropertyViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  427. {
  428. region = regionManager;
  429. dialogs = dialogService;
  430. events = eventAggregator;
  431. LoadedCommand = new DelegateCommand<object>(Loaded);
  432. PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
  433. PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
  434. PageMailTextLostFocusCommand = new DelegateCommand<object>(PageMailTextLostFocus);
  435. PageMailTextKeyDownCommand = new DelegateCommand<object>(PageMailTextKeyDown);
  436. PageWebTextLostFocusCommand = new DelegateCommand<object>(PageWebTextLostFocus);
  437. PageWebTextKeyDownCommand = new DelegateCommand<object>(PageWebTextKeyDown);
  438. BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
  439. LocationCommand = new DelegateCommand<object>(LocationPage);
  440. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  441. isLoaded = true;
  442. InitString();
  443. }
  444. private void PageWebTextKeyDown(object obj)
  445. {
  446. if (obj is KeyEventArgs keyEventArgs)
  447. {
  448. if (keyEventArgs.Key == Key.Enter)
  449. {
  450. PageWebTextLostFocus(obj);
  451. }
  452. }
  453. }
  454. private void PageWebTextLostFocus(object obj)
  455. {
  456. if (!string.IsNullOrEmpty(PageWebTextContent))
  457. {
  458. if (CheckPageWebVaild(PageWebTextContent))
  459. {
  460. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  461. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  462. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  463. {
  464. PageWebTextContent = "http://" + PageWebTextContent;
  465. }
  466. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  467. AnnotAttribEvent?.UpdateAnnot();
  468. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  469. }
  470. }
  471. }
  472. private void PageMailTextKeyDown(object obj)
  473. {
  474. if (obj is KeyEventArgs keyEventArgs)
  475. {
  476. if (keyEventArgs.Key == Key.Enter)
  477. {
  478. PageMailTextLostFocus(obj);
  479. }
  480. }
  481. }
  482. private void PageMailTextLostFocus(object obj)
  483. {
  484. if (!string.IsNullOrEmpty(PageMailTextContent))
  485. {
  486. if (CheckPageMailVaild(PageMailTextContent))
  487. {
  488. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  489. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  490. AnnotAttribEvent?.UpdateAnnot();
  491. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  492. }
  493. }
  494. }
  495. /// <summary>
  496. /// 页码文本框 Enter键
  497. /// </summary>
  498. /// <param name="obj"></param>
  499. private void PageNumTextKeyDown(object obj)
  500. {
  501. if (obj is KeyEventArgs keyEventArgs)
  502. {
  503. if (keyEventArgs.Key == Key.Enter)
  504. {
  505. PageNumTextLostFocus(obj);
  506. }
  507. }
  508. }
  509. /// <summary>
  510. /// 页码文本框失去焦点
  511. /// </summary>
  512. /// <param name="obj"></param>
  513. private void PageNumTextLostFocus(object obj)
  514. {
  515. if (!string.IsNullOrEmpty(PageNumTextContent))
  516. {
  517. if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  518. {
  519. //isSelected = true;
  520. historyPageIndex = LinkAnnotArgs.PageIndex;
  521. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  522. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  523. AnnotAttribEvent?.UpdateAnnot();
  524. //isTextChanged = false;
  525. SetImagePreview(LinkAnnotArgs.DestIndex);
  526. //ImagePreviewVisibility = Visibility.Collapsed;
  527. //BtnGOorBackVisibility= Visibility.Collapsed;
  528. BtnLocationIsEnabled = false;
  529. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  530. SelectAnnotation();
  531. }
  532. else
  533. {
  534. BtnLocationIsEnabled = true;
  535. ImagePreviewVisibility = Visibility.Collapsed;
  536. BtnGOorBackVisibility = Visibility.Collapsed;
  537. isSelected = false;
  538. }
  539. }
  540. else
  541. {
  542. BtnLocationIsEnabled = true;
  543. }
  544. }
  545. /// <summary>
  546. /// 模块选择
  547. /// </summary>
  548. /// <param name="obj"></param>
  549. private void ToggleButtonTabSelected(object obj)
  550. {
  551. if (obj is RadioButton radioButton)
  552. {
  553. switch (radioButton.Tag)
  554. {
  555. case "Page":
  556. LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
  557. LinkAnnotArgs.URI = string.Empty;
  558. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  559. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  560. isMail = false;
  561. if (btnGOorBack != null)
  562. {
  563. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  564. {
  565. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  566. }
  567. else
  568. {
  569. ChangeBtnGOorBack("GO", btnGOorBack);
  570. }
  571. }
  572. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  573. break;
  574. case "Web":
  575. isMail = false;
  576. SetLinkType_URIState("Web");
  577. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  578. break;
  579. case "Mail":
  580. isMail = true;
  581. SetLinkType_URIState("Mail");
  582. BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  583. break;
  584. }
  585. }
  586. }
  587. /// <summary>
  588. /// 设置BtnGOorBack状态
  589. /// </summary>
  590. /// <param name="uri"></param>
  591. private void SetLinkType_URIState(string uri)
  592. {
  593. LinkAnnotArgs.LinkType = LINK_TYPE.URI;
  594. LinkAnnotArgs.URI = string.Empty;
  595. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  596. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  597. if (btnGOorBack != null)
  598. {
  599. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  600. //ChangeBtnGOorBack("GO", btnGOorBack);
  601. switch (uri)
  602. {
  603. case "Web":
  604. if (!string.IsNullOrEmpty(PageWebTextContent))
  605. {
  606. ChangeBtnGOorBack("GO", btnGOorBack);
  607. }
  608. //BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  609. break;
  610. case "Mail":
  611. if (!string.IsNullOrEmpty(PageMailTextContent))
  612. {
  613. ChangeBtnGOorBack("GO", btnGOorBack);
  614. }
  615. //BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  616. break;
  617. }
  618. }
  619. }
  620. /// <summary>
  621. /// 定位目标范围
  622. /// </summary>
  623. /// <param name="obj"></param>
  624. private void LocationPage(object obj)
  625. {
  626. //没有选择注释工具、也不是右键添加注释
  627. //在阅读页,选择注释后,点击按钮
  628. //需要设置相关参数
  629. //if (AnnotAttribEvent.IsAnnotCreateReset == false && propertyPanel.IsAddLink == false)
  630. //{
  631. // propertyPanel.IsLocationLink = true;
  632. // pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  633. // pdfViewer.SetToolParam(LinkAnnotArgs);
  634. //}
  635. historyPageIndex = pdfViewer.CurrentIndex;
  636. isSelected = false;
  637. BtnLocationIsChecked = true;
  638. if (region.Regions.ContainsRegionWithName(viewContentViewModel.TipContentRegionName))
  639. {
  640. var views = region.Regions[viewContentViewModel.TipContentRegionName].Views;
  641. var isHas = views.FindFirst(q => q is Views.TipContent.LinkAnnotTip);
  642. if (isHas != null)
  643. {
  644. region.Regions[viewContentViewModel.TipContentRegionName].Activate(isHas);
  645. }
  646. else
  647. {
  648. region.RequestNavigate(viewContentViewModel.TipContentRegionName, "LinkAnnotTip");
  649. }
  650. }
  651. if (BtnLocationIsChecked && viewContentViewModel.TipVisible == Visibility.Visible)
  652. {
  653. pdfViewer.EnableDrawSelectArea(false);
  654. viewContentViewModel.ShowTip(false);
  655. BtnLocationIsChecked = false;
  656. PageNumTextIsEnabled = true;
  657. }
  658. else
  659. {
  660. pdfViewer.EnableDrawSelectArea(true);
  661. viewContentViewModel.ShowTip(true);
  662. PageNumTextIsEnabled = false;
  663. //BtnLocationIsChecked = true;
  664. }
  665. //AnnotAttribEvent.IsAnnotCreateReset = true;
  666. }
  667. private Rect paintrect;
  668. /// <summary>
  669. /// 定位目标范围完成时,触发
  670. /// </summary>
  671. /// <param name="sender"></param>
  672. /// <param name="e"></param>
  673. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  674. {
  675. if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
  676. {
  677. if (e.NotifyData is AreaLocate areaInfo)
  678. {
  679. if (pdfViewer.ToolManager.CurrentAnnotArgs == null)
  680. {
  681. CleanLocationState();
  682. return;
  683. }
  684. if (pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  685. {
  686. System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
  687. historyPageIndex = LinkAnnotArgs.PageIndex;
  688. AnnotAttribEvent = propertyPanel.AnnotEvent;
  689. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  690. LinkAnnotArgs.IsPositionSet = true;
  691. LinkAnnotArgs.DestIndex = areaInfo.PageIndex;
  692. LinkAnnotArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  693. paintrect = LinkAnnotArgs.GetLinkCreateArea();
  694. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  695. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
  696. AnnotAttribEvent?.UpdateAnnot();
  697. LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  698. pdfViewer.GoToPage(historyPageIndex);
  699. BackToPage(historyPageIndex, areaInfo.PageIndex);
  700. //if (string.IsNullOrEmpty(PageNumTextContent))
  701. //{
  702. pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  703. //}
  704. BtnLocationIsChecked = false;
  705. PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
  706. SetImagePreview(areaInfo.PageIndex);
  707. viewContentViewModel.TipVisible = Visibility.Collapsed;
  708. //没有选择注释工具、也不是右键添加注释
  709. //在阅读页,选择注释后,点击按钮
  710. //需要设置相关参数
  711. //if (propertyPanel.IsLocationLink)
  712. //{
  713. // pdfViewer.SetMouseMode(MouseModes.PanTool);
  714. // propertyPanel.IsLocationLink = false;
  715. // pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  716. //}
  717. }
  718. }
  719. }
  720. }
  721. private void CleanLocationState()
  722. {
  723. viewContentViewModel.TipVisible = Visibility.Collapsed;
  724. BtnLocationIsChecked = false;
  725. propertyPanel.IsLocationLink = false;
  726. //没有选择注释工具、也不是右键添加注释
  727. //在阅读页,选择注释后,点击按钮
  728. //需要设置相关参数
  729. if (propertyPanel.IsAddLink == false)
  730. {
  731. pdfViewer.SetMouseMode(MouseModes.PanTool);
  732. }
  733. }
  734. /// <summary>
  735. /// 前往/返回按钮
  736. /// </summary>
  737. /// <param name="obj"></param>
  738. private void BtnGOorBackPageEvent(object obj)
  739. {
  740. if (obj is object[] arry)
  741. {
  742. btnGOorBack = arry[0] as Button;
  743. RadioButton btnLinkPage = arry[1] as RadioButton;
  744. RadioButton btnLinkWeb = arry[2] as RadioButton;
  745. RadioButton btnLinkMail = arry[3] as RadioButton;
  746. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  747. {
  748. if (btnLinkPage.IsChecked == true)
  749. {
  750. LinkPageGoOrBack();
  751. }
  752. if (btnLinkWeb.IsChecked == true)
  753. {
  754. LinkWebSave();
  755. }
  756. if (btnLinkMail.IsChecked == true)
  757. {
  758. LinkMailSave();
  759. }
  760. }
  761. }
  762. }
  763. /// <summary>
  764. /// 保存邮件链接
  765. /// </summary>
  766. private void LinkMailSave()
  767. {
  768. if (CheckPageMailVaild(PageMailTextContent))
  769. {
  770. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  771. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  772. //AnnotAttribEvent?.UpdateAnnot();
  773. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  774. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  775. }
  776. }
  777. /// <summary>
  778. /// 保存web链接
  779. /// </summary>
  780. private void LinkWebSave()
  781. {
  782. if (CheckPageWebVaild(PageWebTextContent))
  783. {
  784. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  785. //PageWebTextContent = PageWebTextContent.Trim().ToLower();
  786. //if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  787. //{
  788. // PageWebTextContent = "http://" + PageWebTextContent;
  789. //}
  790. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  791. //AnnotAttribEvent?.UpdateAnnot();
  792. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  793. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  794. {
  795. Process.Start(PageWebTextContent);
  796. }
  797. }
  798. else
  799. {
  800. BtnGOorBackVisibility = Visibility.Collapsed;
  801. }
  802. }
  803. /// <summary>
  804. /// 页码链接 GoOrBack
  805. /// </summary>
  806. private void LinkPageGoOrBack()
  807. {
  808. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  809. ////注释位置
  810. //Rect rect = LinkAnnotArgs.GetLinkPaintArea();
  811. ////定位
  812. //Rect paintrect = LinkAnnotArgs.GetLinkCreateArea();
  813. //double height = 0;
  814. //CPDFDocument doc = pdfViewer.Document;
  815. //CPDFPage docPage;
  816. //double zoom = pdfViewer.ZoomFactor;
  817. if (btnGOorBack.Tag.ToString() == "GO")
  818. {
  819. pdfViewer.GoToPage(pageIndex);
  820. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  821. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  822. //AnnotAttribEvent?.UpdateAnnot();
  823. historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
  824. GoToPage(pageIndex);
  825. ChangeBtnGOorBack("BACK", btnGOorBack);
  826. }
  827. else
  828. {
  829. if (historyPageIndex != -1)
  830. {
  831. pdfViewer.GoToPage(historyPageIndex);
  832. SelectAnnotation();
  833. BackToPage(historyPageIndex, pageIndex);
  834. ChangeBtnGOorBack("GO", btnGOorBack);
  835. //pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  836. }
  837. }
  838. }
  839. private void BackToPage(int historyPageIndex, int pageIndex)
  840. {
  841. CPDFLinkAnnotation cPDFLink = LinkAnnotArgs.GetLinkAnnot();
  842. if (cPDFLink == null)
  843. {
  844. return;
  845. }
  846. //注释位置
  847. Rect rect = LinkAnnotArgs.GetLinkPaintArea();
  848. //定位
  849. Rect paintrect = LinkAnnotArgs.GetLinkCreateArea();
  850. double height = 0;
  851. CPDFDocument doc = pdfViewer.Document;
  852. CPDFPage docPage;
  853. double zoom = pdfViewer.ZoomFactor;
  854. docPage = doc.PageAtIndex(historyPageIndex, false);
  855. if (!rect.IsEmpty)
  856. {
  857. double vertical = pdfViewer.GetVerticalOffset();
  858. if (docPage.PageSize.Height / 2 < rect.Top)
  859. {
  860. if (historyPageIndex < pageIndex)
  861. {
  862. height = vertical + (rect.Bottom * zoom);
  863. pdfViewer.ScrollToVerticalOffset(height);
  864. }
  865. else
  866. {
  867. height = vertical + (rect.Top * zoom);
  868. pdfViewer.ScrollToVerticalOffset(height);
  869. }
  870. }
  871. }
  872. }
  873. private void GoToPage(int pageIndex)
  874. {
  875. CPDFLinkAnnotation cPDFLink = LinkAnnotArgs.GetLinkAnnot();
  876. if (cPDFLink == null)
  877. {
  878. return;
  879. }
  880. //定位
  881. paintrect = LinkAnnotArgs.GetLinkCreateArea();
  882. double height = 0;
  883. CPDFDocument doc = pdfViewer.Document;
  884. CPDFPage docPage;
  885. double zoom = pdfViewer.ZoomFactor;
  886. docPage = doc.PageAtIndex(pageIndex, false);
  887. if (!paintrect.IsEmpty)
  888. {
  889. double vertical = pdfViewer.GetVerticalOffset();
  890. if (docPage.PageSize.Height / 2 < paintrect.Top)
  891. {
  892. if (historyPageIndex < pageIndex)
  893. {
  894. height = vertical + (paintrect.Bottom * zoom);
  895. pdfViewer.ScrollToVerticalOffset(height);
  896. }
  897. else
  898. {
  899. height = vertical + (paintrect.Top * zoom);
  900. pdfViewer.ScrollToVerticalOffset(height);
  901. }
  902. }
  903. //height = vertical + (paintrect.Bottom * zoom);
  904. }
  905. }
  906. /// <summary>
  907. /// 选择注释
  908. /// </summary>
  909. private void SelectAnnotation()
  910. {
  911. if (LinkAnnotArgs != null && !isSelected && LinkAnnotArgs.AnnotIndex >= 0)
  912. {
  913. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  914. isSelected = true;
  915. }
  916. else
  917. {
  918. isSelected = false;
  919. }
  920. }
  921. /// <summary>
  922. /// 设置前往/返回按钮的文本
  923. /// </summary>
  924. /// <param name="flag"></param>
  925. /// <param name="button"></param>
  926. private void ChangeBtnGOorBack(string flag, Button button)
  927. {
  928. switch (flag)
  929. {
  930. case "BACK":
  931. button.Tag = "BACK";
  932. button.Content = "BACK";
  933. break;
  934. case "GO":
  935. button.Tag = "GO";
  936. button.Content = "GO";
  937. break;
  938. default:
  939. break;
  940. }
  941. }
  942. /// <summary>
  943. /// 页码格式验证
  944. /// </summary>
  945. /// <param name="pageNum"></param>
  946. /// <param name="text"></param>
  947. /// <returns></returns>
  948. private bool CheckPageNumVaild(out int pageNum, string text)
  949. {
  950. pageNum = -1;
  951. if (string.IsNullOrEmpty(text))
  952. {
  953. return false;
  954. }
  955. if (text.Trim() != string.Empty)
  956. {
  957. if (!int.TryParse(text.Trim(), out pageNum))
  958. {
  959. ShowPageNumTip = Visibility.Visible;
  960. PageNumTipText = " Page number error.";
  961. return false;
  962. }
  963. }
  964. if (pageNum < 1 || pageNum > totalPage)
  965. {
  966. ShowPageNumTip = Visibility.Visible;
  967. PageNumTipText = " Page number out of range.";
  968. return false;
  969. }
  970. else
  971. {
  972. ShowPageNumTip = Visibility.Collapsed;
  973. }
  974. //BtnGOorBackVisibility = Visibility.Visible;
  975. return true;
  976. }
  977. /// <summary>
  978. /// web链接格式验证
  979. /// </summary>
  980. /// <param name="text"></param>
  981. /// <returns></returns>
  982. private bool CheckPageWebVaild(string text)
  983. {
  984. BtnGOorBackVisibility = Visibility.Collapsed;
  985. if (string.IsNullOrEmpty(text))
  986. {
  987. return false;
  988. }
  989. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  990. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  991. {
  992. ShowPageWebTip = Visibility.Visible;
  993. PageWebTipText = "Invalid link.";
  994. return false;
  995. }
  996. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  997. if (matchText.Length != checkUrl.Length)
  998. {
  999. ShowPageWebTip = Visibility.Visible;
  1000. PageWebTipText = "Invalid link.";
  1001. return false;
  1002. }
  1003. else
  1004. {
  1005. ShowPageWebTip = Visibility.Collapsed;
  1006. }
  1007. BtnGOorBackVisibility = Visibility.Visible;
  1008. return true;
  1009. }
  1010. /// <summary>
  1011. /// 邮箱链接格式验证
  1012. /// </summary>
  1013. /// <param name="text"></param>
  1014. /// <returns></returns>
  1015. private bool CheckPageMailVaild(string text)
  1016. {
  1017. BtnGOorBackVisibility = Visibility.Collapsed;
  1018. if (string.IsNullOrEmpty(text))
  1019. {
  1020. return false;
  1021. }
  1022. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  1023. {
  1024. ShowPageMailTip = Visibility.Visible;
  1025. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  1026. return false;
  1027. }
  1028. else
  1029. {
  1030. ShowPageMailTip = Visibility.Collapsed;
  1031. }
  1032. BtnGOorBackVisibility = Visibility.Visible;
  1033. return true;
  1034. }
  1035. /// <summary>
  1036. /// 设置图像
  1037. /// </summary>
  1038. /// <param name="pageIndex"></param>
  1039. private void SetImagePreview(int pageIndex)
  1040. {
  1041. ImagePreviewVisibility = Visibility.Visible;
  1042. BtnGOorBackVisibility = Visibility.Visible;
  1043. BtnLocationIsEnabled = true;
  1044. int dpiHeight = (int)ImagePreviewHeight;
  1045. int dpiWidth = (int)ImagePreviewWidth;
  1046. if (dpiHeight <= 0 || dpiWidth <= 0)
  1047. {
  1048. return;
  1049. }
  1050. //int pageIndex = AnnotArgs.DestIndex;
  1051. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  1052. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  1053. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  1054. scale = Math.Min(scale, 1);
  1055. int cropWidth = (int)(scale * pageSize.Width);
  1056. int cropHeight = (int)(scale * pageSize.Height);
  1057. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  1058. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  1059. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  1060. PreviewImage = WirteBitmap;
  1061. }
  1062. /// <summary>
  1063. /// 链接画框完成时触发事件
  1064. /// </summary>
  1065. /// <param name="sender"></param>
  1066. /// <param name="e"></param>
  1067. private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
  1068. {
  1069. SetTextBoxEnableOrNot(e, totalPage);
  1070. }
  1071. private void Loaded(object obj)
  1072. {
  1073. if (obj is CompositeCommandParameter composite)
  1074. {
  1075. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  1076. {
  1077. linkAnnot = linkAnnotProperty;
  1078. }
  1079. }
  1080. }
  1081. public bool IsNavigationTarget(NavigationContext navigationContext)
  1082. {
  1083. return true;
  1084. }
  1085. public void OnNavigatedFrom(NavigationContext navigationContext)
  1086. {
  1087. }
  1088. public void OnNavigatedTo(NavigationContext navigationContext)
  1089. {
  1090. navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out propertyPanel);
  1091. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  1092. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  1093. events.GetEvent<LinkAnnotTipCloseEvent>().Subscribe(LinkAnnotTipClose, e => e.UniCode == App.mainWindowViewModel.SelectedItem.Unicode);
  1094. if (propertyPanel != null && pdfViewer != null)
  1095. {
  1096. pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  1097. pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  1098. AnnotAttribEvent = propertyPanel.AnnotEvent;
  1099. AnnotType = propertyPanel.annot.EventType;
  1100. document = pdfViewer.Document;
  1101. totalPage = pdfViewer.Document.PageCount;
  1102. if (viewContentViewModel.IsRightMenuCreateLink)
  1103. {
  1104. SetTextBoxEnableOrNot(true, totalPage);
  1105. }
  1106. else
  1107. {
  1108. SetTextBoxEnableOrNot(false, totalPage);
  1109. }
  1110. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  1111. {
  1112. //根据选择的,链接,获取对应的信息
  1113. GetAnnotAttribute();
  1114. }
  1115. if (isLoaded)
  1116. {
  1117. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  1118. {
  1119. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1120. if (LinkAnnotArgs.AnnotIndex == -1)
  1121. {
  1122. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1123. }
  1124. if (btnGOorBack != null)
  1125. {
  1126. ChangeBtnGOorBack("GO", btnGOorBack);
  1127. }
  1128. if (AnnotAttribEvent.IsAnnotCreateReset)
  1129. {
  1130. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1131. if (LinkAnnotArgs == null)
  1132. {
  1133. backPageIndex = pdfViewer.CurrentIndex;
  1134. }
  1135. else
  1136. {
  1137. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. if (LinkAnnotArgs == null)
  1143. {
  1144. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1145. }
  1146. }
  1147. }
  1148. private void LinkAnnotTipClose(LinkAnnotTipCloseArgs obj)
  1149. {
  1150. pdfViewer.EnableDrawSelectArea(false);
  1151. viewContentViewModel.ShowTip(false);
  1152. BtnLocationIsChecked = false;
  1153. //PageNumTextIsEnabled = true;
  1154. }
  1155. /// <summary>
  1156. /// 选择链接后,显示参数
  1157. /// </summary>
  1158. private void GetAnnotAttribute()
  1159. {
  1160. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  1161. {
  1162. //设置显示的链接模块
  1163. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  1164. }
  1165. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  1166. {
  1167. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  1168. if (LinkAnnotArgs == null)
  1169. {
  1170. backPageIndex = pdfViewer.CurrentIndex;
  1171. }
  1172. else
  1173. {
  1174. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1175. }
  1176. }
  1177. //暂定 第一版 不显示邮箱、web链接模块
  1178. //if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  1179. //{
  1180. // string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  1181. // if (!string.IsNullOrEmpty(linkUrl))
  1182. // {
  1183. // if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  1184. // {
  1185. // SetLinkType(LINK_TYPE.URI, true);
  1186. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1187. // {
  1188. // SetLinkEmail("");
  1189. // }
  1190. // else
  1191. // {
  1192. // SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  1193. // }
  1194. // }
  1195. // else
  1196. // {
  1197. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1198. // {
  1199. // SetLinkUrl("");
  1200. // }
  1201. // else
  1202. // {
  1203. // SetLinkUrl(linkUrl);
  1204. // }
  1205. // }
  1206. // }
  1207. //}
  1208. }
  1209. private void SetLinkUrl(string url)
  1210. {
  1211. PageWebTextContent = url;
  1212. PageWebTextIsEnabled = true;
  1213. }
  1214. private void SetLinkEmail(string email)
  1215. {
  1216. PageMailTextContent = email;
  1217. PageMailTextIsEnabled = true;
  1218. }
  1219. /// <summary>
  1220. /// 设置页码的数据
  1221. /// </summary>
  1222. /// <param name="pageNum"></param>
  1223. private void SetLinkPageNum(int pageNum)
  1224. {
  1225. if (pageNum > 0 && pageNum <= totalPage)
  1226. {
  1227. PageNumTextContent = pageNum.ToString();
  1228. BtnLocationIsEnabled = true;
  1229. PageNumTextIsEnabled = true;
  1230. ImagePreviewVisibility = Visibility.Visible;
  1231. SetImagePreview(pageNum - 1);
  1232. BtnLocationIsEnabled = true;
  1233. //if (btnGOorBack != null)
  1234. //{
  1235. // ChangeBtnGOorBack("GO", btnGOorBack);
  1236. //}
  1237. }
  1238. }
  1239. /// <summary>
  1240. /// 根据链接类型显示模块
  1241. /// </summary>
  1242. /// <param name="linkType"></param>
  1243. /// <param name="isMail"></param>
  1244. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  1245. {
  1246. BtnLinkPageIsChecked = false;
  1247. BtnLinkMailIsChecked = false;
  1248. BtnLinkWebIsChecked = false;
  1249. //暂定 第一版 不显示邮箱、web链接模块
  1250. if (linkType == LINK_TYPE.URI)
  1251. {
  1252. BtnLinkPageIsChecked = true;
  1253. BtnGOorBackVisibility = Visibility.Collapsed;
  1254. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1255. return;
  1256. }
  1257. switch (linkType)
  1258. {
  1259. case LINK_TYPE.GOTO:
  1260. BtnLinkPageIsChecked = true;
  1261. break;
  1262. case LINK_TYPE.URI:
  1263. if (this.isMail || isMail)
  1264. {
  1265. BtnLinkMailIsChecked = true;
  1266. }
  1267. else
  1268. {
  1269. BtnLinkWebIsChecked = true;
  1270. }
  1271. break;
  1272. default:
  1273. BtnLinkPageIsChecked = true;
  1274. break;
  1275. }
  1276. }
  1277. /// <summary>
  1278. /// 设置文本框和按钮的属性
  1279. /// </summary>
  1280. /// <param name="enable"></param>
  1281. /// <param name="totalPage"></param>
  1282. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  1283. {
  1284. this.totalPage = totalPage;
  1285. PageNumTextContent = string.Empty;
  1286. PageWebTextContent = string.Empty;
  1287. PageMailTextContent = string.Empty;
  1288. PageNumTextIsEnabled = enable;
  1289. PageWebTextIsEnabled = enable;
  1290. PageMailTextIsEnabled = enable;
  1291. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  1292. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  1293. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  1294. ShowPageNumTip = Visibility.Collapsed;
  1295. ShowPageWebTip = Visibility.Collapsed;
  1296. ShowPageMailTip = Visibility.Collapsed;
  1297. BtnLocationIsEnabled = enable;
  1298. }
  1299. /// <summary>
  1300. /// 将PDF的页面转换成数据流
  1301. /// </summary>
  1302. /// <param name="document"></param>
  1303. /// <param name="pageIndex"></param>
  1304. /// <param name="renderRect"></param>
  1305. /// <param name="currentZoom"></param>
  1306. /// <param name="renderAnnot"></param>
  1307. /// <param name="renderForm"></param>
  1308. /// <returns></returns>
  1309. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  1310. {
  1311. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  1312. {
  1313. return null;
  1314. }
  1315. try
  1316. {
  1317. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  1318. currentZoom = currentZoom * 96 / 72;
  1319. int flag = 0;
  1320. if (renderAnnot)
  1321. {
  1322. flag = 1;
  1323. }
  1324. uint bgColor = 0xFFFFFFFF;
  1325. CPDFPage page = document.PageAtIndex(pageIndex);
  1326. if (page.IsValid())
  1327. {
  1328. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  1329. }
  1330. return bmpData;
  1331. }
  1332. catch (Exception ex)
  1333. {
  1334. return null;
  1335. }
  1336. }
  1337. /// <summary>
  1338. /// 获取页面的矩形
  1339. /// </summary>
  1340. /// <param name="document"></param>
  1341. /// <param name="pageIndex"></param>
  1342. /// <param name="displayBox"></param>
  1343. /// <returns></returns>
  1344. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  1345. {
  1346. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  1347. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  1348. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  1349. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  1350. return boundRect;
  1351. }
  1352. /// <summary>
  1353. /// 获取页面的尺寸
  1354. /// </summary>
  1355. /// <param name="document"></param>
  1356. /// <param name="pageIndex"></param>
  1357. /// <returns></returns>
  1358. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  1359. {
  1360. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  1361. pageSize.Width = pageSize.Width / 72.0 * 96;
  1362. pageSize.Height = pageSize.Height / 72.0 * 96;
  1363. return pageSize;
  1364. }
  1365. }
  1366. }