LinkAnnotPropertyViewModel.cs 48 KB

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