LinkAnnotPropertyViewModel.cs 58 KB

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