LinkAnnotPropertyViewModel.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.Model;
  6. using PDF_Office.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_Office.Helper;
  20. using Microsoft.Office.Interop.Word;
  21. using static Dropbox.Api.Files.FileCategory;
  22. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  23. using System.Windows.Controls;
  24. using PDF_Office.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. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  32. {
  33. internal class LinkAnnotPropertyViewModel : BindableBase, INavigationAware
  34. {
  35. public AnnotAttribEvent AnnotAttribEvent { get; set; }
  36. private AnnotPropertyPanel PropertyPanel;
  37. private AnnotArgsType annotType;
  38. private LinkAnnotArgs annotArgs;
  39. public LinkAnnotArgs AnnotArgs
  40. {
  41. get
  42. {
  43. return annotArgs;
  44. }
  45. set
  46. {
  47. if (annotArgs != null)
  48. {
  49. annotArgs.LinkDrawFinished -= annotArgs_LinkDrawFinished;
  50. }
  51. annotArgs = value;
  52. if (annotArgs != null)
  53. {
  54. annotArgs.LinkDrawFinished += annotArgs_LinkDrawFinished;
  55. }
  56. }
  57. }
  58. public AnnotArgsType AnnotType
  59. {
  60. get { return annotType; }
  61. set
  62. {
  63. SetProperty(ref annotType, value);
  64. }
  65. }
  66. #region 输入框的验证、设置
  67. private string pageNumTextContent;
  68. public string PageNumTextContent
  69. {
  70. get
  71. {
  72. //if (string.IsNullOrEmpty(pageNumTextContent))
  73. //{
  74. // return "Enter target page";
  75. //}
  76. //if (pageNumTextContent != "Enter target page")
  77. //{
  78. if (CheckPageNumVaild(out int pageNum, pageNumTextContent))
  79. {
  80. if (AnnotArgs != null)
  81. {
  82. AnnotArgs.DestIndex = pageNum - 1;
  83. SetImagePreview(AnnotArgs.DestIndex);
  84. }
  85. }
  86. else
  87. {
  88. //BtnLocationIsEnabled = false;
  89. ImagePreviewVisibility = Visibility.Collapsed;
  90. BtnGOorBackVisibility = Visibility.Collapsed;
  91. }
  92. //}
  93. return pageNumTextContent;
  94. }
  95. set
  96. {
  97. SetProperty(ref pageNumTextContent, value);
  98. }
  99. }
  100. /// <summary>
  101. /// 设置预文本内容
  102. /// </summary>
  103. private string pageNumPlaceHoldText = "输入目标页面";
  104. public string PageNumPlaceHoldText
  105. {
  106. get { return pageNumPlaceHoldText; }
  107. set
  108. {
  109. SetProperty(ref pageNumPlaceHoldText, value);
  110. }
  111. }
  112. /// <summary>
  113. /// 是否显示输入框下方提示文案
  114. /// </summary>
  115. private Visibility showPageNumTip = Visibility.Collapsed;
  116. public Visibility ShowPageNumTip
  117. {
  118. get { return showPageNumTip; }
  119. set
  120. {
  121. SetProperty(ref showPageNumTip, value);
  122. }
  123. }
  124. /// <summary>
  125. /// 输入框下方提示文案
  126. /// </summary>
  127. private string pageNumTipText = "Page number error.";
  128. public string PageNumTipText
  129. {
  130. get { return pageNumTipText; }
  131. set
  132. {
  133. SetProperty(ref pageNumTipText, value);
  134. }
  135. }
  136. private bool pageNumTextIsEnabled = false;
  137. public bool PageNumTextIsEnabled
  138. {
  139. get { return pageNumTextIsEnabled; }
  140. set
  141. {
  142. SetProperty(ref pageNumTextIsEnabled, value);
  143. }
  144. }
  145. private bool pageWebTextIsEnabled = false;
  146. public bool PageWebTextIsEnabled
  147. {
  148. get { return pageWebTextIsEnabled; }
  149. set
  150. {
  151. SetProperty(ref pageWebTextIsEnabled, value);
  152. }
  153. }
  154. private string pageWebPlaceHoldText = "输入您要跳转的链接";
  155. public string PageWebPlaceHoldText
  156. {
  157. get { return pageWebPlaceHoldText; }
  158. set
  159. {
  160. SetProperty(ref pageWebPlaceHoldText, value);
  161. }
  162. }
  163. private Visibility showPageWebTip = Visibility.Collapsed;
  164. public Visibility ShowPageWebTip
  165. {
  166. get { return showPageWebTip; }
  167. set
  168. {
  169. SetProperty(ref showPageWebTip, value);
  170. }
  171. }
  172. private string pageWebTextContent;
  173. public string PageWebTextContent
  174. {
  175. get
  176. {
  177. if (CheckPageWebVaild(pageWebTextContent))
  178. {
  179. pageWebTextContent = pageWebTextContent.Trim().ToLower();
  180. if (!pageWebTextContent.StartsWith("http://") && !pageWebTextContent.StartsWith("https://"))
  181. {
  182. pageWebTextContent = "http://" + pageWebTextContent;
  183. }
  184. if (AnnotArgs != null)
  185. {
  186. AnnotArgs.URI = pageWebTextContent;
  187. }
  188. }
  189. return pageWebTextContent;
  190. }
  191. set
  192. {
  193. SetProperty(ref pageWebTextContent, value);
  194. }
  195. }
  196. private string pageWebTipText = "Invalid link.";
  197. public string PageWebTipText
  198. {
  199. get { return pageWebTipText; }
  200. set
  201. {
  202. SetProperty(ref pageWebTipText, value);
  203. }
  204. }
  205. #endregion 输入框的验证、设置
  206. #region 按钮、图片的设置
  207. private bool btnLocationIsEnabled = false;
  208. public bool BtnLocationIsEnabled
  209. {
  210. get { return btnLocationIsEnabled; }
  211. set
  212. {
  213. SetProperty(ref btnLocationIsEnabled, value);
  214. }
  215. }
  216. private WriteableBitmap previewImage;
  217. public WriteableBitmap PreviewImage
  218. {
  219. get { return previewImage; }
  220. set
  221. {
  222. SetProperty(ref previewImage, value);
  223. }
  224. }
  225. private int imagePreviewWidth = 196;
  226. public int ImagePreviewWidth
  227. {
  228. get { return imagePreviewWidth; }
  229. set
  230. {
  231. SetProperty(ref imagePreviewWidth, value);
  232. }
  233. }
  234. private int imagePreviewHeight = 256;
  235. public int ImagePreviewHeight
  236. {
  237. get { return imagePreviewHeight; }
  238. set
  239. {
  240. SetProperty(ref imagePreviewHeight, value);
  241. }
  242. }
  243. private Visibility imagePreviewVisibility = Visibility.Collapsed;
  244. public Visibility ImagePreviewVisibility
  245. {
  246. get { return imagePreviewVisibility; }
  247. set
  248. {
  249. SetProperty(ref imagePreviewVisibility, value);
  250. }
  251. }
  252. private Visibility btnGOorBackVisibility = Visibility.Collapsed;
  253. public Visibility BtnGOorBackVisibility
  254. {
  255. get { return btnGOorBackVisibility; }
  256. set
  257. {
  258. SetProperty(ref btnGOorBackVisibility, value);
  259. }
  260. }
  261. private string btnGOorBackContent = "GO";
  262. public string BtnGOorBackContent
  263. {
  264. get { return btnGOorBackContent; }
  265. set
  266. {
  267. SetProperty(ref btnGOorBackContent, value);
  268. }
  269. }
  270. private bool btnLinkPageIsChecked = true;
  271. public bool BtnLinkPageIsChecked
  272. {
  273. get { return btnLinkPageIsChecked; }
  274. set
  275. {
  276. SetProperty(ref btnLinkPageIsChecked, value);
  277. }
  278. }
  279. private bool btnLinkWebIsChecked = false;
  280. public bool BtnLinkWebIsChecked
  281. {
  282. get { return btnLinkWebIsChecked; }
  283. set
  284. {
  285. SetProperty(ref btnLinkWebIsChecked, value);
  286. }
  287. }
  288. private bool btnLinkMailIsChecked = false;
  289. public bool BtnLinkMailIsChecked
  290. {
  291. get { return btnLinkMailIsChecked; }
  292. set
  293. {
  294. SetProperty(ref btnLinkMailIsChecked, value);
  295. }
  296. }
  297. #endregion 按钮、图片的设置
  298. private bool isLoaded = false;
  299. private bool isMail = false;
  300. private string historyBtnGOorBackTag = string.Empty;
  301. private int totalPage = 0;
  302. private int backPageIndex = 0;
  303. private int historyPageIndex = 0;
  304. private CPDFViewer pdfViewer;
  305. private CPDFDocument document;
  306. private Button btnGOorBack = null;
  307. private LinkAnnotProperty linkAnnot;
  308. public DelegateCommand<object> LoadedCommand { get; set; }
  309. public DelegateCommand<object> PageNumTextChangedCommand { get; set; }
  310. public DelegateCommand<object> BtnGOorBackPageCommand { get; set; }
  311. public DelegateCommand<object> LocationCommand { get; set; }
  312. public DelegateCommand<object> ToggleButtonTabCommand { get; set; }
  313. public LinkAnnotPropertyViewModel()
  314. {
  315. LoadedCommand = new DelegateCommand<object>(Loaded);
  316. PageNumTextChangedCommand = new DelegateCommand<object>(PageNumTextChanged);
  317. BtnGOorBackPageCommand = new DelegateCommand<object>(BtnGOorBackPageEvent);
  318. LocationCommand = new DelegateCommand<object>(LocationPage);
  319. ToggleButtonTabCommand = new DelegateCommand<object>(ToggleButtonTabSelected);
  320. isLoaded = true;
  321. }
  322. private void ToggleButtonTabSelected(object obj)
  323. {
  324. if (obj is RadioButton radioButton)
  325. {
  326. switch (radioButton.Tag)
  327. {
  328. case "Page":
  329. AnnotArgs.LinkType = LINK_TYPE.GOTO;
  330. isMail = false;
  331. if (!string.IsNullOrEmpty(historyBtnGOorBackTag))
  332. {
  333. ChangeBtnGOorBack(historyBtnGOorBackTag, btnGOorBack);
  334. }
  335. else
  336. {
  337. ChangeBtnGOorBack("GO", btnGOorBack);
  338. }
  339. BtnGOorBackVisibility = string.IsNullOrEmpty(PageNumTextContent) ? Visibility.Collapsed : Visibility.Visible;
  340. break;
  341. case "Web":
  342. AnnotArgs.LinkType = LINK_TYPE.URI;
  343. isMail = false;
  344. if (btnGOorBack != null)
  345. {
  346. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  347. ChangeBtnGOorBack("GO", btnGOorBack);
  348. }
  349. BtnGOorBackVisibility = string.IsNullOrEmpty(PageWebTextContent) ? Visibility.Collapsed : Visibility.Visible;
  350. break;
  351. case "Mail":
  352. AnnotArgs.LinkType = LINK_TYPE.URI;
  353. isMail = true;
  354. if (btnGOorBack != null)
  355. {
  356. historyBtnGOorBackTag = btnGOorBack.Tag.ToString();
  357. ChangeBtnGOorBack("GO", btnGOorBack);
  358. }
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// 定位
  367. /// </summary>
  368. /// <param name="obj"></param>
  369. private void LocationPage(object obj)
  370. {
  371. if (!string.IsNullOrEmpty(PageNumTextContent))
  372. {
  373. if (CheckPageNumVaild(out int pageNum, PageNumTextContent))
  374. {
  375. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.GOTO);
  376. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkDestIndx, pageNum - 1);
  377. AnnotAttribEvent?.UpdateAnnot();
  378. AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  379. }
  380. }
  381. }
  382. /// <summary>
  383. /// 前往/返回按钮
  384. /// </summary>
  385. /// <param name="obj"></param>
  386. private void BtnGOorBackPageEvent(object obj)
  387. {
  388. if (obj is object[] arry)
  389. {
  390. btnGOorBack = arry[0] as Button;
  391. RadioButton btnLinkPage = arry[1] as RadioButton;
  392. RadioButton btnLinkWeb = arry[2] as RadioButton;
  393. RadioButton btnLinkMail = arry[3] as RadioButton;
  394. if (btnLinkPage != null && btnGOorBack != null && btnLinkWeb != null && btnLinkMail != null)
  395. {
  396. if (btnLinkPage.IsChecked == true)
  397. {
  398. int pageIndex = Convert.ToInt32(PageNumTextContent) - 1;
  399. if (btnGOorBack.Tag.ToString() == "GO")
  400. {
  401. historyPageIndex = AnnotArgs != null ? AnnotArgs.PageIndex : backPageIndex;
  402. pdfViewer.GoToPage(pageIndex);
  403. ChangeBtnGOorBack("BACK", btnGOorBack);
  404. //SetImagePreview(historyPageIndex);
  405. }
  406. else
  407. {
  408. pdfViewer.GoToPage(historyPageIndex);
  409. ChangeBtnGOorBack("GO", btnGOorBack);
  410. //SetImagePreview(pageIndex);
  411. }
  412. }
  413. if (btnLinkWeb.IsChecked == true)
  414. {
  415. if (CheckPageWebVaild(PageWebTextContent))
  416. {
  417. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkType, LINK_TYPE.URI);
  418. PageWebTextContent = PageWebTextContent.Trim().ToLower();
  419. if (!PageWebTextContent.StartsWith("http://") && !PageWebTextContent.StartsWith("https://"))
  420. {
  421. PageWebTextContent = "http://" + PageWebTextContent;
  422. }
  423. AnnotAttribEvent?.UpdateAttrib(AnnotAttrib.LinkUri, PageWebTextContent);
  424. AnnotAttribEvent?.UpdateAnnot();
  425. AnnotArgs?.InvokeLinkSaveCalled(this, EventArgs.Empty);
  426. if (PageWebTextContent.StartsWith("http://") || PageWebTextContent.StartsWith("https://"))
  427. {
  428. Process.Start(PageWebTextContent);
  429. }
  430. }
  431. else
  432. {
  433. BtnGOorBackVisibility = Visibility.Collapsed;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. /// <summary>
  440. /// 设置前往/返回按钮的文本
  441. /// </summary>
  442. /// <param name="flag"></param>
  443. /// <param name="button"></param>
  444. private void ChangeBtnGOorBack(string flag, Button button)
  445. {
  446. switch (flag)
  447. {
  448. case "BACK":
  449. button.Tag = "BACK";
  450. button.Content = "BACK";
  451. break;
  452. case "GO":
  453. button.Tag = "GO";
  454. button.Content = "GO";
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. private void PageNumTextChanged(object obj)
  461. {
  462. if (obj is TextChangedEventArgs textChangedEventArgs)
  463. {
  464. if (textChangedEventArgs.Source is TextBoxWithTip textBox)
  465. {
  466. int pageNum = -1;
  467. //if (CheckPageNumVaild(out pageNum))
  468. //{
  469. // if (AnnotArgs != null)
  470. // {
  471. // AnnotArgs.DestIndex = pageNum - 1;
  472. // SetImagePreview();
  473. // }
  474. //}
  475. }
  476. }
  477. }
  478. /// <summary>
  479. /// 页码格式验证
  480. /// </summary>
  481. /// <param name="pageNum"></param>
  482. /// <param name="text"></param>
  483. /// <returns></returns>
  484. private bool CheckPageNumVaild(out int pageNum, string text)
  485. {
  486. pageNum = -1;
  487. if (string.IsNullOrEmpty(text))
  488. {
  489. return false;
  490. }
  491. if (text.Trim() != string.Empty)
  492. {
  493. if (!int.TryParse(text.Trim(), out pageNum))
  494. {
  495. ShowPageNumTip = Visibility.Visible;
  496. PageNumTipText = " Page number error.";
  497. return false;
  498. }
  499. }
  500. if (pageNum < 1 || pageNum > totalPage)
  501. {
  502. ShowPageNumTip = Visibility.Visible;
  503. PageNumTipText = " Page number out of range.";
  504. return false;
  505. }
  506. else
  507. {
  508. ShowPageNumTip = Visibility.Collapsed;
  509. }
  510. BtnGOorBackVisibility = Visibility.Visible;
  511. return true;
  512. }
  513. private bool CheckPageWebVaild(string text)
  514. {
  515. BtnGOorBackVisibility = Visibility.Collapsed;
  516. if (string.IsNullOrEmpty(text))
  517. {
  518. return false;
  519. }
  520. string checkUrl = text.ToLower().TrimStart("http://".ToCharArray()).TrimStart("https://".ToCharArray());
  521. if (!Regex.IsMatch(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+"))
  522. {
  523. ShowPageWebTip = Visibility.Visible;
  524. PageWebTipText = "Invalid link.";
  525. return false;
  526. }
  527. string matchText = Regex.Match(checkUrl, "([a-zA-Z0-9/\\-%\\?#&=]+[./\\-%\\?#&=]?)+").Value;
  528. if (matchText.Length != checkUrl.Length)
  529. {
  530. ShowPageWebTip = Visibility.Visible;
  531. PageWebTipText = "Invalid link.";
  532. return false;
  533. }
  534. else
  535. {
  536. ShowPageWebTip = Visibility.Collapsed;
  537. }
  538. BtnGOorBackVisibility = Visibility.Visible;
  539. return true;
  540. }
  541. /// <summary>
  542. /// 设置图像
  543. /// </summary>
  544. /// <param name="pageIndex"></param>
  545. private void SetImagePreview(int pageIndex)
  546. {
  547. ImagePreviewVisibility = Visibility.Visible;
  548. BtnGOorBackVisibility = Visibility.Visible;
  549. int dpiHeight = (int)ImagePreviewHeight;
  550. int dpiWidth = (int)ImagePreviewWidth;
  551. if (dpiHeight <= 0 || dpiWidth <= 0)
  552. {
  553. return;
  554. }
  555. //int pageIndex = AnnotArgs.DestIndex;
  556. Rect MediaRect = GetPageRect(document, pageIndex, CPDFDisplayBox.MediaBox);
  557. System.Windows.Size pageSize = GetPageSize(document, pageIndex);
  558. double scale = Math.Min(dpiWidth / MediaRect.Width, dpiHeight / MediaRect.Height);
  559. scale = Math.Min(scale, 1);
  560. int cropWidth = (int)(scale * pageSize.Width);
  561. int cropHeight = (int)(scale * pageSize.Height);
  562. byte[] imageDatas = RenderPDFPageToArray(document, pageIndex, new Rect(0, 0, cropWidth, cropHeight), scale, true, true);
  563. WriteableBitmap WirteBitmap = new WriteableBitmap(cropWidth, cropHeight, 96, 96, PixelFormats.Bgra32, null);
  564. WirteBitmap.WritePixels(new Int32Rect(0, 0, cropWidth, cropHeight), imageDatas, WirteBitmap.BackBufferStride, 0);
  565. PreviewImage = WirteBitmap;
  566. }
  567. /// <summary>
  568. /// 链接画框完成时触发事件
  569. /// </summary>
  570. /// <param name="sender"></param>
  571. /// <param name="e"></param>
  572. private void annotArgs_LinkDrawFinished(object sender, bool e)
  573. {
  574. //linkAnnot.SetTextBoxEnableOrNot(e, totalPage);
  575. SetTextBoxEnableOrNot(e, totalPage);
  576. //AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  577. }
  578. private void Loaded(object obj)
  579. {
  580. if (obj is CompositeCommandParameter composite)
  581. {
  582. if (composite.Parameter is LinkAnnotProperty linkAnnotProperty)
  583. {
  584. linkAnnot = linkAnnotProperty;
  585. }
  586. }
  587. }
  588. public bool IsNavigationTarget(NavigationContext navigationContext)
  589. {
  590. return true;
  591. }
  592. public void OnNavigatedFrom(NavigationContext navigationContext)
  593. {
  594. }
  595. public void OnNavigatedTo(NavigationContext navigationContext)
  596. {
  597. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  598. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  599. if (PropertyPanel != null && pdfViewer != null)
  600. {
  601. AnnotAttribEvent = PropertyPanel.AnnotEvent;
  602. AnnotType = PropertyPanel.annot.EventType;
  603. document = pdfViewer.Document;
  604. totalPage = pdfViewer.Document.PageCount;
  605. SetTextBoxEnableOrNot(false, totalPage);
  606. SetAnnotAttribute();
  607. if (isLoaded)
  608. {
  609. if (pdfViewer != null && pdfViewer.ToolManager != null && pdfViewer.ToolManager.CurrentAnnotArgs?.EventType == AnnotArgsType.AnnotLink)
  610. {
  611. if (btnGOorBack != null)
  612. {
  613. ChangeBtnGOorBack("GO", btnGOorBack);
  614. }
  615. if (AnnotAttribEvent.IsAnnotCreateReset)
  616. {
  617. AnnotArgs = pdfViewer.ToolManager.CurrentAnnotArgs as LinkAnnotArgs;
  618. if (AnnotArgs == null)
  619. {
  620. backPageIndex = pdfViewer.CurrentIndex;
  621. }
  622. else
  623. {
  624. AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  625. }
  626. }
  627. }
  628. }
  629. if (AnnotArgs == null)
  630. {
  631. AnnotArgs = (LinkAnnotArgs)PropertyPanel.annot;
  632. }
  633. }
  634. }
  635. private void SetAnnotAttribute()
  636. {
  637. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkType))
  638. {
  639. SetLinkType((LINK_TYPE)AnnotAttribEvent.Attribs[AnnotAttrib.LinkType]);
  640. }
  641. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkDestIndx) && !AnnotAttribEvent.IsAnnotCreateReset)
  642. {
  643. SetLinkPageNum((int)AnnotAttribEvent.Attribs[AnnotAttrib.LinkDestIndx] + 1);
  644. if (AnnotArgs == null)
  645. {
  646. backPageIndex = pdfViewer.CurrentIndex;
  647. }
  648. else
  649. {
  650. AnnotArgs.PageIndex = pdfViewer.CurrentIndex;
  651. }
  652. }
  653. if (AnnotAttribEvent.Attribs.ContainsKey(AnnotAttrib.LinkUri))
  654. {
  655. string linkUrl = (string)AnnotAttribEvent.Attribs[AnnotAttrib.LinkUri];
  656. if (!string.IsNullOrEmpty(linkUrl))
  657. {
  658. if (linkUrl.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
  659. {
  660. SetLinkType(LINK_TYPE.URI, true);
  661. if (AnnotAttribEvent.IsAnnotCreateReset)
  662. {
  663. SetLinkEmail("");
  664. }
  665. else
  666. {
  667. SetLinkEmail(linkUrl.ToLower().TrimStart("mailto:".ToCharArray()));
  668. }
  669. }
  670. else
  671. {
  672. if (AnnotAttribEvent.IsAnnotCreateReset)
  673. {
  674. SetLinkUrl("");
  675. }
  676. else
  677. {
  678. SetLinkUrl(linkUrl);
  679. }
  680. }
  681. }
  682. }
  683. }
  684. private void SetLinkUrl(string url)
  685. {
  686. PageWebTextContent = url;
  687. PageWebTextIsEnabled = true;
  688. }
  689. private void SetLinkEmail(string email)
  690. {
  691. }
  692. /// <summary>
  693. /// 设置页码的数据
  694. /// </summary>
  695. /// <param name="pageNum"></param>
  696. private void SetLinkPageNum(int pageNum)
  697. {
  698. if (pageNum > 0 && pageNum <= totalPage)
  699. {
  700. PageNumTextContent = pageNum.ToString();
  701. BtnLocationIsEnabled = true;
  702. PageNumTextIsEnabled = true;
  703. ImagePreviewVisibility = Visibility.Visible;
  704. //SetImagePreview(pageNum - 1);
  705. //if (btnGOorBack != null)
  706. //{
  707. // ChangeBtnGOorBack("GO", btnGOorBack);
  708. //}
  709. }
  710. }
  711. /// <summary>
  712. /// 根据链接类型显示模块
  713. /// </summary>
  714. /// <param name="linkType"></param>
  715. /// <param name="isMail"></param>
  716. public void SetLinkType(LINK_TYPE linkType, bool isMail = false)
  717. {
  718. BtnLinkPageIsChecked = false;
  719. BtnLinkMailIsChecked = false;
  720. BtnLinkWebIsChecked = false;
  721. switch (linkType)
  722. {
  723. case LINK_TYPE.GOTO:
  724. BtnLinkPageIsChecked = true;
  725. break;
  726. case LINK_TYPE.URI:
  727. if (this.isMail || isMail)
  728. {
  729. BtnLinkMailIsChecked = true;
  730. }
  731. else
  732. {
  733. BtnLinkWebIsChecked = true;
  734. }
  735. break;
  736. default:
  737. BtnLinkPageIsChecked = true;
  738. break;
  739. }
  740. }
  741. private void SetTextBoxEnableOrNot(bool enable, int totalPage)
  742. {
  743. this.totalPage = totalPage;
  744. PageNumTextContent = string.Empty;
  745. PageWebTextContent = string.Empty;
  746. //PageMailText.Text = string.Empty;
  747. PageNumTextIsEnabled = enable;
  748. PageWebTextIsEnabled = enable;
  749. //if (PageNumTextIsEnabled == true)
  750. //{
  751. // PageNumPlaceHoldText = string.Format($"1-{totalPage}页");
  752. //}
  753. //else
  754. //{
  755. // PageNumPlaceHoldText = "输入目标页面";
  756. //}
  757. PageNumPlaceHoldText = PageNumTextIsEnabled ? string.Format($"1-{totalPage}页") : "输入目标页面";
  758. PageWebPlaceHoldText = PageWebTextIsEnabled ? "https://www.pdfreaderpro.com" : "输入您要跳转的链接";
  759. ShowPageNumTip = Visibility.Collapsed;
  760. //BorderPageNumText.Background = enable ? Brushes.White : Brushes.LightGray;
  761. ShowPageWebTip = Visibility.Collapsed;
  762. //PageWebText.IsEnabled = enable;
  763. //BorderPageWebText.Background = enable ? Brushes.White : Brushes.LightGray;
  764. //PageMailText.IsEnabled = enable;
  765. //BorderPageMailText.Background = enable ? Brushes.White : Brushes.LightGray;
  766. BtnLocationIsEnabled = enable;
  767. //if (AnnotArgs.LinkType == LINK_TYPE.URI)
  768. //{
  769. // BtnGOorBackVisibility = Visibility.Visible;
  770. //}
  771. //else
  772. //{
  773. // BtnGOorBackVisibility = Visibility.Collapsed;
  774. //}
  775. //BtnGOorBack.IsEnabled = false;
  776. }
  777. public byte[] RenderPDFPageToArray(CPDFDocument document, int pageIndex, Rect renderRect, double currentZoom, bool renderAnnot = false, bool renderForm = false)
  778. {
  779. if (renderRect.Width <= 0 || renderRect.Height <= 0 || document == null)
  780. {
  781. return null;
  782. }
  783. try
  784. {
  785. byte[] bmpData = new byte[(int)renderRect.Width * (int)renderRect.Height * 4];
  786. currentZoom = currentZoom * 96 / 72;
  787. int flag = 0;
  788. if (renderAnnot)
  789. {
  790. flag = 1;
  791. }
  792. uint bgColor = 0xFFFFFFFF;
  793. CPDFPage page = document.PageAtIndex(pageIndex);
  794. if (page.IsValid())
  795. {
  796. page.RenderPageBitmapWithMatrix((float)currentZoom, renderRect, bgColor, bmpData, flag, renderForm);
  797. }
  798. return bmpData;
  799. }
  800. catch (Exception ex)
  801. {
  802. return null;
  803. }
  804. }
  805. private Rect GetPageRect(CPDFDocument document, int pageIndex, CPDFDisplayBox displayBox)
  806. {
  807. CPDFPage currentPage = document.PageAtIndex(pageIndex);
  808. Rect boundBox = currentPage.GetBoundsForBox(displayBox);
  809. Rect boundRect = new Rect((int)(boundBox.Left / 72.0 * 96.0), (int)(boundBox.Top / 72.0 * 96.0),
  810. (int)(boundBox.Width / 72.0 * 96.0), (int)(boundBox.Height / 72.0 * 96.0));
  811. return boundRect;
  812. }
  813. private System.Windows.Size GetPageSize(CPDFDocument document, int pageIndex)
  814. {
  815. System.Windows.Size pageSize = document.GetPageSize(pageIndex);
  816. pageSize.Width = pageSize.Width / 72.0 * 96;
  817. pageSize.Height = pageSize.Height / 72.0 * 96;
  818. return pageSize;
  819. }
  820. }
  821. }