LinkAnnotPropertyViewModel.cs 52 KB

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