TextEditToolContentViewModel.cs 34 KB

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