LinkAnnotPropertyViewModel.cs 48 KB

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