LinkAnnotPropertyViewModel.cs 62 KB

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