LinkAnnotPropertyViewModel.cs 43 KB

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