TextEditToolContentViewModel.cs 38 KB

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