LinkAnnotPropertyViewModel.cs 49 KB

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