LinkAnnotPropertyViewModel.cs 58 KB

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