LinkAnnotPropertyViewModel.cs 49 KB

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