LinkAnnotPropertyViewModel.cs 51 KB

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