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