LinkAnnotPropertyViewModel.cs 48 KB

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