TextEditToolContentViewModel.cs 34 KB

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