LinkAnnotPropertyViewModel.cs 59 KB

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