TextEditToolContentViewModel.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  11. using System.Windows.Controls;
  12. using Prism.Regions;
  13. using ComPDFKitViewer.PdfViewer;
  14. using PDF_Master.Model;
  15. using Microsoft.Win32;
  16. using PDF_Master.CustomControl;
  17. using ComPDFKitViewer;
  18. using System.Windows.Input;
  19. using PDF_Master.Helper;
  20. using ComPDFKitViewer.AnnotEvent;
  21. using System.ComponentModel;
  22. using System.IO;
  23. using System.Drawing;
  24. using System.Drawing.Imaging;
  25. using PDF_Master.EventAggregators;
  26. using Prism.Events;
  27. using System.Windows.Media.Imaging;
  28. using PDFReader_WPF.Helper;
  29. namespace PDF_Master.ViewModels.Tools
  30. {
  31. public class TextEditToolContentViewModel: BindableBase, INavigationAware
  32. {
  33. #region 属性与变量
  34. public ViewContentViewModel viewContentViewModel;
  35. private CPDFViewer PDFViewer;
  36. private IEventAggregator events;
  37. private IRegionManager regions;
  38. /// <summary>
  39. /// 用于区分事件的唯一码
  40. /// </summary>
  41. private string unicode;
  42. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  43. private bool _isTextEdit = false;
  44. public bool IsTextEdit
  45. {
  46. get { return _isTextEdit; }
  47. set { SetProperty(ref _isTextEdit, value);
  48. }
  49. }
  50. private bool _isImgEdit = false;
  51. public bool IsImgEdit
  52. {
  53. get { return _isImgEdit; }
  54. set
  55. {
  56. SetProperty(ref _isImgEdit, value);
  57. }
  58. }
  59. private bool _flg=false;
  60. public bool flg
  61. {
  62. get { return _flg; }
  63. set { SetProperty(ref _flg, value); }
  64. }
  65. /// <summary>
  66. /// 替换图片指令
  67. /// </summary>
  68. private bool _ReplaceImgflg = false;
  69. public bool ReplaceImgflg
  70. {
  71. get { return _ReplaceImgflg; }
  72. set { SetProperty(ref _ReplaceImgflg, value); }
  73. }
  74. /// <summary>
  75. /// 完成裁剪图片指令
  76. /// </summary>
  77. private bool _CropImgflg = false;
  78. public bool CropImgflg
  79. {
  80. get { return _CropImgflg; }
  81. set { SetProperty(ref _CropImgflg, value); }
  82. }
  83. /// <summary>
  84. /// 刷新预览图片指令
  85. /// </summary>
  86. private bool _REImgflg = false;
  87. public bool REImgflg
  88. {
  89. get { return _REImgflg; }
  90. set { SetProperty(ref _REImgflg, value); }
  91. }
  92. #endregion
  93. #region Command
  94. // 添加文本、图像
  95. public DelegateCommand<object> AddContentCommand { get; set; }
  96. public DelegateCommand AddTextCommand { get; set; }
  97. public DelegateCommand AddImgCommand { get; set; }
  98. public DelegateCommand EditTextModeCommand { get; set; }
  99. #endregion
  100. #region 初始化
  101. public TextEditToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  102. {
  103. events = eventAggregator;
  104. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  105. regions = regionManager;
  106. InitCommand();
  107. InitBtnToProperty();
  108. }
  109. private void InitCommand()
  110. {
  111. AddContentCommand = new DelegateCommand<object>(AddContent);
  112. AddTextCommand = new DelegateCommand(AddText);
  113. AddImgCommand = new DelegateCommand(AddImg);
  114. EditTextModeCommand = new DelegateCommand(EditTextMode);
  115. }
  116. private void InitBtnToProperty()
  117. {
  118. btnToProperty.Add("Text", "TextEditProperty");
  119. btnToProperty.Add("Image", "ImageEditProperty");
  120. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  121. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  122. }
  123. #endregion
  124. #region 右键菜单
  125. //点击空白处时
  126. private ContextMenu EmptyStateMenu(object sender)
  127. {
  128. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  129. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  130. //粘贴
  131. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  132. //添加文本
  133. customMenu.SetMenuBinding(1, AddTextCommand);
  134. //添加图像
  135. customMenu.SetMenuBinding(2, AddImgCommand);
  136. return popMenu;
  137. }
  138. //选中文字的框
  139. private ContextMenu SelectTextBorder(object sender)
  140. {
  141. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  142. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  143. //复制
  144. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  145. //剪切
  146. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  147. //粘贴
  148. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  149. //删除
  150. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  151. customMenu.SetVisibilityProperty(6, false);
  152. return popMenu;
  153. }
  154. //选中文字内容
  155. private ContextMenu SelectTextContent(object sender)
  156. {
  157. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  158. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  159. //复制
  160. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  161. //剪切
  162. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  163. //粘贴
  164. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  165. //粘贴并匹配样式
  166. customMenu.SetVisibilityProperty(3, false);
  167. //删除
  168. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  169. //全部选定
  170. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  171. return popMenu;
  172. }
  173. //编辑中右键
  174. private ContextMenu EditTextContent(object sender)
  175. {
  176. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  177. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  178. ////复制
  179. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  180. ////剪切
  181. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  182. //全部选定
  183. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  184. //粘贴
  185. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  186. //粘贴并匹配样式
  187. customMenu.SetVisibilityProperty(2, false);
  188. ////删除
  189. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  190. return popMenu;
  191. }
  192. #endregion
  193. #region 图片右键菜单
  194. //选中图像时
  195. private ContextMenu SelectImgPDFEdit(object sender)
  196. {
  197. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  198. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  199. //复制
  200. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  201. //剪切
  202. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  203. //粘贴
  204. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  205. //删除
  206. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  207. //裁剪
  208. customMenu.SetMenuBinding(4, new DelegateCommand(CropMode));
  209. //替换
  210. customMenu.SetMenuBinding(5, new DelegateCommand(ReplaceImg));
  211. //导出
  212. customMenu.SetMenuBinding(6, new DelegateCommand(ExportImg));
  213. return popMenu;
  214. }
  215. //选中裁剪图像时
  216. private ContextMenu CropImgPDFEdit(object sender)
  217. {
  218. var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
  219. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  220. //确认裁剪
  221. customMenu.SetMenuBinding(0, new DelegateCommand(CropImg));
  222. //取消裁剪
  223. customMenu.SetMenuBinding(1, new DelegateCommand(CancelCropImg));
  224. //还原裁剪
  225. customMenu.SetMenuBinding(2, new DelegateCommand(RestoreCropImg));
  226. return popMenu;
  227. }
  228. //多选图片右键
  229. private ContextMenu SelectMoreImage(object sender)
  230. {
  231. var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
  232. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  233. //复制
  234. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  235. //剪切
  236. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  237. //粘贴
  238. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  239. //删除
  240. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  241. //导出
  242. customMenu.SetMenuBinding(4, new DelegateCommand(ExportImg));
  243. return popMenu;
  244. }
  245. #endregion
  246. #region 快捷键
  247. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  248. {
  249. try
  250. {
  251. if (e.Key == Key.Escape)
  252. {
  253. if (PDFViewer != null)
  254. {
  255. //缩小esc的操作范围
  256. if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText)
  257. {
  258. if(PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditText) ==0)
  259. {
  260. PDFViewer.RemovePDFEditEmptyText();
  261. //只有在有画框的时候才进行
  262. if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true)
  263. {
  264. PDFViewer.RemoveTool(false);
  265. }
  266. else if (IsTextEdit == true || IsImgEdit == true)
  267. {
  268. IsImgEdit = false;
  269. IsTextEdit = false;
  270. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  271. //文本和图像都框选
  272. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  273. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  274. PDFViewer.ReloadDocument();
  275. ShowPropertyPanel(false);
  276. }
  277. }
  278. {
  279. PDFViewer.ClearSelectPDFEdit();
  280. PDFViewer.RemovePDFEditEmptyText();
  281. PDFViewer.ReloadDocument();
  282. }
  283. }
  284. else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  285. {
  286. if (PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditImage) == 0)
  287. {
  288. PDFViewer.RemovePDFEditEmptyText();
  289. if (PDFViewer.MouseMode == MouseModes.PDFEdit && PDFViewer.ToolManager.HasTool == true)
  290. {
  291. PDFViewer.RemoveTool(false);
  292. }
  293. else if (IsTextEdit == true || IsImgEdit == true)
  294. {
  295. IsImgEdit = false;
  296. IsTextEdit = false;
  297. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  298. //文本和图像都框选
  299. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  300. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  301. PDFViewer.ReloadDocument();
  302. ShowPropertyPanel(false);
  303. }
  304. }
  305. else
  306. {
  307. PDFViewer.ClearSelectPDFEdit();
  308. PDFViewer.ReloadDocument();
  309. }
  310. }
  311. }
  312. }
  313. }
  314. catch
  315. {
  316. }
  317. }
  318. #endregion
  319. //模式选择进入
  320. public void AddContent(object obj)
  321. {
  322. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  323. //添加付费拦截锁,限制编辑操作
  324. if (!ServiceHelper.IAPBeforeFunction())
  325. {
  326. IsImgEdit = false;
  327. IsTextEdit = false;
  328. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  329. PDFViewer.ReloadDocument();
  330. return;
  331. }
  332. var btn = obj as CustomIconToggleBtn;
  333. EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
  334. }
  335. //编辑按钮逻辑,暂时保留
  336. private void EditTextMode()
  337. {
  338. }
  339. //对应模式的逻辑
  340. private void EnterEditMode(bool isCheckMode,string modeType)
  341. {
  342. if (isCheckMode)
  343. {
  344. if (modeType == "Text")
  345. {
  346. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  347. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditText);
  348. DataTrackingHelper.IsClearEntryPath = true;
  349. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddText");
  350. IsImgEdit = false;
  351. IsTextEdit = true;
  352. //只框选文本
  353. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  354. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  355. PDFViewer.ReloadDocument();
  356. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  357. AddToPropertyPanel("Text", null);
  358. ShowPropertyPanel(true);
  359. }
  360. else
  361. {
  362. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  363. DataTrackingHelper.IsClearEntryPath = true;
  364. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditImage);
  365. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddImage");
  366. IsImgEdit = true;
  367. IsTextEdit = false;
  368. //只框选图像
  369. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  370. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  371. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  372. PDFViewer.ReloadDocument();
  373. AddToPropertyPanel("PropertyPanelContent", null);
  374. ShowPropertyPanel(true);
  375. }
  376. }
  377. else
  378. {
  379. IsImgEdit = false;
  380. IsTextEdit = false;
  381. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  382. //文本和图像都框选
  383. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  384. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  385. PDFViewer.ReloadDocument();
  386. ShowPropertyPanel(false);
  387. }
  388. }
  389. #region Command功能
  390. private void AddText()
  391. {
  392. EnterEditMode(true, "Text");
  393. }
  394. private void AddImg()
  395. {
  396. EnterEditMode(true, "Image");
  397. }
  398. //进入裁剪模式
  399. private void CropMode()
  400. {
  401. if (TextEditEvent != null)
  402. {
  403. TextEditEvent.ClipImage = true;
  404. TextEditEvent.UpdatePDFEditByEventArgs();
  405. flg = true;
  406. }
  407. }
  408. //取消裁剪
  409. private void CancelCropImg()
  410. {
  411. if (TextEditEvent != null)
  412. {
  413. TextEditEvent.ClipImage = false;
  414. TextEditEvent.CancelClip();
  415. flg = false;
  416. TextEditEvent.UpdatePDFEditByEventArgs();
  417. }
  418. }
  419. //还原裁剪
  420. private void RestoreCropImg()
  421. {
  422. if (TextEditEvent != null)
  423. {
  424. TextEditEvent.RestoreClip();
  425. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  426. }
  427. }
  428. //完成裁剪
  429. private void CropImg()
  430. {
  431. CropImgflg = true;
  432. }
  433. //导出图片
  434. private void ExportImg()
  435. {
  436. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  437. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  438. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  439. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  440. {
  441. if (string.IsNullOrEmpty(folder.SelectedPath))
  442. return;
  443. var keyValueList = PDFViewer.GetSelectedImages();
  444. int i = 0;
  445. foreach (var bitmap in keyValueList)
  446. {
  447. foreach (var bitmapItem in bitmap.Value)
  448. {
  449. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  450. i++;
  451. }
  452. }
  453. var strFilePath = folder.SelectedPath + "\\0.png";
  454. CommonHelper.ShowFileBrowser(strFilePath);
  455. }
  456. }
  457. //替换图片
  458. private void ReplaceImg()
  459. {
  460. ReplaceImgflg = true;
  461. }
  462. //选中的图像
  463. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  464. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  465. private void GetImagePreView()
  466. {
  467. try
  468. {
  469. var list = PDFViewer.GetSelectedImages();
  470. if (list != null && list.Count > 0)
  471. {
  472. System.Drawing.Bitmap bitmap = null;
  473. foreach (var item in list)
  474. {
  475. if (item.Value.Count > 0)
  476. {
  477. bitmap = item.Value[0];
  478. break;
  479. }
  480. }
  481. if (bitmap != null)
  482. {
  483. IntPtr ip = bitmap.GetHbitmap();
  484. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  485. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  486. CurrentImg = bitmapSource;
  487. }
  488. }
  489. }
  490. catch
  491. {
  492. }
  493. }
  494. #endregion
  495. //传参数给属性面板
  496. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  497. {
  498. if (btnToProperty.ContainsKey(type))
  499. {
  500. NavigationParameters parameters = new NavigationParameters();
  501. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  502. parameters.Add(ParameterNames.AnnotEvent, e);
  503. parameters.Add(ParameterNames.TextEditToolContentViewModel, this);
  504. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  505. {
  506. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  507. }));
  508. if(e!=null)
  509. {
  510. ShowPropertyPanel(true);
  511. }
  512. else if(e==null&&IsImgEdit==false&&IsTextEdit==false)
  513. {
  514. ShowPropertyPanel(false);
  515. }
  516. }
  517. }
  518. //控制属性面板是否展示
  519. private void ShowPropertyPanel(bool show = true)
  520. {
  521. viewContentViewModel.IsPropertyOpen = show;
  522. }
  523. private void OnViewModel1PropertyChanged(object sender, PropertyChangedEventArgs e)
  524. {
  525. if (e.PropertyName == "TabSelectedIndex")
  526. {
  527. if (viewContentViewModel.ToolTooRow == new GridLength(40))
  528. {
  529. IsImgEdit = false;
  530. IsTextEdit = false;
  531. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  532. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  533. PDFViewer.ReloadDocument();
  534. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  535. AddToPropertyPanel("PropertyPanelContent", null);
  536. ShowPropertyPanel(false);
  537. }
  538. else
  539. {
  540. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  541. PDFViewer.ReloadDocument();
  542. }
  543. }
  544. }
  545. public void OnNavigatedTo(NavigationContext navigationContext)
  546. {
  547. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  548. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  549. if (PDFViewer != null)
  550. {
  551. viewContentViewModel.PropertyChanged += OnViewModel1PropertyChanged;
  552. //左键激活
  553. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  554. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  555. //右键
  556. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  557. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  558. //图片添加
  559. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  560. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  561. //粘贴后选中
  562. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  563. PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler;
  564. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  565. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  566. }
  567. }
  568. private void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
  569. {
  570. PDFViewer.SelectPDFEdit(e,true);
  571. }
  572. protected PDFEditEvent TextEditEvent;
  573. //左键激活逻辑
  574. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  575. {
  576. //添加付费拦截锁,限制编辑操作
  577. if (!ServiceHelper.IAPBeforeFunction())
  578. {
  579. IsImgEdit = false;
  580. IsTextEdit = false;
  581. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  582. PDFViewer.ReloadDocument();
  583. return;
  584. }
  585. if (e != null && e.Count > 0)
  586. {
  587. TextEditEvent=e[0];
  588. bool isText = false;
  589. bool isImg = false;
  590. foreach (var item in e)
  591. {
  592. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  593. {
  594. isImg = true;
  595. }
  596. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  597. {
  598. isText = true;
  599. }
  600. }
  601. if (isText == true && isImg == false)
  602. {
  603. AddToPropertyPanel("Text", e);
  604. }
  605. if (isText == false && isImg == true)
  606. {
  607. AddToPropertyPanel("Image", e);
  608. }
  609. if (isText == true && isImg == true)
  610. {
  611. AddToPropertyPanel("TextAndImage", e);
  612. }
  613. }
  614. else if(IsTextEdit==true)
  615. {
  616. AddToPropertyPanel("PropertyPanelContent", null);
  617. AddToPropertyPanel("Text", null);
  618. }
  619. else
  620. {
  621. AddToPropertyPanel("PropertyPanelContent", null);
  622. }
  623. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  624. }
  625. //右键点击逻辑
  626. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  627. {
  628. if (e == null)
  629. return;
  630. switch (e.CommandType)
  631. {
  632. case CommandType.Context:
  633. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  634. {
  635. e.PopupMenu = EmptyStateMenu(sender);
  636. }
  637. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  638. {
  639. //文字编辑
  640. if (e.PressOnBorder == true)
  641. {
  642. e.PopupMenu = SelectTextBorder(sender);
  643. }
  644. else if(e.PressOnSelectedText == true)
  645. {
  646. e.PopupMenu = SelectTextContent(sender);
  647. }
  648. else if(e.SelectText=="")
  649. {
  650. e.PopupMenu= EditTextContent(sender);
  651. }
  652. else
  653. {
  654. e.PopupMenu = SelectTextContent(sender);
  655. }
  656. }
  657. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  658. {
  659. if (e.IsClipState == true)
  660. {
  661. e.PopupMenu = CropImgPDFEdit(sender);
  662. }
  663. else
  664. {
  665. e.PopupMenu = SelectImgPDFEdit(sender);
  666. }
  667. }
  668. break;
  669. default:
  670. e.DoCommand();
  671. break;
  672. }
  673. if (e.PopupMenu != null)
  674. {
  675. e.Handle = true;
  676. }
  677. flg = e.IsClipState;
  678. }
  679. //图片添加逻辑
  680. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  681. {
  682. //图片创建
  683. if (e.NotifyType == CustomNotifyType.PDFEditImageCreate)
  684. {
  685. OpenFileDialog openFileDialog = new OpenFileDialog();
  686. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  687. if (openFileDialog.ShowDialog() == true)
  688. {
  689. PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500,500);
  690. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  691. }
  692. }
  693. //图片调整大小
  694. else if(e.NotifyType == CustomNotifyType.PDFEditImageResize)
  695. {
  696. REImgflg = true;
  697. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  698. }
  699. }
  700. /// <summary>
  701. /// 压缩
  702. /// </summary>
  703. /// <param name="filePath"></param>
  704. /// <param name="maxwidth"></param>
  705. /// <returns></returns>
  706. public static string CompressImage(string filePath, int maxwidth = 600)
  707. {
  708. try
  709. {
  710. string sourcepath = filePath;
  711. var guid = Guid.NewGuid().ToString();
  712. string folder = Path.Combine(App.CurrentPath, "Temp");
  713. if (!Directory.Exists(folder))
  714. {
  715. Directory.CreateDirectory(folder);
  716. }
  717. var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
  718. Bitmap bitmap = new Bitmap(sourcepath);
  719. var b = bitmap;
  720. //bitmap.Dispose();
  721. double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
  722. scale = Math.Min(scale, 1);
  723. System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
  724. newsize.Width = (int)(scale * b.Width);
  725. newsize.Height = (int)(scale * b.Height);
  726. if (!File.Exists(path))
  727. {
  728. if (CheckTextFile(sourcepath) == FileExtension.PNG)
  729. {
  730. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  731. {
  732. bp.Save(path, ImageFormat.Png);
  733. }
  734. }
  735. else
  736. {
  737. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  738. {
  739. bp.Save(path, ImageFormat.Jpeg);
  740. }
  741. }
  742. }
  743. return path;
  744. }
  745. catch
  746. {
  747. return filePath;
  748. }
  749. }
  750. /// <summary>
  751. /// 根据图片数据判断图片类型
  752. /// </summary>
  753. /// <param name="fileName"></param>
  754. /// <returns></returns>
  755. public static FileExtension CheckTextFile(string fileName)
  756. {
  757. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  758. System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
  759. string fileType = string.Empty; ;
  760. try
  761. {
  762. byte data = br.ReadByte();
  763. fileType += data.ToString();
  764. data = br.ReadByte();
  765. fileType += data.ToString();
  766. FileExtension extension;
  767. try
  768. {
  769. extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
  770. }
  771. catch
  772. {
  773. extension = FileExtension.VALIDFILE;
  774. }
  775. return extension;
  776. }
  777. catch (Exception ex)
  778. {
  779. throw ex;
  780. }
  781. finally
  782. {
  783. if (fs != null)
  784. {
  785. fs.Close();
  786. br.Close();
  787. }
  788. }
  789. }
  790. public bool IsNavigationTarget(NavigationContext navigationContext)
  791. {
  792. return true;
  793. }
  794. public void OnNavigatedFrom(NavigationContext navigationContext)
  795. {
  796. IsImgEdit = false;
  797. IsTextEdit = false;
  798. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  799. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  800. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  801. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  802. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  803. ShowPropertyPanel(false);
  804. }
  805. }
  806. }