TextEditToolContentViewModel.cs 38 KB

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