LinkAnnotPropertyViewModel.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 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. if (linkArgs.AnnotIndex == -1)
  678. {
  679. linkArgs = (LinkAnnotArgs)propertyPanel.annot;
  680. }
  681. System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
  682. linkArgs.IsPositionSet = true;
  683. linkArgs.DestIndex = areaInfo.PageIndex;
  684. linkArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  685. //linkArgs.AnnotIndex = LinkAnnotArgs.AnnotIndex;
  686. //linkArgs.PageIndex = LinkAnnotArgs.PageIndex;
  687. historyPageIndex = LinkAnnotArgs.PageIndex;
  688. //LinkAnnotArgs = linkArgs;
  689. //if (linkArgs.AnnotIndex == -1)
  690. //{
  691. // LinkAnnotArgs.IsPositionSet = true;
  692. // LinkAnnotArgs.DestIndex = areaInfo.PageIndex;
  693. // LinkAnnotArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  694. // LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  695. //}
  696. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  697. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
  698. AnnotAttribEvent?.UpdateAnnot();
  699. linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  700. PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
  701. SetImagePreview(areaInfo.PageIndex);
  702. //viewContentViewModel.LinkAnnotTipVisibility = Visibility.Collapsed;
  703. viewContentViewModel.TipVisible = Visibility.Collapsed;
  704. //没有选择注释工具、也不是右键添加注释
  705. //在阅读页,选择注释后,点击按钮
  706. //需要设置相关参数
  707. if (propertyPanel.IsLocationLink)
  708. {
  709. pdfViewer.SetMouseMode(MouseModes.PanTool);
  710. propertyPanel.IsLocationLink = false;
  711. }
  712. //await System.Threading.Tasks.Task.Delay(5);
  713. pdfViewer.GoToPage(historyPageIndex);
  714. //await System.Threading.Tasks.Task.Delay(5);
  715. //pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  716. BtnLocationIsChecked = false;
  717. //}
  718. //else
  719. //{
  720. // CleanLocationState();
  721. // return;
  722. //}
  723. }
  724. }
  725. }
  726. }
  727. private void CleanLocationState()
  728. {
  729. viewContentViewModel.TipVisible = Visibility.Collapsed;
  730. BtnLocationIsChecked = false;
  731. propertyPanel.IsLocationLink = false;
  732. //没有选择注释工具、也不是右键添加注释
  733. //在阅读页,选择注释后,点击按钮
  734. //需要设置相关参数
  735. if (propertyPanel.IsAddLink == false)
  736. {
  737. pdfViewer.SetMouseMode(MouseModes.PanTool);
  738. }
  739. }
  740. /// <summary>
  741. /// 前往/返回按钮
  742. /// </summary>
  743. /// <param name="obj"></param>
  744. private void BtnGOorBackPageEvent(object obj)
  745. {
  746. if (obj is object[] arry)
  747. {
  748. btnGOorBack = arry[0] as Button;
  749. RadioButton btnLinkPage = arry[1] as RadioButton;
  750. RadioButton btnLinkWeb = arry[2] as RadioButton;
  751. RadioButton btnLinkMail = arry[3] as RadioButton;
  752. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  753. {
  754. if (btnLinkPage.IsChecked == true)
  755. {
  756. LinkPageGoOrBack();
  757. }
  758. if (btnLinkWeb.IsChecked == true)
  759. {
  760. LinkWebSave();
  761. }
  762. if (btnLinkMail.IsChecked == true)
  763. {
  764. LinkMailSave();
  765. }
  766. }
  767. }
  768. }
  769. /// <summary>
  770. /// 保存邮件链接
  771. /// </summary>
  772. private void LinkMailSave()
  773. {
  774. if (CheckPageMailVaild(PageMailTextContent))
  775. {
  776. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  777. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  778. //AnnotAttribEvent?.UpdateAnnot();
  779. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  780. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  781. }
  782. }
  783. /// <summary>
  784. /// 保存web链接
  785. /// </summary>
  786. private void LinkWebSave()
  787. {
  788. if (CheckPageWebVaild(PageWebTextContent))
  789. {
  790. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  791. //PageWebTextContent = PageWebTextContent.Trim().ToLower();
  792. //if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  793. //{
  794. // PageWebTextContent = "http://" + PageWebTextContent;
  795. //}
  796. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  797. //AnnotAttribEvent?.UpdateAnnot();
  798. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  799. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  800. {
  801. Process.Start(PageWebTextContent);
  802. }
  803. }
  804. else
  805. {
  806. BtnGOorBackVisibility = Visibility.Collapsed;
  807. }
  808. }
  809. /// <summary>
  810. /// 页码链接 GoOrBack
  811. /// </summary>
  812. private void LinkPageGoOrBack()
  813. {
  814. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  815. if (btnGOorBack.Tag.ToString() == "GO")
  816. {
  817. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  818. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  819. //AnnotAttribEvent?.UpdateAnnot();
  820. historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
  821. pdfViewer.GoToPage(pageIndex);
  822. ChangeBtnGOorBack("BACK", btnGOorBack);
  823. }
  824. else
  825. {
  826. if (historyPageIndex != -1)
  827. {
  828. SelectAnnotation();
  829. pdfViewer.GoToPage(historyPageIndex);
  830. ChangeBtnGOorBack("GO", btnGOorBack);
  831. }
  832. }
  833. }
  834. /// <summary>
  835. /// 选择注释
  836. /// </summary>
  837. private void SelectAnnotation()
  838. {
  839. if (LinkAnnotArgs != null && !isSelected && LinkAnnotArgs.AnnotIndex >= 0)
  840. {
  841. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  842. isSelected = true;
  843. }
  844. else
  845. {
  846. isSelected = false;
  847. }
  848. }
  849. /// <summary>
  850. /// 设置前往/返回按钮的文本
  851. /// </summary>
  852. /// <param name="flag"></param>
  853. /// <param name="button"></param>
  854. private void ChangeBtnGOorBack(string flag, Button button)
  855. {
  856. switch (flag)
  857. {
  858. case "BACK":
  859. button.Tag = "BACK";
  860. button.Content = "BACK";
  861. break;
  862. case "GO":
  863. button.Tag = "GO";
  864. button.Content = "GO";
  865. break;
  866. default:
  867. break;
  868. }
  869. }
  870. /// <summary>
  871. /// 页码格式验证
  872. /// </summary>
  873. /// <param name="pageNum"></param>
  874. /// <param name="text"></param>
  875. /// <returns></returns>
  876. private bool CheckPageNumVaild(out int pageNum, string text)
  877. {
  878. pageNum = -1;
  879. if (string.IsNullOrEmpty(text))
  880. {
  881. return false;
  882. }
  883. if (text.Trim() != string.Empty)
  884. {
  885. if (!int.TryParse(text.Trim(), out pageNum))
  886. {
  887. ShowPageNumTip = Visibility.Visible;
  888. PageNumTipText = " Page number error.";
  889. return false;
  890. }
  891. }
  892. if (pageNum < 1 || pageNum > totalPage)
  893. {
  894. ShowPageNumTip = Visibility.Visible;
  895. PageNumTipText = " Page number out of range.";
  896. return false;
  897. }
  898. else
  899. {
  900. ShowPageNumTip = Visibility.Collapsed;
  901. }
  902. //BtnGOorBackVisibility = Visibility.Visible;
  903. return true;
  904. }
  905. /// <summary>
  906. /// web链接格式验证
  907. /// </summary>
  908. /// <param name="text"></param>
  909. /// <returns></returns>
  910. private bool CheckPageWebVaild(string text)
  911. {
  912. BtnGOorBackVisibility = Visibility.Collapsed;
  913. if (string.IsNullOrEmpty(text))
  914. {
  915. return false;
  916. }
  917. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  918. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  919. {
  920. ShowPageWebTip = Visibility.Visible;
  921. PageWebTipText = "Invalid link.";
  922. return false;
  923. }
  924. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  925. if (matchText.Length != checkUrl.Length)
  926. {
  927. ShowPageWebTip = Visibility.Visible;
  928. PageWebTipText = "Invalid link.";
  929. return false;
  930. }
  931. else
  932. {
  933. ShowPageWebTip = Visibility.Collapsed;
  934. }
  935. BtnGOorBackVisibility = Visibility.Visible;
  936. return true;
  937. }
  938. /// <summary>
  939. /// 邮箱链接格式验证
  940. /// </summary>
  941. /// <param name="text"></param>
  942. /// <returns></returns>
  943. private bool CheckPageMailVaild(string text)
  944. {
  945. BtnGOorBackVisibility = Visibility.Collapsed;
  946. if (string.IsNullOrEmpty(text))
  947. {
  948. return false;
  949. }
  950. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  951. {
  952. ShowPageMailTip = Visibility.Visible;
  953. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  954. return false;
  955. }
  956. else
  957. {
  958. ShowPageMailTip = Visibility.Collapsed;
  959. }
  960. BtnGOorBackVisibility = Visibility.Visible;
  961. return true;
  962. }
  963. /// <summary>
  964. /// 设置图像
  965. /// </summary>
  966. /// <param name="pageIndex"></param>
  967. private void SetImagePreview(int pageIndex)
  968. {
  969. ImagePreviewVisibility = Visibility.Visible;
  970. BtnGOorBackVisibility = Visibility.Visible;
  971. BtnLocationIsEnabled = true;
  972. int dpiHeight = (int)ImagePreviewHeight;
  973. int dpiWidth = (int)ImagePreviewWidth;
  974. if (dpiHeight <= 0 || dpiWidth <= 0)
  975. {
  976. return;
  977. }
  978. //int pageIndex = AnnotArgs.DestIndex;
  979. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  980. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  981. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  982. scale = Math.Min(scale, 1);
  983. int cropWidth = (int)(scale * pageSize.Width);
  984. int cropHeight = (int)(scale * pageSize.Height);
  985. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  986. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  987. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  988. PreviewImage = WirteBitmap;
  989. }
  990. /// <summary>
  991. /// 链接画框完成时触发事件
  992. /// </summary>
  993. /// <param name="sender"></param>
  994. /// <param name="e"></param>
  995. private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
  996. {
  997. SetTextBoxEnableOrNot(e, totalPage);
  998. }
  999. private void Loaded(object obj)
  1000. {
  1001. if (obj is CompositeCommandParameter composite)
  1002. {
  1003. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  1004. {
  1005. linkAnnot = linkAnnotProperty;
  1006. }
  1007. }
  1008. }
  1009. public bool IsNavigationTarget(NavigationContext navigationContext)
  1010. {
  1011. return true;
  1012. }
  1013. public void OnNavigatedFrom(NavigationContext navigationContext)
  1014. {
  1015. }
  1016. public void OnNavigatedTo(NavigationContext navigationContext)
  1017. {
  1018. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanel);
  1019. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  1020. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  1021. if (propertyPanel != null && pdfViewer != null)
  1022. {
  1023. pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  1024. pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  1025. AnnotAttribEvent = propertyPanel.AnnotEvent;
  1026. AnnotType = propertyPanel.annot.EventType;
  1027. document = pdfViewer.Document;
  1028. totalPage = pdfViewer.Document.PageCount;
  1029. if (viewContentViewModel.IsRightMenuCreateLink)
  1030. {
  1031. SetTextBoxEnableOrNot(true, totalPage);
  1032. }
  1033. else
  1034. {
  1035. SetTextBoxEnableOrNot(false, totalPage);
  1036. }
  1037. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  1038. {
  1039. //根据选择的,链接,获取对应的信息
  1040. GetAnnotAttribute();
  1041. }
  1042. if (isLoaded)
  1043. {
  1044. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  1045. {
  1046. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1047. if (LinkAnnotArgs.AnnotIndex == -1)
  1048. {
  1049. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1050. }
  1051. if (btnGOorBack != null)
  1052. {
  1053. ChangeBtnGOorBack("GO", btnGOorBack);
  1054. }
  1055. if (AnnotAttribEvent.IsAnnotCreateReset)
  1056. {
  1057. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1058. if (LinkAnnotArgs == null)
  1059. {
  1060. backPageIndex = pdfViewer.CurrentIndex;
  1061. }
  1062. else
  1063. {
  1064. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. if (LinkAnnotArgs == null)
  1070. {
  1071. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1072. }
  1073. }
  1074. }
  1075. /// <summary>
  1076. /// 选择链接后,显示参数
  1077. /// </summary>
  1078. private void GetAnnotAttribute()
  1079. {
  1080. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  1081. {
  1082. //设置显示的链接模块
  1083. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  1084. }
  1085. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  1086. {
  1087. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  1088. if (LinkAnnotArgs == null)
  1089. {
  1090. backPageIndex = pdfViewer.CurrentIndex;
  1091. }
  1092. else
  1093. {
  1094. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1095. }
  1096. }
  1097. //暂定 第一版 不显示邮箱、web链接模块
  1098. //if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  1099. //{
  1100. // string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  1101. // if (!string.IsNullOrEmpty(linkUrl))
  1102. // {
  1103. // if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  1104. // {
  1105. // SetLinkType(LINK_TYPE.URI, true);
  1106. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1107. // {
  1108. // SetLinkEmail("");
  1109. // }
  1110. // else
  1111. // {
  1112. // SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  1113. // }
  1114. // }
  1115. // else
  1116. // {
  1117. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1118. // {
  1119. // SetLinkUrl("");
  1120. // }
  1121. // else
  1122. // {
  1123. // SetLinkUrl(linkUrl);
  1124. // }
  1125. // }
  1126. // }
  1127. //}
  1128. }
  1129. private void SetLinkUrl(string url)
  1130. {
  1131. PageWebTextContent = url;
  1132. PageWebTextIsEnabled = true;
  1133. }
  1134. private void SetLinkEmail(string email)
  1135. {
  1136. PageMailTextContent = email;
  1137. PageMailTextIsEnabled = true;
  1138. }
  1139. /// <summary>
  1140. /// 设置页码的数据
  1141. /// </summary>
  1142. /// <param name="pageNum"></param>
  1143. private void SetLinkPageNum(int pageNum)
  1144. {
  1145. if (pageNum > 0 && pageNum <= totalPage)
  1146. {
  1147. PageNumTextContent = pageNum.ToString();
  1148. BtnLocationIsEnabled = true;
  1149. PageNumTextIsEnabled = true;
  1150. ImagePreviewVisibility = Visibility.Visible;
  1151. SetImagePreview(pageNum - 1);
  1152. BtnLocationIsEnabled = true;
  1153. //if (btnGOorBack != null)
  1154. //{
  1155. // ChangeBtnGOorBack("GO", btnGOorBack);
  1156. //}
  1157. }
  1158. }
  1159. /// <summary>
  1160. /// 根据链接类型显示模块
  1161. /// </summary>
  1162. /// <param name="linkType"></param>
  1163. /// <param name="isMail"></param>
  1164. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  1165. {
  1166. BtnLinkPageIsChecked = false;
  1167. BtnLinkMailIsChecked = false;
  1168. BtnLinkWebIsChecked = false;
  1169. //暂定 第一版 不显示邮箱、web链接模块
  1170. if (linkType == LINK_TYPE.URI)
  1171. {
  1172. BtnLinkPageIsChecked = true;
  1173. BtnGOorBackVisibility = Visibility.Collapsed;
  1174. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1175. return;
  1176. }
  1177. switch (linkType)
  1178. {
  1179. case LINK_TYPE.GOTO:
  1180. BtnLinkPageIsChecked = true;
  1181. break;
  1182. case LINK_TYPE.URI:
  1183. if (this.isMail || isMail)
  1184. {
  1185. BtnLinkMailIsChecked = true;
  1186. }
  1187. else
  1188. {
  1189. BtnLinkWebIsChecked = true;
  1190. }
  1191. break;
  1192. default:
  1193. BtnLinkPageIsChecked = true;
  1194. break;
  1195. }
  1196. }
  1197. /// <summary>
  1198. /// 设置文本框和按钮的属性
  1199. /// </summary>
  1200. /// <param name="enable"></param>
  1201. /// <param name="totalPage"></param>
  1202. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  1203. {
  1204. this.totalPage = totalPage;
  1205. PageNumTextContent = string.Empty;
  1206. PageWebTextContent = string.Empty;
  1207. PageMailTextContent = string.Empty;
  1208. PageNumTextIsEnabled = enable;
  1209. PageWebTextIsEnabled = enable;
  1210. PageMailTextIsEnabled = enable;
  1211. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  1212. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  1213. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  1214. ShowPageNumTip = Visibility.Collapsed;
  1215. ShowPageWebTip = Visibility.Collapsed;
  1216. ShowPageMailTip = Visibility.Collapsed;
  1217. BtnLocationIsEnabled = enable;
  1218. }
  1219. /// <summary>
  1220. /// 将PDF的页面转换成数据流
  1221. /// </summary>
  1222. /// <param name="document"></param>
  1223. /// <param name="pageIndex"></param>
  1224. /// <param name="renderRect"></param>
  1225. /// <param name="currentZoom"></param>
  1226. /// <param name="renderAnnot"></param>
  1227. /// <param name="renderForm"></param>
  1228. /// <returns></returns>
  1229. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  1230. {
  1231. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  1232. {
  1233. return null;
  1234. }
  1235. try
  1236. {
  1237. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  1238. currentZoom = currentZoom * 96 / 72;
  1239. int flag = 0;
  1240. if (renderAnnot)
  1241. {
  1242. flag = 1;
  1243. }
  1244. uint bgColor = 0xFFFFFFFF;
  1245. CPDFPage page = document.PageAtIndex(pageIndex);
  1246. if (page.IsValid())
  1247. {
  1248. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  1249. }
  1250. return bmpData;
  1251. }
  1252. catch (Exception ex)
  1253. {
  1254. return null;
  1255. }
  1256. }
  1257. /// <summary>
  1258. /// 获取页面的矩形
  1259. /// </summary>
  1260. /// <param name="document"></param>
  1261. /// <param name="pageIndex"></param>
  1262. /// <param name="displayBox"></param>
  1263. /// <returns></returns>
  1264. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  1265. {
  1266. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  1267. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  1268. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  1269. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  1270. return boundRect;
  1271. }
  1272. /// <summary>
  1273. /// 获取页面的尺寸
  1274. /// </summary>
  1275. /// <param name="document"></param>
  1276. /// <param name="pageIndex"></param>
  1277. /// <returns></returns>
  1278. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  1279. {
  1280. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  1281. pageSize.Width = pageSize.Width / 72.0 * 96;
  1282. pageSize.Height = pageSize.Height / 72.0 * 96;
  1283. return pageSize;
  1284. }
  1285. }
  1286. }