LinkAnnotPropertyViewModel.cs 57 KB

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