TextEditToolContentViewModel.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. //延时 =》添加图片,弹出系统文件弹窗,双击选中图片后添加到页面上,需要选中图片
  630. await Task.Delay(50);
  631. PDFViewer.SelectPDFEdit(e, true);
  632. }
  633. protected PDFEditEvent TextEditEvent;
  634. //左键激活逻辑
  635. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  636. {
  637. PDFViewer.ToolManager.IgnoreMoveDistance = 5;
  638. //判断是否已退出登录,限制编辑操作
  639. if (!App.IsLogin)
  640. {
  641. IsImgEdit = false;
  642. IsTextEdit = false;
  643. PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
  644. PDFViewer.ReloadDocument();
  645. App.mainWindowViewModel.OpenLogin();
  646. return;
  647. }
  648. //从配置文件拿样式
  649. pdfDefaultEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
  650. if (e != null && e.Count > 0)
  651. {
  652. TextEditEvent = e[0];
  653. bool isText = false;
  654. bool isImg = false;
  655. foreach (var item in e)
  656. {
  657. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  658. {
  659. isImg = true;
  660. }
  661. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  662. {
  663. isText = true;
  664. }
  665. }
  666. if (isText == true && isImg == false)
  667. {
  668. if (e.Count == 1 && string.IsNullOrEmpty(e[0].FontName))
  669. {
  670. e = SynchronizeToPropertyPanel(e, defaultEdit);
  671. }
  672. AddToPropertyPanel("Text", e);
  673. }
  674. if (isText == false && isImg == true)
  675. {
  676. AddToPropertyPanel("Image", e);
  677. }
  678. if (isText == true && isImg == true)
  679. {
  680. AddToPropertyPanel("TextAndImage", e);
  681. }
  682. }
  683. ///未事先设置 if里面的内容,单击添加文本会失败
  684. //else if(IsTextEdit==true)
  685. //{
  686. // //单击添加文本
  687. // PDFViewer.ToolManager.EnableClickCreate = true;
  688. // PDFViewer.ToolManager.ClickCreateWidth = 60;
  689. // PDFViewer.ToolManager.ClickCreateHeight = 30;
  690. // AddToPropertyPanel("Text", null);
  691. //}
  692. else
  693. {
  694. Trace.WriteLine("PropertyPanelContentEdit");
  695. AddToPropertyPanel("PropertyPanelContentEdit", null);
  696. }
  697. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  698. }
  699. /// <summary>
  700. /// 拿配置文件的属性,刷新属性面板
  701. /// </summary>
  702. /// <param name="e"></param>
  703. /// <param name="defaultEdit"></param>
  704. /// <returns></returns>
  705. private List<PDFEditEvent> SynchronizeToPropertyPanel(List<PDFEditEvent> e, DefaultEditProperty defaultEdit)
  706. {
  707. List<PDFEditEvent> editEvents = new List<PDFEditEvent>();
  708. PDFEditEvent editEvent = e[0];
  709. if (defaultEdit != null)
  710. {
  711. editEvent.FontName = defaultEdit.FontName;
  712. editEvent.FontSize = defaultEdit.FontSize;
  713. editEvent.TextAlign = defaultEdit.TextAlign;
  714. editEvent.AutoBlock = false;
  715. //editEvent.ClipImage = defaultEdit.ClipImage;
  716. //editEvent.EditType = defaultEdit.EditType;
  717. editEvent.FontColor = defaultEdit.FontColor;
  718. //editEvent.HorizontalMirror = defaultEdit.HorizontalMirror;
  719. //editEvent.VerticalMirror = defaultEdit.VerticalMirror;
  720. editEvent.IsBold = defaultEdit.IsBold;
  721. editEvent.IsItalic = defaultEdit.IsItalic;
  722. //editEvent.ReplaceImagePath = defaultEdit.ReplaceImagePath;
  723. //editEvent.Rotate = defaultEdit.Rotate;
  724. //editEvent.Transparency = defaultEdit.Transparency;
  725. //PDFViewer.SetPDFEditParam(editEvent);
  726. e[0] = editEvent;
  727. editEvents = e;
  728. }
  729. return editEvents;
  730. }
  731. //右键点击逻辑
  732. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  733. {
  734. if (e == null)
  735. return;
  736. switch (e.CommandType)
  737. {
  738. case CommandType.Context:
  739. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
  740. {
  741. e.PopupMenu = EmptyStateMenu(sender);
  742. }
  743. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  744. {
  745. //文字编辑
  746. if (e.PressOnBorder == true)
  747. {
  748. e.PopupMenu = SelectTextBorder(sender);
  749. }
  750. else if (e.PressOnSelectedText == true)
  751. {
  752. e.PopupMenu = SelectTextContent(sender);
  753. }
  754. else if (e.SelectText == "")
  755. {
  756. e.PopupMenu = EditTextContent(sender);
  757. }
  758. else
  759. {
  760. e.PopupMenu = SelectTextContent(sender);
  761. }
  762. }
  763. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  764. {
  765. if (e.IsClipState == true)
  766. {
  767. e.PopupMenu = CropImgPDFEdit(sender);
  768. }
  769. else
  770. {
  771. e.PopupMenu = SelectImgPDFEdit(sender);
  772. }
  773. }
  774. break;
  775. default:
  776. e.DoCommand();
  777. break;
  778. }
  779. if (e.PopupMenu != null)
  780. {
  781. e.Handle = true;
  782. }
  783. flg = e.IsClipState;
  784. }
  785. //图片添加逻辑
  786. private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
  787. {
  788. //图片创建
  789. if (e.NotifyType == CustomNotifyType.PDFEditImageCreate)
  790. {
  791. OpenFileDialog openFileDialog = new OpenFileDialog();
  792. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  793. if (openFileDialog.ShowDialog() == true)
  794. {
  795. PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500, 500);
  796. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  797. }
  798. }
  799. //图片调整大小
  800. else if (e.NotifyType == CustomNotifyType.PDFEditImageResize)
  801. {
  802. REImgflg = true;
  803. events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  804. }
  805. }
  806. /// <summary>
  807. /// 压缩
  808. /// </summary>
  809. /// <param name="filePath"></param>
  810. /// <param name="maxwidth"></param>
  811. /// <returns></returns>
  812. public static string CompressImage(string filePath, int maxwidth = 600)
  813. {
  814. try
  815. {
  816. string sourcepath = filePath;
  817. var guid = Guid.NewGuid().ToString();
  818. string folder = Path.Combine(App.CurrentPath, "Temp");
  819. if (!Directory.Exists(folder))
  820. {
  821. Directory.CreateDirectory(folder);
  822. }
  823. var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
  824. Bitmap bitmap = new Bitmap(sourcepath);
  825. var b = bitmap;
  826. //bitmap.Dispose();
  827. double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
  828. scale = Math.Min(scale, 1);
  829. System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
  830. newsize.Width = (int)(scale * b.Width);
  831. newsize.Height = (int)(scale * b.Height);
  832. if (!File.Exists(path))
  833. {
  834. if (CheckTextFile(sourcepath) == FileExtension.PNG)
  835. {
  836. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  837. {
  838. bp.Save(path, ImageFormat.Png);
  839. }
  840. }
  841. else
  842. {
  843. using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
  844. {
  845. bp.Save(path, ImageFormat.Jpeg);
  846. }
  847. }
  848. }
  849. return path;
  850. }
  851. catch
  852. {
  853. return filePath;
  854. }
  855. }
  856. /// <summary>
  857. /// 根据图片数据判断图片类型
  858. /// </summary>
  859. /// <param name="fileName"></param>
  860. /// <returns></returns>
  861. public static FileExtension CheckTextFile(string fileName)
  862. {
  863. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  864. System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
  865. string fileType = string.Empty; ;
  866. try
  867. {
  868. byte data = br.ReadByte();
  869. fileType += data.ToString();
  870. data = br.ReadByte();
  871. fileType += data.ToString();
  872. FileExtension extension;
  873. try
  874. {
  875. extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
  876. }
  877. catch
  878. {
  879. extension = FileExtension.VALIDFILE;
  880. }
  881. return extension;
  882. }
  883. catch (Exception ex)
  884. {
  885. throw ex;
  886. }
  887. finally
  888. {
  889. if (fs != null)
  890. {
  891. fs.Close();
  892. br.Close();
  893. }
  894. }
  895. }
  896. public bool IsNavigationTarget(NavigationContext navigationContext)
  897. {
  898. return true;
  899. }
  900. public void OnNavigatedFrom(NavigationContext navigationContext)
  901. {
  902. IsImgEdit = false;
  903. IsTextEdit = false;
  904. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  905. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  906. PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
  907. PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
  908. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  909. ShowPropertyPanel(false);
  910. }
  911. }
  912. }