LinkAnnotPropertyViewModel.cs 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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> PageNumTextGotFocusCommand { get; set; }
  438. public DelegateCommand<object> PageNumTextKeyDownCommand { get; set; }
  439. public DelegateCommand<object> PageMailTextLostFocusCommand { get; set; }
  440. public DelegateCommand<object> PageMailTextKeyDownCommand { get; set; }
  441. public DelegateCommand<object> PageWebTextLostFocusCommand { get; set; }
  442. public DelegateCommand<object> PageWebTextKeyDownCommand { get; set; }
  443. public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
  444. public DelegateCommand<object> LocationCommand { get; set; }
  445. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  446. #endregion 命令
  447. public LinkAnnotPropertyViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  448. {
  449. region = regionManager;
  450. dialogs = dialogService;
  451. events = eventAggregator;
  452. LoadedCommand = new DelegateCommand<object>(Loaded);
  453. PageNumTextLostFocusCommand = new DelegateCommand<object>(PageNumTextLostFocus);
  454. //PageNumTextGotFocusCommand = new DelegateCommand<object>(PageNumTextGotFocus);
  455. PageNumTextKeyDownCommand = new DelegateCommand<object>(PageNumTextKeyDown);
  456. PageMailTextLostFocusCommand = new DelegateCommand<object>(PageMailTextLostFocus);
  457. PageMailTextKeyDownCommand = new DelegateCommand<object>(PageMailTextKeyDown);
  458. PageWebTextLostFocusCommand = new DelegateCommand<object>(PageWebTextLostFocus);
  459. PageWebTextKeyDownCommand = new DelegateCommand<object>(PageWebTextKeyDown);
  460. BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
  461. LocationCommand = new DelegateCommand<object>(LocationPage);
  462. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  463. isLoaded = true;
  464. InitString();
  465. }
  466. private void PageWebTextKeyDown(object obj)
  467. {
  468. if (obj is KeyEventArgs keyEventArgs)
  469. {
  470. if (keyEventArgs.Key == Key.Enter)
  471. {
  472. PageWebTextLostFocus(obj);
  473. }
  474. }
  475. }
  476. private void PageWebTextLostFocus(object obj)
  477. {
  478. if (!string.IsNullOrEmpty(PageWebTextContent))
  479. {
  480. if (CheckPageWebVaild(PageWebTextContent))
  481. {
  482. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  483. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  484. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  485. {
  486. PageWebTextContent = "http://" + PageWebTextContent;
  487. }
  488. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  489. AnnotAttribEvent?.UpdateAnnot();
  490. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  491. }
  492. }
  493. }
  494. private void PageMailTextKeyDown(object obj)
  495. {
  496. if (obj is KeyEventArgs keyEventArgs)
  497. {
  498. if (keyEventArgs.Key == Key.Enter)
  499. {
  500. PageMailTextLostFocus(obj);
  501. }
  502. }
  503. }
  504. private void PageMailTextLostFocus(object obj)
  505. {
  506. if (!string.IsNullOrEmpty(PageMailTextContent))
  507. {
  508. if (CheckPageMailVaild(PageMailTextContent))
  509. {
  510. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  511. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  512. AnnotAttribEvent?.UpdateAnnot();
  513. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  514. }
  515. }
  516. }
  517. private string pageNumTextStr = string.Empty;
  518. /// <summary>
  519. /// 页码文本框 Enter键
  520. /// </summary>
  521. /// <param name="obj"></param>
  522. private void PageNumTextKeyDown(object obj)
  523. {
  524. if (obj is KeyEventArgs keyEventArgs)
  525. {
  526. if (keyEventArgs.Key == Key.Enter)
  527. {
  528. PageNumTextLostFocus(obj);
  529. pageNumTextStr = PageNumTextContent;
  530. }
  531. }
  532. }
  533. /// <summary>
  534. /// 页码文本框获取焦点
  535. /// </summary>
  536. /// <param name="obj"></param>
  537. private void PageNumTextGotFocus(object obj)
  538. {
  539. BtnLocationIsEnabled = false;
  540. viewContentViewModel.ShowTip(false);
  541. }
  542. /// <summary>
  543. /// 页码文本框失去焦点
  544. /// </summary>
  545. /// <param name="obj"></param>
  546. private void PageNumTextLostFocus(object obj)
  547. {
  548. if (!string.IsNullOrEmpty(PageNumTextContent)
  549. && pdfViewer.MouseMode == MouseModes.AnnotCreate
  550. && pageNumTextStr != PageNumTextContent)
  551. {
  552. if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  553. {
  554. isSelected = false;
  555. propertyPanel.IsAddLink = false;
  556. if (LinkAnnotArgs.PageIndex == -1)
  557. {
  558. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  559. }
  560. historyPageIndex = LinkAnnotArgs.PageIndex;
  561. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  562. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  563. //AnnotAttribEvent?.UpdateAnnot();
  564. //isTextChanged = false;
  565. SetImagePreview(LinkAnnotArgs.DestIndex);
  566. //ImagePreviewVisibility = Visibility.Collapsed;
  567. //BtnGOorBackVisibility= Visibility.Collapsed;
  568. BtnLocationIsEnabled = false;
  569. if (viewContentViewModel.IsRightMenuSelectedTextAddLink == false)
  570. {
  571. int index = (int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx];
  572. if (viewContentViewModel.IsSelectedTextAddLink)
  573. {
  574. viewContentViewModel.IsSelectedTextAddLink = false;
  575. //LinkAnnotArgs.DestIndex = pageNum - 1;
  576. //pdfViewer.CreatePageAnnot(pdfViewer.CurrentIndex, LinkAnnotArgs);
  577. }
  578. }
  579. //else
  580. //{
  581. // //右键选择文本 创建链接
  582. // LinkAnnotArgs.DestIndex = pageNum - 1;
  583. // LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  584. //}
  585. if (LinkAnnotArgs.AnnotIndex != -1)
  586. {
  587. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  588. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  589. AnnotAttribEvent?.UpdateAnnot();
  590. }
  591. else
  592. {
  593. LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  594. }
  595. //if (viewContentViewModel.IsRightMenuSelectedTextAddLink || viewContentViewModel.IsRightMenuCreateLink)
  596. //{
  597. // viewContentViewModel.IsRightMenuSelectedTextAddLink = false;
  598. // viewContentViewModel.IsRightMenuCreateLink = false;
  599. // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  600. // var annotArgs = new SelectToolArgs();
  601. // pdfViewer.SetToolParam(annotArgs);
  602. // return;
  603. //}
  604. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  605. //SelectAnnotation();
  606. }
  607. else
  608. {
  609. BtnLocationIsEnabled = true;
  610. ImagePreviewVisibility = Visibility.Collapsed;
  611. BtnGOorBackVisibility = Visibility.Collapsed;
  612. }
  613. }
  614. else
  615. {
  616. BtnLocationIsEnabled = true;
  617. }
  618. }
  619. /// <summary>
  620. /// 模块选择
  621. /// </summary>
  622. /// <param name="obj"></param>
  623. private void ToggleButtonTabSelected(object obj)
  624. {
  625. if (obj is RadioButton radioButton)
  626. {
  627. switch (radioButton.Tag)
  628. {
  629. case "Page":
  630. LinkAnnotArgs.LinkType = LINK_TYPE.GOTO;
  631. LinkAnnotArgs.URI = string.Empty;
  632. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.GOTO;
  633. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  634. isMail = false;
  635. if (btnGOorBack != null)
  636. {
  637. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  638. {
  639. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  640. }
  641. else
  642. {
  643. ChangeBtnGOorBack("GO", btnGOorBack);
  644. }
  645. }
  646. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  647. break;
  648. case "Web":
  649. isMail = false;
  650. SetLinkType_URIState("Web");
  651. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  652. break;
  653. case "Mail":
  654. isMail = true;
  655. SetLinkType_URIState("Mail");
  656. BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  657. break;
  658. }
  659. }
  660. }
  661. /// <summary>
  662. /// 设置BtnGOorBack状态
  663. /// </summary>
  664. /// <param name="uri"></param>
  665. private void SetLinkType_URIState(string uri)
  666. {
  667. LinkAnnotArgs.LinkType = LINK_TYPE.URI;
  668. LinkAnnotArgs.URI = string.Empty;
  669. AnnotAttribEvent.Attribs[AnnotAttrib.LinkType] = LINK_TYPE.URI;
  670. AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri] = string.Empty;
  671. if (btnGOorBack != null)
  672. {
  673. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  674. //ChangeBtnGOorBack("GO", btnGOorBack);
  675. switch (uri)
  676. {
  677. case "Web":
  678. if (!string.IsNullOrEmpty(PageWebTextContent))
  679. {
  680. ChangeBtnGOorBack("GO", btnGOorBack);
  681. }
  682. //BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  683. break;
  684. case "Mail":
  685. if (!string.IsNullOrEmpty(PageMailTextContent))
  686. {
  687. ChangeBtnGOorBack("GO", btnGOorBack);
  688. }
  689. //BtnGOorBackVisibility = string.IsNullOrEmpty(PageMailTextContent) ? Visibility.Collapsed : Visibility.Visible;
  690. break;
  691. }
  692. }
  693. }
  694. /// <summary>
  695. /// 定位目标范围
  696. /// </summary>
  697. /// <param name="obj"></param>
  698. private void LocationPage(object obj)
  699. {
  700. //没有选择注释工具、也不是右键添加注释
  701. //在阅读页,选择注释后,点击按钮
  702. //需要设置相关参数
  703. //if (AnnotAttribEvent.IsAnnotCreateReset == false && propertyPanel.IsAddLink == false)
  704. //{
  705. // propertyPanel.IsLocationLink = true;
  706. // pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  707. // pdfViewer.SetToolParam(LinkAnnotArgs);
  708. //}
  709. if (pdfViewer.MouseMode == MouseModes.AnnotCreate)
  710. {
  711. historyPageIndex = pdfViewer.CurrentIndex;
  712. isSelected = false;
  713. BtnLocationIsChecked = true;
  714. if (region.Regions.ContainsRegionWithName(viewContentViewModel.TipContentRegionName))
  715. {
  716. var views = region.Regions[viewContentViewModel.TipContentRegionName].Views;
  717. var isHas = views.FindFirst(q => q is Views.TipContent.LinkAnnotTip);
  718. if (isHas != null)
  719. {
  720. region.Regions[viewContentViewModel.TipContentRegionName].Activate(isHas);
  721. }
  722. else
  723. {
  724. region.RequestNavigate(viewContentViewModel.TipContentRegionName, "LinkAnnotTip");
  725. }
  726. }
  727. //viewContentViewModel.TipVisible = Visibility.Visible;
  728. if (BtnLocationIsChecked && viewContentViewModel.TipVisible == Visibility.Visible)
  729. {
  730. pdfViewer.EnableDrawSelectArea(false);
  731. viewContentViewModel.ShowTip(false);
  732. BtnLocationIsChecked = false;
  733. PageNumTextIsEnabled = true;
  734. }
  735. else
  736. {
  737. pdfViewer.EnableDrawSelectArea(true);
  738. viewContentViewModel.ShowTip(true);
  739. PageNumTextIsEnabled = false;
  740. //BtnLocationIsChecked = true;
  741. }
  742. //CommonDrawData.LinkAreaSelectBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(50, 23, 112, 244));
  743. //AnnotAttribEvent.IsAnnotCreateReset = true;
  744. }
  745. }
  746. private Rect paintrect;
  747. /// <summary>
  748. /// 链接事件
  749. /// </summary>
  750. /// <param name="sender"></param>
  751. /// <param name="e"></param>
  752. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  753. {
  754. IsEmptyPanelVisibility = Visibility.Collapsed;
  755. //delete 键删除后
  756. if (e.NotifyType == CustomNotifyType.LinkToolRemove)
  757. {
  758. IsEmptyPanelVisibility = Visibility.Visible;
  759. }
  760. //定位目标范围完成时,触发
  761. if (e.NotifyType == CustomNotifyType.LinkAreaLocate)
  762. {
  763. if (e.NotifyData is AreaLocate areaInfo)
  764. {
  765. if (pdfViewer.ToolManager.CurrentAnnotArgs == null)
  766. {
  767. CleanLocationState();
  768. return;
  769. }
  770. if ((pdfViewer.ToolManager.CurrentAnnotArgs.EventType == AnnotArgsType.AnnotLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  771. || (viewContentViewModel.IsRightMenuSelectedTextAddLink && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  772. || (viewContentViewModel.IsRightMenuCreateLink && pdfViewer.MouseMode == MouseModes.AnnotCreate))
  773. {
  774. propertyPanel.IsAddLink = false;
  775. System.Windows.Size pageSize = pdfViewer.Document.GetPageSize(areaInfo.PageIndex);
  776. if (LinkAnnotArgs.PageIndex == -1)
  777. {
  778. LinkAnnotArgs.PageIndex = historyPageIndex - 1;
  779. }
  780. historyPageIndex = LinkAnnotArgs.PageIndex;
  781. AnnotAttribEvent = propertyPanel.AnnotEvent;
  782. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  783. LinkAnnotArgs.IsPositionSet = true;
  784. LinkAnnotArgs.DestIndex = areaInfo.PageIndex;
  785. LinkAnnotArgs.DestinationPos = new System.Windows.Point(areaInfo.Area.X, pageSize.Height - areaInfo.Area.Y);
  786. if (viewContentViewModel.IsRightMenuSelectedTextAddLink == false)
  787. {
  788. if (viewContentViewModel.IsSelectedTextAddLink)
  789. {
  790. viewContentViewModel.IsSelectedTextAddLink = false;
  791. //pdfViewer.CreatePageAnnot(historyPageIndex, LinkAnnotArgs);
  792. }
  793. }
  794. //else
  795. //{
  796. // //右键选择文本 创建链接
  797. // //pdfViewer.CreatePageAnnot(historyPageIndex, LinkAnnotArgs);
  798. // LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  799. //}
  800. if (LinkAnnotArgs.AnnotIndex != -1)
  801. {
  802. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  803. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, areaInfo.PageIndex);
  804. AnnotAttribEvent?.UpdateAnnot();
  805. }
  806. else
  807. {
  808. LinkAnnotArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  809. }
  810. BtnLocationIsChecked = false;
  811. PageNumTextContent = (areaInfo.PageIndex + 1).ToString();
  812. //SetImagePreview(areaInfo.PageIndex);
  813. viewContentViewModel.TipVisible = Visibility.Collapsed;
  814. //精准定位
  815. paintrect = LinkAnnotArgs.GetLinkCreateArea();
  816. pdfViewer.GoToPage(historyPageIndex);
  817. BackToPage(historyPageIndex, areaInfo.PageIndex);
  818. //if (string.IsNullOrEmpty(PageNumTextContent))
  819. //{
  820. //pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  821. //}
  822. if (viewContentViewModel.IsSelectedTextAddLink)
  823. {
  824. viewContentViewModel.IsSelectedTextAddLink = false;
  825. }
  826. //右键选择文本 创建链接
  827. //if (viewContentViewModel.IsRightMenuSelectedTextAddLink || viewContentViewModel.IsRightMenuCreateLink )
  828. //{
  829. // viewContentViewModel.IsRightMenuSelectedTextAddLink = false;
  830. // viewContentViewModel.IsRightMenuCreateLink = false;
  831. // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  832. // var annotArgs = new SelectToolArgs();
  833. // pdfViewer.SetToolParam(annotArgs);
  834. // return;
  835. //}
  836. }
  837. }
  838. }
  839. }
  840. private void CleanLocationState()
  841. {
  842. viewContentViewModel.TipVisible = Visibility.Collapsed;
  843. BtnLocationIsChecked = false;
  844. //没有选择注释工具、也不是右键添加注释
  845. //在阅读页,选择注释后,点击按钮
  846. //需要设置相关参数
  847. if (propertyPanel.IsAddLink == false)
  848. {
  849. pdfViewer.SetMouseMode(MouseModes.PanTool);
  850. }
  851. }
  852. /// <summary>
  853. /// 前往/返回按钮
  854. /// </summary>
  855. /// <param name="obj"></param>
  856. private void BtnGOorBackPageEvent(object obj)
  857. {
  858. if (obj is object[] arry)
  859. {
  860. btnGOorBack = arry[0] as Button;
  861. RadioButton btnLinkPage = arry[1] as RadioButton;
  862. RadioButton btnLinkWeb = arry[2] as RadioButton;
  863. RadioButton btnLinkMail = arry[3] as RadioButton;
  864. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  865. {
  866. if (btnLinkPage.IsChecked == true)
  867. {
  868. LinkPageGoOrBack();
  869. }
  870. if (btnLinkWeb.IsChecked == true)
  871. {
  872. LinkWebSave();
  873. }
  874. if (btnLinkMail.IsChecked == true)
  875. {
  876. LinkMailSave();
  877. }
  878. }
  879. }
  880. }
  881. /// <summary>
  882. /// 保存邮件链接
  883. /// </summary>
  884. private void LinkMailSave()
  885. {
  886. if (CheckPageMailVaild(PageMailTextContent))
  887. {
  888. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  889. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, "mailto:" + PageMailTextContent.Trim());
  890. //AnnotAttribEvent?.UpdateAnnot();
  891. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  892. Process.Start("mailto:?to=" + PageMailTextContent.Trim());
  893. }
  894. }
  895. /// <summary>
  896. /// 保存web链接
  897. /// </summary>
  898. private void LinkWebSave()
  899. {
  900. if (CheckPageWebVaild(PageWebTextContent))
  901. {
  902. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  903. //PageWebTextContent = PageWebTextContent.Trim().ToLower();
  904. //if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  905. //{
  906. // PageWebTextContent = "http://" + PageWebTextContent;
  907. //}
  908. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  909. //AnnotAttribEvent?.UpdateAnnot();
  910. //LinkAnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  911. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  912. {
  913. Process.Start(PageWebTextContent);
  914. }
  915. }
  916. else
  917. {
  918. BtnGOorBackVisibility = Visibility.Collapsed;
  919. }
  920. }
  921. /// <summary>
  922. /// 页码链接 GoOrBack
  923. /// </summary>
  924. private void LinkPageGoOrBack()
  925. {
  926. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  927. ////注释位置
  928. //Rect rect = LinkAnnotArgs.GetLinkPaintArea();
  929. ////定位
  930. //Rect paintrect = LinkAnnotArgs.GetLinkCreateArea();
  931. //double height = 0;
  932. //CPDFDocument doc = pdfViewer.Document;
  933. //CPDFPage docPage;
  934. //double zoom = pdfViewer.ZoomFactor;
  935. if (btnGOorBack.Tag.ToString() == "GO")
  936. {
  937. CPDFLinkAnnotation linkAnnot = LinkAnnotArgs.GetLinkAnnot();
  938. if (linkAnnot != null)
  939. {
  940. CPDFDestination dest = linkAnnot.GetDestination(pdfViewer.Document);
  941. System.Windows.Point rawPoint = new System.Windows.Point(dest.Position_X, dest.Position_Y);
  942. CPDFPage docPage = pdfViewer.Document.PageAtIndex(pageIndex, false);
  943. pdfViewer.GoToPage(pageIndex, new System.Windows.Point(rawPoint.X, docPage.PageSize.Height - rawPoint.Y));
  944. }
  945. else
  946. {
  947. pdfViewer.GoToPage(pageIndex);
  948. GoToPage(pageIndex);
  949. }
  950. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  951. //AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageIndex);
  952. //AnnotAttribEvent?.UpdateAnnot();
  953. historyPageIndex = LinkAnnotArgs != null ? LinkAnnotArgs.PageIndex : backPageIndex;
  954. //GoToPage(pageIndex);
  955. ChangeBtnGOorBack("BACK", btnGOorBack);
  956. }
  957. else
  958. {
  959. if (historyPageIndex != -1)
  960. {
  961. pdfViewer.GoToPage(historyPageIndex);
  962. //SelectAnnotation();
  963. BackToPage(historyPageIndex, pageIndex);
  964. ChangeBtnGOorBack("GO", btnGOorBack);
  965. //pdfViewer.SelectAnnotation(historyPageIndex, LinkAnnotArgs.AnnotIndex);
  966. }
  967. }
  968. }
  969. private void BackToPage(int historyPageIndex, int pageIndex)
  970. {
  971. CPDFLinkAnnotation cPDFLink = LinkAnnotArgs.GetLinkAnnot();
  972. if (cPDFLink == null)
  973. {
  974. pdfViewer.GoToPage(historyPageIndex);
  975. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  976. return;
  977. }
  978. //注释位置
  979. Rect rect = LinkAnnotArgs.GetLinkPaintArea();
  980. //定位
  981. Rect paintrect = LinkAnnotArgs.GetLinkCreateArea();
  982. double height = 0;
  983. CPDFDocument doc = pdfViewer.Document;
  984. CPDFPage docPage;
  985. double zoom = pdfViewer.ZoomFactor;
  986. docPage = doc.PageAtIndex(historyPageIndex, false);
  987. if (!rect.IsEmpty)
  988. {
  989. double vertical = pdfViewer.GetVerticalOffset();
  990. if (docPage.PageSize.Height / 2 < rect.Top)
  991. {
  992. if (historyPageIndex < pageIndex)
  993. {
  994. //height = vertical + (rect.Bottom * zoom);
  995. height = vertical + (rect.Top * zoom);
  996. pdfViewer.ScrollToVerticalOffset(height);
  997. }
  998. else
  999. {
  1000. height = vertical + (rect.Top * zoom);
  1001. pdfViewer.ScrollToVerticalOffset(height);
  1002. }
  1003. }
  1004. }
  1005. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  1006. }
  1007. private void GoToPage(int pageIndex)
  1008. {
  1009. //注释位置
  1010. Rect rect = LinkAnnotArgs.GetLinkPaintArea();
  1011. //定位
  1012. paintrect = LinkAnnotArgs.GetLinkCreateArea();
  1013. //System.Windows.Point point = LinkAnnotArgs.DestinationPos;
  1014. double height = 0;
  1015. //CPDFDocument doc = pdfViewer.Document;
  1016. //CPDFPage docPage;
  1017. double zoom = pdfViewer.ZoomFactor;
  1018. //docPage = doc.PageAtIndex(pageIndex, false);
  1019. if (!paintrect.IsEmpty)
  1020. {
  1021. CPDFPage docPage = pdfViewer.Document.PageAtIndex(pageIndex, false);
  1022. //pdfViewer.GoToPage(pageIndex, new System.Windows.Point(0, docPage.PageSize.Height - paintrect.Y*2));
  1023. double vertical = pdfViewer.GetVerticalOffset();
  1024. if (docPage.PageSize.Height / 2 < paintrect.Top)
  1025. {
  1026. if (historyPageIndex < pageIndex)
  1027. {
  1028. //height = vertical + (paintrect.Bottom * zoom);
  1029. height = vertical + (paintrect.Top * zoom);
  1030. pdfViewer.ScrollToVerticalOffset(height);
  1031. }
  1032. else
  1033. {
  1034. height = vertical + (paintrect.Top * zoom);
  1035. pdfViewer.ScrollToVerticalOffset(height);
  1036. }
  1037. }
  1038. height = vertical + (paintrect.Bottom * zoom);
  1039. }
  1040. }
  1041. /// <summary>
  1042. /// 选择注释
  1043. /// </summary>
  1044. private void SelectAnnotation()
  1045. {
  1046. if (LinkAnnotArgs != null && !isSelected && LinkAnnotArgs.AnnotIndex >= 0)
  1047. {
  1048. pdfViewer.SelectAnnotation(LinkAnnotArgs.PageIndex, LinkAnnotArgs.AnnotIndex);
  1049. isSelected = true;
  1050. }
  1051. else
  1052. {
  1053. isSelected = false;
  1054. }
  1055. }
  1056. /// <summary>
  1057. /// 设置前往/返回按钮的文本
  1058. /// </summary>
  1059. /// <param name="flag"></param>
  1060. /// <param name="button"></param>
  1061. private void ChangeBtnGOorBack(string flag, Button button)
  1062. {
  1063. switch (flag)
  1064. {
  1065. case "BACK":
  1066. button.Tag = "BACK";
  1067. button.Content = "BACK";
  1068. break;
  1069. case "GO":
  1070. button.Tag = "GO";
  1071. button.Content = "GO";
  1072. break;
  1073. default:
  1074. break;
  1075. }
  1076. }
  1077. /// <summary>
  1078. /// 页码格式验证
  1079. /// </summary>
  1080. /// <param name="pageNum"></param>
  1081. /// <param name="text"></param>
  1082. /// <returns></returns>
  1083. private bool CheckPageNumVaild(out int pageNum, string text)
  1084. {
  1085. pageNum = -1;
  1086. if (string.IsNullOrEmpty(text))
  1087. {
  1088. return false;
  1089. }
  1090. if (text.Trim() != string.Empty)
  1091. {
  1092. if (!int.TryParse(text.Trim(), out pageNum))
  1093. {
  1094. ShowPageNumTip = Visibility.Visible;
  1095. PageNumTipText = " 错误格式,仅支持数字";
  1096. return false;
  1097. }
  1098. }
  1099. if (pageNum < 1 || pageNum > totalPage)
  1100. {
  1101. ShowPageNumTip = Visibility.Visible;
  1102. PageNumTipText = " Page number out of range.";
  1103. return false;
  1104. }
  1105. else
  1106. {
  1107. ShowPageNumTip = Visibility.Collapsed;
  1108. }
  1109. //BtnGOorBackVisibility = Visibility.Visible;
  1110. return true;
  1111. }
  1112. /// <summary>
  1113. /// web链接格式验证
  1114. /// </summary>
  1115. /// <param name="text"></param>
  1116. /// <returns></returns>
  1117. private bool CheckPageWebVaild(string text)
  1118. {
  1119. BtnGOorBackVisibility = Visibility.Collapsed;
  1120. if (string.IsNullOrEmpty(text))
  1121. {
  1122. return false;
  1123. }
  1124. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  1125. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  1126. {
  1127. ShowPageWebTip = Visibility.Visible;
  1128. PageWebTipText = "Invalid link.";
  1129. return false;
  1130. }
  1131. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  1132. if (matchText.Length != checkUrl.Length)
  1133. {
  1134. ShowPageWebTip = Visibility.Visible;
  1135. PageWebTipText = "Invalid link.";
  1136. return false;
  1137. }
  1138. else
  1139. {
  1140. ShowPageWebTip = Visibility.Collapsed;
  1141. }
  1142. BtnGOorBackVisibility = Visibility.Visible;
  1143. return true;
  1144. }
  1145. /// <summary>
  1146. /// 邮箱链接格式验证
  1147. /// </summary>
  1148. /// <param name="text"></param>
  1149. /// <returns></returns>
  1150. private bool CheckPageMailVaild(string text)
  1151. {
  1152. BtnGOorBackVisibility = Visibility.Collapsed;
  1153. if (string.IsNullOrEmpty(text))
  1154. {
  1155. return false;
  1156. }
  1157. if (!Regex.IsMatch(text, "^[A-Za-z0-9\u4e00-\u9fa5_\\-\\.]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"))
  1158. {
  1159. ShowPageMailTip = Visibility.Visible;
  1160. PageMailTipText = "The email is invalid.Please enter a valid email address.";
  1161. return false;
  1162. }
  1163. else
  1164. {
  1165. ShowPageMailTip = Visibility.Collapsed;
  1166. }
  1167. BtnGOorBackVisibility = Visibility.Visible;
  1168. return true;
  1169. }
  1170. /// <summary>
  1171. /// 设置图像
  1172. /// </summary>
  1173. /// <param name="pageIndex"></param>
  1174. private void SetImagePreview(int pageIndex)
  1175. {
  1176. ImagePreviewVisibility = Visibility.Visible;
  1177. BtnGOorBackVisibility = Visibility.Visible;
  1178. BtnLocationIsEnabled = true;
  1179. int dpiHeight = (int)ImagePreviewHeight;
  1180. int dpiWidth = (int)ImagePreviewWidth;
  1181. if (dpiHeight <= 0 || dpiWidth <= 0)
  1182. {
  1183. return;
  1184. }
  1185. //int pageIndex = AnnotArgs.DestIndex;
  1186. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  1187. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  1188. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  1189. scale = Math.Min(scale, 1);
  1190. int cropWidth = (int)(scale * pageSize.Width);
  1191. int cropHeight = (int)(scale * pageSize.Height);
  1192. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  1193. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  1194. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  1195. PreviewImage = WirteBitmap;
  1196. }
  1197. /// <summary>
  1198. /// 链接画框完成时触发事件
  1199. /// </summary>
  1200. /// <param name="sender"></param>
  1201. /// <param name="e"></param>
  1202. private void linkAnnotArgs_LinkDrawFinished(object sender, bool e)
  1203. {
  1204. IsEmptyPanelVisibility = e ? Visibility.Collapsed : Visibility.Visible;
  1205. SetTextBoxEnableOrNot(e, totalPage);
  1206. }
  1207. private void Loaded(object obj)
  1208. {
  1209. if (obj is CompositeCommandParameter composite)
  1210. {
  1211. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  1212. {
  1213. linkAnnot = linkAnnotProperty;
  1214. }
  1215. }
  1216. }
  1217. public bool IsNavigationTarget(NavigationContext navigationContext)
  1218. {
  1219. return true;
  1220. }
  1221. public void OnNavigatedFrom(NavigationContext navigationContext)
  1222. {
  1223. }
  1224. public void OnNavigatedTo(NavigationContext navigationContext)
  1225. {
  1226. //SolidColorBrush solidColorBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(50, 23, 112, 244));
  1227. //SolidColorBrush solidColorPen = new SolidColorBrush(System.Windows.Media.Color.FromRgb(23, 112, 244));
  1228. //CommonDrawData.LinkPen = new System.Windows.Media.Pen(solidColorPen, 0);
  1229. //CommonDrawData.LinkBrush = solidColorBrush;
  1230. //CommonDrawData.LinkAreaSelectPen = new System.Windows.Media.Pen(solidColorBrush, 0);
  1231. //CommonDrawData.LinkAreaSelectBrush = solidColorBrush;
  1232. //CommonDrawData.LinkAreaSelectBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(50, 23, 112, 244));
  1233. navigationContext.Parameters.TryGetValue<AnnotTransfer>(ParameterNames.PropertyPanelContentViewModel, out propertyPanel);
  1234. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  1235. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  1236. events.GetEvent<LinkAnnotTipCloseEvent>().Subscribe(LinkAnnotTipClose, e => e.UniCode == App.mainWindowViewModel.SelectedItem.Unicode);
  1237. if (propertyPanel != null && pdfViewer != null)
  1238. {
  1239. pdfViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  1240. pdfViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  1241. AnnotAttribEvent = propertyPanel.AnnotEvent;
  1242. AnnotType = propertyPanel.annot.EventType;
  1243. document = pdfViewer.Document;
  1244. totalPage = pdfViewer.Document.PageCount;
  1245. if (viewContentViewModel.IsRightMenuCreateLink || viewContentViewModel.IsRightMenuSelectedTextAddLink)
  1246. {
  1247. IsEmptyPanelVisibility = viewContentViewModel.IsRightMenuSelectedTextAddLink ? Visibility.Collapsed : Visibility.Visible;
  1248. SetTextBoxEnableOrNot(true, totalPage);
  1249. //viewContentViewModel.IsRightMenuSelectedTextAddLink=false;
  1250. }
  1251. else
  1252. {
  1253. if (viewContentViewModel.IsSelectedTextAddLink)
  1254. {
  1255. IsEmptyPanelVisibility = viewContentViewModel.IsSelectedTextAddLink ? Visibility.Collapsed : Visibility.Visible;
  1256. SetTextBoxEnableOrNot(true, totalPage);
  1257. }
  1258. else
  1259. {
  1260. if (propertyPanel.annot.AnnotIndex == -1 && propertyPanel.annot.PageIndex == -1)
  1261. {
  1262. IsEmptyPanelVisibility = Visibility.Visible;
  1263. }
  1264. if (AnnotAttribEvent.IsAnnotCreateReset == false && propertyPanel.annot.AnnotIndex != -1 && propertyPanel.annot.PageIndex != -1)
  1265. {
  1266. IsEmptyPanelVisibility = Visibility.Collapsed;
  1267. }
  1268. SetTextBoxEnableOrNot(false, totalPage);
  1269. }
  1270. }
  1271. if (isLoaded)
  1272. {
  1273. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  1274. {
  1275. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1276. //LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1277. //if (LinkAnnotArgs.AnnotIndex == -1)
  1278. //{
  1279. // LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1280. //}
  1281. if (btnGOorBack != null)
  1282. {
  1283. ChangeBtnGOorBack("GO", btnGOorBack);
  1284. }
  1285. if (AnnotAttribEvent.IsAnnotCreateReset)
  1286. {
  1287. LinkAnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  1288. if (LinkAnnotArgs == null)
  1289. {
  1290. backPageIndex = pdfViewer.CurrentIndex;
  1291. }
  1292. else
  1293. {
  1294. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1295. }
  1296. }
  1297. }
  1298. //右键选择文本 创建链接
  1299. else
  1300. {
  1301. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1302. }
  1303. }
  1304. if (AnnotAttribEvent.IsAnnotCreateReset == false)
  1305. {
  1306. //根据选择的,链接,获取对应的信息
  1307. GetAnnotAttribute();
  1308. }
  1309. if (LinkAnnotArgs == null)
  1310. {
  1311. LinkAnnotArgs = (LinkAnnotArgs)propertyPanel.annot;
  1312. }
  1313. }
  1314. }
  1315. private void LinkAnnotTipClose(LinkAnnotTipCloseArgs obj)
  1316. {
  1317. pdfViewer.EnableDrawSelectArea(false);
  1318. viewContentViewModel.ShowTip(false);
  1319. BtnLocationIsChecked = false;
  1320. //PageNumTextIsEnabled = true;
  1321. }
  1322. /// <summary>
  1323. /// 选择链接后,显示参数
  1324. /// </summary>
  1325. private void GetAnnotAttribute()
  1326. {
  1327. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  1328. {
  1329. //设置显示的链接模块
  1330. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  1331. }
  1332. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  1333. {
  1334. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  1335. if (LinkAnnotArgs == null)
  1336. {
  1337. backPageIndex = pdfViewer.CurrentIndex;
  1338. }
  1339. else
  1340. {
  1341. if (LinkAnnotArgs.PageIndex == -1)
  1342. LinkAnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  1343. }
  1344. }
  1345. //暂定 第一版 不显示邮箱、web链接模块
  1346. //if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  1347. //{
  1348. // string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  1349. // if (!string.IsNullOrEmpty(linkUrl))
  1350. // {
  1351. // if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  1352. // {
  1353. // SetLinkType(LINK_TYPE.URI, true);
  1354. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1355. // {
  1356. // SetLinkEmail("");
  1357. // }
  1358. // else
  1359. // {
  1360. // SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  1361. // }
  1362. // }
  1363. // else
  1364. // {
  1365. // if (AnnotAttribEvent.IsAnnotCreateReset)
  1366. // {
  1367. // SetLinkUrl("");
  1368. // }
  1369. // else
  1370. // {
  1371. // SetLinkUrl(linkUrl);
  1372. // }
  1373. // }
  1374. // }
  1375. //}
  1376. }
  1377. private void SetLinkUrl(string url)
  1378. {
  1379. PageWebTextContent = url;
  1380. PageWebTextIsEnabled = true;
  1381. }
  1382. private void SetLinkEmail(string email)
  1383. {
  1384. PageMailTextContent = email;
  1385. PageMailTextIsEnabled = true;
  1386. }
  1387. /// <summary>
  1388. /// 设置页码的数据
  1389. /// </summary>
  1390. /// <param name="pageNum"></param>
  1391. private void SetLinkPageNum(int pageNum)
  1392. {
  1393. if (pageNum > 0 && pageNum <= totalPage)
  1394. {
  1395. PageNumTextContent = pageNum.ToString();
  1396. BtnLocationIsEnabled = true;
  1397. PageNumTextIsEnabled = true;
  1398. ImagePreviewVisibility = Visibility.Visible;
  1399. SetImagePreview(pageNum - 1);
  1400. BtnLocationIsEnabled = true;
  1401. //if (btnGOorBack != null)
  1402. //{
  1403. // ChangeBtnGOorBack("GO", btnGOorBack);
  1404. //}
  1405. }
  1406. }
  1407. /// <summary>
  1408. /// 根据链接类型显示模块
  1409. /// </summary>
  1410. /// <param name="linkType"></param>
  1411. /// <param name="isMail"></param>
  1412. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  1413. {
  1414. BtnLinkPageIsChecked = false;
  1415. BtnLinkMailIsChecked = false;
  1416. BtnLinkWebIsChecked = false;
  1417. //暂定 第一版 不显示邮箱、web链接模块
  1418. if (linkType == LINK_TYPE.URI)
  1419. {
  1420. BtnLinkPageIsChecked = true;
  1421. BtnGOorBackVisibility = Visibility.Collapsed;
  1422. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1423. return;
  1424. }
  1425. switch (linkType)
  1426. {
  1427. case LINK_TYPE.GOTO:
  1428. BtnLinkPageIsChecked = true;
  1429. break;
  1430. case LINK_TYPE.URI:
  1431. if (this.isMail || isMail)
  1432. {
  1433. BtnLinkMailIsChecked = true;
  1434. }
  1435. else
  1436. {
  1437. BtnLinkWebIsChecked = true;
  1438. }
  1439. break;
  1440. default:
  1441. BtnLinkPageIsChecked = true;
  1442. break;
  1443. }
  1444. }
  1445. /// <summary>
  1446. /// 设置文本框和按钮的属性
  1447. /// </summary>
  1448. /// <param name="enable"></param>
  1449. /// <param name="totalPage"></param>
  1450. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  1451. {
  1452. this.totalPage = totalPage;
  1453. PageNumTextContent = string.Empty;
  1454. PageWebTextContent = string.Empty;
  1455. PageMailTextContent = string.Empty;
  1456. PageNumTextIsEnabled = enable;
  1457. PageWebTextIsEnabled = enable;
  1458. PageMailTextIsEnabled = enable;
  1459. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  1460. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  1461. PageMailPlaceHoldText = PageMailTextIsEnabled ? "support@pdfreaderpro.com" : "输入您要跳转邮件地址";
  1462. ShowPageNumTip = Visibility.Collapsed;
  1463. ShowPageWebTip = Visibility.Collapsed;
  1464. ShowPageMailTip = Visibility.Collapsed;
  1465. BtnLocationIsEnabled = enable;
  1466. }
  1467. /// <summary>
  1468. /// 将PDF的页面转换成数据流
  1469. /// </summary>
  1470. /// <param name="document"></param>
  1471. /// <param name="pageIndex"></param>
  1472. /// <param name="renderRect"></param>
  1473. /// <param name="currentZoom"></param>
  1474. /// <param name="renderAnnot"></param>
  1475. /// <param name="renderForm"></param>
  1476. /// <returns></returns>
  1477. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  1478. {
  1479. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  1480. {
  1481. return null;
  1482. }
  1483. try
  1484. {
  1485. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  1486. currentZoom = currentZoom * 96 / 72;
  1487. int flag = 0;
  1488. if (renderAnnot)
  1489. {
  1490. flag = 1;
  1491. }
  1492. uint bgColor = 0xFFFFFFFF;
  1493. CPDFPage page = document.PageAtIndex(pageIndex);
  1494. if (page.IsValid())
  1495. {
  1496. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  1497. }
  1498. return bmpData;
  1499. }
  1500. catch (Exception ex)
  1501. {
  1502. return null;
  1503. }
  1504. }
  1505. /// <summary>
  1506. /// 获取页面的矩形
  1507. /// </summary>
  1508. /// <param name="document"></param>
  1509. /// <param name="pageIndex"></param>
  1510. /// <param name="displayBox"></param>
  1511. /// <returns></returns>
  1512. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  1513. {
  1514. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  1515. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  1516. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  1517. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  1518. return boundRect;
  1519. }
  1520. /// <summary>
  1521. /// 获取页面的尺寸
  1522. /// </summary>
  1523. /// <param name="document"></param>
  1524. /// <param name="pageIndex"></param>
  1525. /// <returns></returns>
  1526. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  1527. {
  1528. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  1529. pageSize.Width = pageSize.Width / 72.0 * 96;
  1530. pageSize.Height = pageSize.Height / 72.0 * 96;
  1531. return pageSize;
  1532. }
  1533. }
  1534. }