TextEditToolContentViewModel.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. AddToPropertyPanel("Text", null);
  367. ShowPropertyPanel(true);
  368. }
  369. else
  370. {
  371. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
  372. DataTrackingHelper.IsClearEntryPath = true;
  373. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditImage);
  374. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddImage");
  375. IsImgEdit = true;
  376. IsTextEdit = false;
  377. //只框选图像
  378. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  379. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  380. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  381. PDFViewer.ReloadDocument();
  382. AddToPropertyPanel("PropertyPanelContentEdit", null);
  383. ShowPropertyPanel(true);
  384. }
  385. }
  386. else
  387. {
  388. IsImgEdit = false;
  389. IsTextEdit = false;
  390. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  391. //文本和图像都框选
  392. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  393. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  394. PDFViewer.ReloadDocument();
  395. ShowPropertyPanel(false);
  396. }
  397. }
  398. #region Command功能
  399. private void AddText()
  400. {
  401. EnterEditMode(true, "Text");
  402. }
  403. private void AddImg()
  404. {
  405. EnterEditMode(true, "Image");
  406. }
  407. //进入裁剪模式
  408. private void CropMode()
  409. {
  410. if (TextEditEvent != null)
  411. {
  412. TextEditEvent.ClipImage = true;
  413. TextEditEvent.UpdatePDFEditByEventArgs();
  414. flg = true;
  415. }
  416. }
  417. //取消裁剪
  418. private void CancelCropImg()
  419. {
  420. if (TextEditEvent != null)
  421. {
  422. TextEditEvent.ClipImage = false;
  423. TextEditEvent.CancelClip();
  424. flg = false;
  425. TextEditEvent.UpdatePDFEditByEventArgs();
  426. }
  427. }
  428. //还原裁剪
  429. private void RestoreCropImg()
  430. {
  431. if (TextEditEvent != null)
  432. {
  433. TextEditEvent.RestoreClip();
  434. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  435. }
  436. }
  437. //完成裁剪
  438. private void CropImg()
  439. {
  440. CropImgflg = true;
  441. }
  442. //导出图片
  443. private void ExportImg()
  444. {
  445. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  446. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  447. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  448. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  449. {
  450. if (string.IsNullOrEmpty(folder.SelectedPath))
  451. return;
  452. var keyValueList = PDFViewer.GetSelectedImages();
  453. int i = 0;
  454. foreach (var bitmap in keyValueList)
  455. {
  456. foreach (var bitmapItem in bitmap.Value)
  457. {
  458. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  459. i++;
  460. }
  461. }
  462. var strFilePath = folder.SelectedPath + "\\0.png";
  463. CommonHelper.ShowFileBrowser(strFilePath);
  464. }
  465. }
  466. //替换图片
  467. private void ReplaceImg()
  468. {
  469. ReplaceImgflg = true;
  470. }
  471. //选中的图像
  472. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  473. public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
  474. private void GetImagePreView()
  475. {
  476. try
  477. {
  478. var list = PDFViewer.GetSelectedImages();
  479. if (list != null && list.Count > 0)
  480. {
  481. System.Drawing.Bitmap bitmap = null;
  482. foreach (var item in list)
  483. {
  484. if (item.Value.Count > 0)
  485. {
  486. bitmap = item.Value[0];
  487. break;
  488. }
  489. }
  490. if (bitmap != null)
  491. {
  492. IntPtr ip = bitmap.GetHbitmap();
  493. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  494. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  495. CurrentImg = bitmapSource;
  496. }
  497. }
  498. }
  499. catch
  500. {
  501. }
  502. }
  503. #endregion
  504. //传参数给属性面板
  505. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  506. {
  507. if (btnToProperty.ContainsKey(type))
  508. {
  509. NavigationParameters parameters = new NavigationParameters();
  510. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  511. parameters.Add(ParameterNames.AnnotEvent, e);
  512. parameters.Add(ParameterNames.TextEditToolContentViewModel, this);
  513. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  514. {
  515. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  516. }));
  517. if(e!=null)
  518. {
  519. ShowPropertyPanel(true);
  520. }
  521. else if(e==null&&IsImgEdit==false&&IsTextEdit==false)
  522. {
  523. ShowPropertyPanel(false);
  524. }
  525. }
  526. }
  527. //控制属性面板是否展示
  528. private void ShowPropertyPanel(bool show = true)
  529. {
  530. viewContentViewModel.IsPropertyOpen = show;
  531. }
  532. private void OnViewModel1PropertyChanged(object sender, PropertyChangedEventArgs e)
  533. {
  534. if (e.PropertyName == "TabSelectedIndex")
  535. {
  536. if (viewContentViewModel.ToolTooRow == new GridLength(40))
  537. {
  538. IsImgEdit = false;
  539. IsTextEdit = false;
  540. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
  541. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  542. PDFViewer.ReloadDocument();
  543. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  544. AddToPropertyPanel("PropertyPanelContentEdit", null);
  545. ShowPropertyPanel(false);
  546. }
  547. else
  548. {
  549. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  550. PDFViewer.ReloadDocument();
  551. }
  552. }
  553. }
  554. public void OnNavigatedTo(NavigationContext navigationContext)
  555. {
  556. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  557. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  558. if (PDFViewer != null)
  559. {
  560. viewContentViewModel.PropertyChanged += OnViewModel1PropertyChanged;
  561. //左键激活
  562. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  563. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  564. //右键
  565. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  566. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  567. //图片添加
  568. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  569. PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
  570. //粘贴后选中
  571. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  572. PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler;
  573. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  574. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  575. }
  576. }
  577. private void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
  578. {
  579. PDFViewer.ClearSelectPDFEdit();
  580. PDFViewer.SelectPDFEdit(e,true);
  581. }
  582. protected PDFEditEvent TextEditEvent;
  583. //左键激活逻辑
  584. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  585. {
  586. PDFViewer.ToolManager.IgnoreMoveDistance =5;
  587. //判断是否已退出登录,限制编辑操作
  588. if (!App.IsLogin)
  589. {
  590. IsImgEdit = false;
  591. IsTextEdit = false;
  592. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  593. PDFViewer.ReloadDocument();
  594. App.mainWindowViewModel.OpenLogin();
  595. return;
  596. }
  597. if (e != null && e.Count > 0)
  598. {
  599. TextEditEvent=e[0];
  600. bool isText = false;
  601. bool isImg = false;
  602. foreach (var item in e)
  603. {
  604. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  605. {
  606. isImg = true;
  607. }
  608. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  609. {
  610. isText = true;
  611. }
  612. }
  613. if (isText == true && isImg == false)
  614. {
  615. AddToPropertyPanel("Text", e);
  616. }
  617. if (isText == false && isImg == true)
  618. {
  619. AddToPropertyPanel("Image", e);
  620. }
  621. if (isText == true && isImg == true)
  622. {
  623. AddToPropertyPanel("TextAndImage", e);
  624. }
  625. }
  626. else if(IsTextEdit==true)
  627. {
  628. //单击添加文本
  629. PDFViewer.ToolManager.EnableClickCreate = true;
  630. PDFViewer.ToolManager.ClickCreateWidth = 60;
  631. PDFViewer.ToolManager.ClickCreateHeight = 30;
  632. AddToPropertyPanel("Text", null);
  633. }
  634. else
  635. {
  636. AddToPropertyPanel("PropertyPanelContentEdit", null);
  637. }
  638. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  639. }
  640. //右键点击逻辑
  641. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  642. {
  643. if (e == null)
  644. return;
  645. switch (e.CommandType)
  646. {
  647. case CommandType.Context:
  648. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  649. {
  650. e.PopupMenu = EmptyStateMenu(sender);
  651. }
  652. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  653. {
  654. //文字编辑
  655. if (e.PressOnBorder == true)
  656. {
  657. e.PopupMenu = SelectTextBorder(sender);
  658. }
  659. else if(e.PressOnSelectedText == true)
  660. {
  661. e.PopupMenu = SelectTextContent(sender);
  662. }
  663. else if(e.SelectText=="")
  664. {
  665. e.PopupMenu= EditTextContent(sender);
  666. }
  667. else
  668. {
  669. e.PopupMenu = SelectTextContent(sender);
  670. }
  671. }
  672. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  673. {
  674. if (e.IsClipState == true)
  675. {
  676. e.PopupMenu = CropImgPDFEdit(sender);
  677. }
  678. else
  679. {
  680. e.PopupMenu = SelectImgPDFEdit(sender);
  681. }
  682. }
  683. break;
  684. default:
  685. e.DoCommand();
  686. break;
  687. }
  688. if (e.PopupMenu != null)
  689. {
  690. e.Handle = true;
  691. }
  692. flg = e.IsClipState;
  693. }
  694. //图片添加逻辑
  695. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  696. {
  697. //图片创建
  698. if (e.NotifyType == CustomNotifyType.PDFEditImageCreate)
  699. {
  700. OpenFileDialog openFileDialog = new OpenFileDialog();
  701. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  702. if (openFileDialog.ShowDialog() == true)
  703. {
  704. PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500,500);
  705. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  706. }
  707. }
  708. //图片调整大小
  709. else if(e.NotifyType == CustomNotifyType.PDFEditImageResize)
  710. {
  711. REImgflg = true;
  712. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  713. }
  714. }
  715. /// <summary>
  716. /// 压缩
  717. /// </summary>
  718. /// <param name="filePath"></param>
  719. /// <param name="maxwidth"></param>
  720. /// <returns></returns>
  721. public static string CompressImage(string filePath, int maxwidth = 600)
  722. {
  723. try
  724. {
  725. string sourcepath = filePath;
  726. var guid = Guid.NewGuid().ToString();
  727. string folder = Path.Combine(App.CurrentPath, "Temp");
  728. if (!Directory.Exists(folder))
  729. {
  730. Directory.CreateDirectory(folder);
  731. }
  732. var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
  733. Bitmap bitmap = new Bitmap(sourcepath);
  734. var b = bitmap;
  735. //bitmap.Dispose();
  736. double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
  737. scale = Math.Min(scale, 1);
  738. System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
  739. newsize.Width = (int)(scale * b.Width);
  740. newsize.Height = (int)(scale * b.Height);
  741. if (!File.Exists(path))
  742. {
  743. if (CheckTextFile(sourcepath) == FileExtension.PNG)
  744. {
  745. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  746. {
  747. bp.Save(path, ImageFormat.Png);
  748. }
  749. }
  750. else
  751. {
  752. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  753. {
  754. bp.Save(path, ImageFormat.Jpeg);
  755. }
  756. }
  757. }
  758. return path;
  759. }
  760. catch
  761. {
  762. return filePath;
  763. }
  764. }
  765. /// <summary>
  766. /// 根据图片数据判断图片类型
  767. /// </summary>
  768. /// <param name="fileName"></param>
  769. /// <returns></returns>
  770. public static FileExtension CheckTextFile(string fileName)
  771. {
  772. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  773. System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
  774. string fileType = string.Empty; ;
  775. try
  776. {
  777. byte data = br.ReadByte();
  778. fileType += data.ToString();
  779. data = br.ReadByte();
  780. fileType += data.ToString();
  781. FileExtension extension;
  782. try
  783. {
  784. extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
  785. }
  786. catch
  787. {
  788. extension = FileExtension.VALIDFILE;
  789. }
  790. return extension;
  791. }
  792. catch (Exception ex)
  793. {
  794. throw ex;
  795. }
  796. finally
  797. {
  798. if (fs != null)
  799. {
  800. fs.Close();
  801. br.Close();
  802. }
  803. }
  804. }
  805. public bool IsNavigationTarget(NavigationContext navigationContext)
  806. {
  807. return true;
  808. }
  809. public void OnNavigatedFrom(NavigationContext navigationContext)
  810. {
  811. IsImgEdit = false;
  812. IsTextEdit = false;
  813. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  814. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  815. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  816. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  817. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  818. ShowPropertyPanel(false);
  819. }
  820. }
  821. }