TextEditPropertyViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. namespace PDF_Office.ViewModels.PropertyPanel
  19. {
  20. public class TextEditPropertyViewModel : BindableBase, INavigationAware
  21. {
  22. #region 属性
  23. #region 编辑PDF全局
  24. private bool _isTextEdit = true;
  25. public bool IsTextEdit
  26. {
  27. get { return _isTextEdit; }
  28. set
  29. {
  30. SetProperty(ref _isTextEdit, value);
  31. }
  32. }
  33. #endregion
  34. #region 文本属性
  35. private double _angle;
  36. public double Angle
  37. {
  38. get { return _angle; }
  39. set
  40. {
  41. SetProperty(ref _angle, value);
  42. }
  43. }
  44. private Brush selectColor = new SolidColorBrush(Colors.Black);
  45. public Brush SelectColor
  46. {
  47. get { return selectColor; }
  48. set
  49. {
  50. SetProperty(ref selectColor, value);
  51. if (TextEditEvent != null)
  52. {
  53. bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A ||
  54. TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B ||
  55. TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G ||
  56. TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R;
  57. if (isok)
  58. {
  59. TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color;
  60. TextEditEvent.UpdatePDFEditByEventArgs();
  61. }
  62. }
  63. }
  64. }
  65. private FontFamily fontFamily = new FontFamily("Courier");
  66. public FontFamily TextFontFamily
  67. {
  68. get { return fontFamily; }
  69. set
  70. {
  71. SetProperty(ref fontFamily, value);
  72. if (TextEditEvent != null)
  73. {
  74. TextEditEvent.FontFamily = fontFamily;
  75. TextEditEvent.UpdatePDFEditByEventArgs();
  76. }
  77. }
  78. }
  79. private FontWeight fontWeights = FontWeights.Normal;
  80. public FontWeight TextFontWeights
  81. {
  82. get { return fontWeights; }
  83. set
  84. {
  85. SetProperty(ref fontWeights, value);
  86. if (TextEditEvent != null)
  87. {
  88. TextEditEvent.FontWeight = fontWeights;
  89. TextEditEvent.UpdatePDFEditByEventArgs();
  90. }
  91. }
  92. }
  93. private FontStyle fontStyle = FontStyles.Normal;
  94. public FontStyle TextFontStyle
  95. {
  96. get { return fontStyle; }
  97. set
  98. {
  99. SetProperty(ref fontStyle, value);
  100. if (TextEditEvent != null)
  101. {
  102. TextEditEvent.FontStyle = fontStyle;
  103. TextEditEvent.UpdatePDFEditByEventArgs();
  104. }
  105. }
  106. }
  107. private int fontSize = 24;
  108. public int TextFontSize
  109. {
  110. get { return fontSize; }
  111. set
  112. {
  113. SetProperty(ref fontSize, value);
  114. if (TextEditEvent != null)
  115. {
  116. TextEditEvent.FontSize = fontSize;
  117. TextEditEvent.UpdatePDFEditByEventArgs();
  118. }
  119. }
  120. }
  121. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  122. public List<FontStyleItem> FontStyleList
  123. {
  124. get { return fontStyleList; }
  125. set
  126. {
  127. SetProperty(ref fontStyleList, value);
  128. }
  129. }
  130. #endregion
  131. #region 图像属性
  132. private bool _isCrop = false;
  133. public bool IsCrop
  134. {
  135. get { return _isCrop; }
  136. set
  137. {
  138. SetProperty(ref _isCrop, value);
  139. }
  140. }
  141. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  142. public System.Windows.Media.Imaging.BitmapSource CurrentImg
  143. {
  144. get { return _currentImg; }
  145. set
  146. {
  147. SetProperty(ref _currentImg, value);
  148. }
  149. }
  150. #endregion
  151. #endregion
  152. #region Command
  153. #region 文本Command
  154. public DelegateCommand AddTextCommand { get; set; }
  155. public DelegateCommand AddImgCommand { get; set; }
  156. public DelegateCommand<object> SelectedColorCommand { get; set; }
  157. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  158. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  159. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  160. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  161. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  162. #endregion
  163. #region 图像Command
  164. public DelegateCommand ReplaceImgCommand { get; set; }
  165. public DelegateCommand ExportImgCommand { get; set; }
  166. public DelegateCommand CropImgCommand { get; set; }
  167. public DelegateCommand ImgAlignCheckedCommand { get; set; }
  168. /// <summary>
  169. /// 逆时针旋转
  170. /// </summary>
  171. public DelegateCommand AntiClockwiseCommand { get; set; }
  172. /// <summary>
  173. /// 顺时针旋转
  174. /// </summary>
  175. public DelegateCommand ClockwiseCommand { get; set; }
  176. public DelegateCommand CropModeCommand { get; set; }
  177. public DelegateCommand CancelCropCommand { get; set; }
  178. #endregion
  179. #endregion
  180. private ComPDFKitViewer.PDFEditEvent TextEditEvent;
  181. public TextEditPropertyViewModel()
  182. {
  183. InitVariable();
  184. InitCommand();
  185. }
  186. private void InitVariable()
  187. {
  188. InitFontStyles();
  189. }
  190. private void InitFontStyles()
  191. {
  192. FontStyleList = LoadFontStyle.Load();
  193. }
  194. private void InitCommand()
  195. {
  196. AddTextCommand = new DelegateCommand(AddText);
  197. AddImgCommand = new DelegateCommand(AddImg);
  198. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  199. ExportImgCommand = new DelegateCommand(ExportImg);
  200. CropImgCommand = new DelegateCommand(CropImg);
  201. ImgAlignCheckedCommand = new DelegateCommand(ImgAlignChecked);
  202. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  203. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  204. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
  205. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
  206. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  207. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  208. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  209. ClockwiseCommand = new DelegateCommand(Clockwise);
  210. CropModeCommand = new DelegateCommand(CropMode);
  211. CancelCropCommand = new DelegateCommand(CancelCropImg);
  212. }
  213. private void ImgAlignChecked()
  214. {
  215. }
  216. private void CancelCropImg()
  217. {
  218. if (TextEditEvent != null)
  219. {
  220. TextEditEvent.ClipImage = false;
  221. TextEditEvent.CancelClip();
  222. TextEditEvent.UpdatePDFEditByEventArgs();
  223. IsCrop = false;
  224. }
  225. }
  226. private void CropImg()
  227. {
  228. if(TextEditEvent != null)
  229. {
  230. TextEditEvent.SaveClip();
  231. TextEditEvent.ClipImage = false;
  232. TextEditEvent.UpdatePDFEditByEventArgs();
  233. IsCrop = false;
  234. }
  235. }
  236. private void ExportImg()
  237. {
  238. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  239. //SaveFileDialog saveFileDialog = new SaveFileDialog();
  240. //saveFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  241. //saveFileDialog.FileName = "编辑PDF导出图片";
  242. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  243. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  244. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  245. {
  246. if (string.IsNullOrEmpty(folder.SelectedPath))
  247. return;
  248. var keyValueList = PDFViewer.GetSelectedImages();
  249. int i = 0;
  250. foreach (var bitmap in keyValueList)
  251. {
  252. foreach(var bitmapItem in bitmap.Value)
  253. {
  254. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  255. i++;
  256. }
  257. }
  258. }
  259. }
  260. private void AddImg()
  261. {
  262. OpenFileDialog openFileDialog = new OpenFileDialog();
  263. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  264. openFileDialog.Multiselect = true;
  265. if ((bool)openFileDialog.ShowDialog())
  266. {
  267. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  268. {
  269. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  270. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  271. }
  272. }
  273. }
  274. private void ReplaceImg()
  275. {
  276. OpenFileDialog openFileDialog = new OpenFileDialog();
  277. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  278. openFileDialog.Multiselect = true;
  279. if ((bool)openFileDialog.ShowDialog())
  280. {
  281. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  282. {
  283. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  284. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  285. TextEditEvent.UpdatePDFEditByEventArgs();
  286. }
  287. }
  288. }
  289. private void AddText()
  290. {
  291. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  292. }
  293. private void TextAlignChecked(object obj)
  294. {
  295. if((string)obj != null && TextEditEvent != null)
  296. {
  297. switch((string)obj)
  298. {
  299. case "AlignLeft":
  300. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  301. break;
  302. case "AlignCenter":
  303. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  304. break;
  305. case "AlignRight":
  306. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  307. break;
  308. case "Align":
  309. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  310. break;
  311. }
  312. TextEditEvent.UpdatePDFEditByEventArgs();
  313. }
  314. }
  315. private void Clockwise()
  316. {
  317. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate + 90;
  318. TextEditEvent.UpdatePDFEditByEventArgs();
  319. }
  320. private void AntiClockwise()
  321. {
  322. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate - 90;
  323. TextEditEvent.UpdatePDFEditByEventArgs();
  324. }
  325. private void CropMode()
  326. {
  327. IsCrop = true;
  328. if(TextEditEvent != null)
  329. {
  330. TextEditEvent.ClipImage = true;
  331. TextEditEvent.UpdatePDFEditByEventArgs();
  332. }
  333. }
  334. private void SelectedColor(object obj)
  335. {
  336. if (obj != null)
  337. {
  338. var colorValue = (Color)obj;
  339. if (colorValue != null)
  340. {
  341. SelectColor = new SolidColorBrush(colorValue);
  342. }
  343. }
  344. }
  345. private void SelectedFontStyle(object obj)
  346. {
  347. if (obj != null && (FontStyleItem)obj != null)
  348. {
  349. var item = (FontStyleItem)obj;
  350. }
  351. }
  352. private void FontFamilyChanged(object obj)
  353. {
  354. if (obj != null)
  355. {
  356. if ((int)obj > -1)
  357. {
  358. if ((int)obj == 0)
  359. {
  360. TextFontFamily = new FontFamily("Courier");
  361. }
  362. if ((int)obj == 1)
  363. {
  364. TextFontFamily = new FontFamily("Helvetica");
  365. }
  366. if ((int)obj == 2)
  367. {
  368. TextFontFamily = new FontFamily("Times");
  369. }
  370. }
  371. }
  372. }
  373. private void FontStyleChanged(object obj)
  374. {
  375. if (obj != null)
  376. {
  377. var item = (ComboBoxItem)obj;
  378. var content = (string)item.Content;
  379. if (content != null)
  380. {
  381. if (content == "Regular")
  382. {
  383. TextFontWeights = FontWeights.Normal;
  384. TextFontStyle = FontStyles.Normal;
  385. }
  386. if (content == "Bold")
  387. {
  388. TextFontWeights = FontWeights.Bold;
  389. TextFontStyle = FontStyles.Normal;
  390. }
  391. if (content == "Italic")
  392. {
  393. TextFontWeights = FontWeights.Normal;
  394. TextFontStyle = FontStyles.Italic;
  395. }
  396. if (content == "Bold Italic")
  397. {
  398. TextFontWeights = FontWeights.Bold;
  399. TextFontStyle = FontStyles.Italic;
  400. }
  401. }
  402. }
  403. }
  404. private void FontSizeChanged(object obj)
  405. {
  406. if (obj != null)
  407. {
  408. var item = (ComboBoxItem)obj;
  409. var content = (string)item.Content;
  410. if (content != null)
  411. {
  412. var intData = int.Parse(content);
  413. TextFontSize = intData;
  414. }
  415. }
  416. }
  417. public void OnNavigatedTo(NavigationContext navigationContext)
  418. {
  419. }
  420. public bool IsNavigationTarget(NavigationContext navigationContext)
  421. {
  422. return true;
  423. }
  424. private CPDFViewer PDFViewer;
  425. public void OnNavigatedFrom(NavigationContext navigationContext)
  426. {
  427. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  428. if(PDFViewer != null)
  429. {
  430. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  431. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  432. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  433. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  434. }
  435. }
  436. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  437. {
  438. ContextMenu popMenu = null;
  439. switch (e.CommandType)
  440. {
  441. case CommandType.Context:
  442. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  443. {
  444. popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  445. if(popMenu != null && popMenu.Items.Count == 4)
  446. {
  447. //复制
  448. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  449. menuItem.CommandTarget = (UIElement)sender;
  450. menuItem.Command = ApplicationCommands.Copy;
  451. //添加文本
  452. menuItem = popMenu.Items[1] as MenuItem;
  453. menuItem.CommandTarget = (UIElement)sender;
  454. menuItem.Command = AddTextCommand;
  455. //添加图像
  456. menuItem = popMenu.Items[2] as MenuItem;
  457. menuItem.CommandTarget = (UIElement)sender;
  458. menuItem.Command = AddImgCommand;
  459. //删除
  460. menuItem = popMenu.Items[3] as MenuItem;
  461. menuItem.CommandTarget = (UIElement)sender;
  462. menuItem.Command = ApplicationCommands.Delete;
  463. e.PopupMenu = popMenu;
  464. if (e.PopupMenu != null)
  465. {
  466. e.Handle = true;
  467. }
  468. }
  469. }
  470. else if(e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  471. {
  472. popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  473. if (popMenu != null && popMenu.Items.Count == 8)
  474. {
  475. //复制
  476. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  477. menuItem.CommandTarget = (UIElement)sender;
  478. menuItem.Command = ApplicationCommands.Copy;
  479. //剪切
  480. menuItem = popMenu.Items[1] as MenuItem;
  481. menuItem.CommandTarget = (UIElement)sender;
  482. menuItem.Command = ApplicationCommands.Cut;
  483. //粘贴
  484. menuItem = popMenu.Items[2] as MenuItem;
  485. menuItem.CommandTarget = (UIElement)sender;
  486. menuItem.Command = ApplicationCommands.Paste;
  487. //删除
  488. menuItem = popMenu.Items[3] as MenuItem;
  489. menuItem.CommandTarget = (UIElement)sender;
  490. menuItem.Command = ApplicationCommands.Delete;
  491. //裁剪
  492. menuItem = popMenu.Items[4] as MenuItem;
  493. menuItem.CommandTarget = (UIElement)sender;
  494. menuItem.Command = CropModeCommand;
  495. //替换
  496. menuItem = popMenu.Items[5] as MenuItem;
  497. menuItem.CommandTarget = (UIElement)sender;
  498. menuItem.Command = ReplaceImgCommand;
  499. //导出
  500. menuItem = popMenu.Items[6] as MenuItem;
  501. menuItem.CommandTarget = (UIElement)sender;
  502. menuItem.Command = ExportImgCommand;
  503. //排列
  504. menuItem = popMenu.Items[7] as MenuItem;
  505. menuItem.CommandTarget = (UIElement)sender;
  506. menuItem.Command = ApplicationCommands.Cut;
  507. e.PopupMenu = popMenu;
  508. if (e.PopupMenu != null)
  509. {
  510. e.Handle = true;
  511. }
  512. }
  513. }
  514. break;
  515. default:
  516. e.DoCommand();
  517. break;
  518. }
  519. }
  520. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  521. {
  522. if(e != null && e.Count > 0)
  523. {
  524. IsTextEdit = (e[0].EditType == ComPDFKit.PDFPage.CPDFEditType.EditText);
  525. TextEditEvent = e[0];
  526. if (IsTextEdit == false)
  527. {
  528. var list = PDFViewer.GetSelectedImages();
  529. if(list != null && list.Count > 0)
  530. {
  531. System.Drawing.Bitmap bitmap = null;
  532. foreach (var item in list)
  533. {
  534. if(item.Value.Count>0)
  535. {
  536. bitmap = item.Value[0];
  537. break;
  538. }
  539. }
  540. if(bitmap != null)
  541. {
  542. IntPtr ip = bitmap.GetHbitmap();
  543. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  544. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  545. CurrentImg = bitmapSource;
  546. }
  547. }
  548. }
  549. }
  550. else
  551. {
  552. IsTextEdit = true;
  553. }
  554. }
  555. }
  556. }