LinkAnnotPropertyViewModel.cs 51 KB

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