TextEditPropertyViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 _isSelectTextAndImg = false;
  25. public bool IsSelectTextAndImg
  26. {
  27. get { return _isSelectTextAndImg; }
  28. set
  29. {
  30. SetProperty(ref _isSelectTextAndImg, value);
  31. }
  32. }
  33. private bool _isLayoutAvgAlign = false;
  34. public bool IsLayoutAvgAlign
  35. {
  36. get { return _isLayoutAvgAlign; }
  37. set
  38. {
  39. SetProperty(ref _isLayoutAvgAlign, value);
  40. }
  41. }
  42. private bool _isLayoutAlign = false;
  43. public bool IsLayoutAlign
  44. {
  45. get { return _isLayoutAlign; }
  46. set
  47. {
  48. SetProperty(ref _isLayoutAlign, value);
  49. }
  50. }
  51. private bool _isTextEdit = true;
  52. public bool IsTextEdit
  53. {
  54. get { return _isTextEdit; }
  55. set
  56. {
  57. SetProperty(ref _isTextEdit, value);
  58. }
  59. }
  60. #endregion
  61. #region 文本属性
  62. private double _angle;
  63. public double Angle
  64. {
  65. get { return _angle; }
  66. set
  67. {
  68. SetProperty(ref _angle, value);
  69. }
  70. }
  71. private Brush selectColor = new SolidColorBrush(Colors.Black);
  72. public Brush SelectColor
  73. {
  74. get { return selectColor; }
  75. set
  76. {
  77. SetProperty(ref selectColor, value);
  78. if (TextEditEvent != null)
  79. {
  80. bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A ||
  81. TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B ||
  82. TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G ||
  83. TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R;
  84. if (isok)
  85. {
  86. TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color;
  87. TextEditEvent.UpdatePDFEditByEventArgs();
  88. }
  89. }
  90. }
  91. }
  92. private FontFamily fontFamily = new FontFamily("Courier");
  93. public FontFamily TextFontFamily
  94. {
  95. get { return fontFamily; }
  96. set
  97. {
  98. SetProperty(ref fontFamily, value);
  99. if (TextEditEvent != null)
  100. {
  101. TextEditEvent.FontFamily = fontFamily;
  102. TextEditEvent.UpdatePDFEditByEventArgs();
  103. }
  104. }
  105. }
  106. private FontWeight fontWeights = FontWeights.Normal;
  107. public FontWeight TextFontWeights
  108. {
  109. get { return fontWeights; }
  110. set
  111. {
  112. SetProperty(ref fontWeights, value);
  113. if (TextEditEvent != null)
  114. {
  115. TextEditEvent.FontWeight = fontWeights;
  116. TextEditEvent.UpdatePDFEditByEventArgs();
  117. }
  118. }
  119. }
  120. private FontStyle fontStyle = FontStyles.Normal;
  121. public FontStyle TextFontStyle
  122. {
  123. get { return fontStyle; }
  124. set
  125. {
  126. SetProperty(ref fontStyle, value);
  127. if (TextEditEvent != null)
  128. {
  129. TextEditEvent.FontStyle = fontStyle;
  130. TextEditEvent.UpdatePDFEditByEventArgs();
  131. }
  132. }
  133. }
  134. private int fontSize = 24;
  135. public int TextFontSize
  136. {
  137. get { return fontSize; }
  138. set
  139. {
  140. SetProperty(ref fontSize, value);
  141. if (TextEditEvent != null)
  142. {
  143. TextEditEvent.FontSize = fontSize;
  144. TextEditEvent.UpdatePDFEditByEventArgs();
  145. }
  146. }
  147. }
  148. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  149. public List<FontStyleItem> FontStyleList
  150. {
  151. get { return fontStyleList; }
  152. set
  153. {
  154. SetProperty(ref fontStyleList, value);
  155. }
  156. }
  157. #endregion
  158. #region 图像属性
  159. private bool _isCrop = false;
  160. public bool IsCrop
  161. {
  162. get { return _isCrop; }
  163. set
  164. {
  165. SetProperty(ref _isCrop, value);
  166. }
  167. }
  168. private System.Windows.Media.Imaging.BitmapSource _currentImg;
  169. public System.Windows.Media.Imaging.BitmapSource CurrentImg
  170. {
  171. get { return _currentImg; }
  172. set
  173. {
  174. SetProperty(ref _currentImg, value);
  175. }
  176. }
  177. #endregion
  178. #endregion
  179. #region Command
  180. #region 文本Command
  181. public DelegateCommand AddTextCommand { get; set; }
  182. public DelegateCommand AddImgCommand { get; set; }
  183. public DelegateCommand<object> SelectedColorCommand { get; set; }
  184. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  185. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  186. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  187. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  188. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  189. public DelegateCommand EditTextModeCommand { get; set; }
  190. #endregion
  191. #region 图像Command
  192. public DelegateCommand ReplaceImgCommand { get; set; }
  193. public DelegateCommand ExportImgCommand { get; set; }
  194. public DelegateCommand CropImgCommand { get; set; }
  195. public DelegateCommand<object> ImgAlignCheckedCommand { get; set; }
  196. /// <summary>
  197. /// 逆时针旋转
  198. /// </summary>
  199. public DelegateCommand AntiClockwiseCommand { get; set; }
  200. /// <summary>
  201. /// 顺时针旋转
  202. /// </summary>
  203. public DelegateCommand ClockwiseCommand { get; set; }
  204. public DelegateCommand CropModeCommand { get; set; }
  205. public DelegateCommand CancelCropCommand { get; set; }
  206. #endregion
  207. public event EventHandler ClearCheckedAglin;
  208. #endregion
  209. private ComPDFKitViewer.PDFEditEvent TextEditEvent;
  210. public TextEditPropertyViewModel()
  211. {
  212. InitVariable();
  213. InitCommand();
  214. }
  215. private void InitVariable()
  216. {
  217. InitFontStyles();
  218. }
  219. private void InitFontStyles()
  220. {
  221. FontStyleList = LoadFontStyle.Load();
  222. }
  223. private void InitCommand()
  224. {
  225. AddTextCommand = new DelegateCommand(AddText);
  226. AddImgCommand = new DelegateCommand(AddImg);
  227. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  228. ExportImgCommand = new DelegateCommand(ExportImg);
  229. CropImgCommand = new DelegateCommand(CropImg);
  230. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  231. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  232. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  233. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
  234. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
  235. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  236. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  237. EditTextModeCommand = new DelegateCommand(EditTextMode);
  238. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  239. ClockwiseCommand = new DelegateCommand(Clockwise);
  240. CropModeCommand = new DelegateCommand(CropMode);
  241. CancelCropCommand = new DelegateCommand(CancelCropImg);
  242. }
  243. private void ImgAlignChecked(object obj)
  244. {
  245. if (obj != null)
  246. {
  247. switch((string)obj)
  248. {
  249. case "AlignLeft":
  250. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  251. break;
  252. case "AlignHorizonCenter":
  253. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  254. break;
  255. case "AlignRight":
  256. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  257. break;
  258. case "DistributeHorizontal":
  259. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  260. break;
  261. case "AlignTop":
  262. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  263. break;
  264. case "AlignVerticalCenter":
  265. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  266. break;
  267. case "AlignBottom":
  268. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  269. break;
  270. case "DistributeVertical":
  271. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  272. break;
  273. }
  274. }
  275. }
  276. private void CancelCropImg()
  277. {
  278. if (TextEditEvent != null)
  279. {
  280. TextEditEvent.ClipImage = false;
  281. TextEditEvent.CancelClip();
  282. TextEditEvent.UpdatePDFEditByEventArgs();
  283. IsCrop = false;
  284. }
  285. }
  286. private void CropImg()
  287. {
  288. if(TextEditEvent != null)
  289. {
  290. TextEditEvent.SaveClip();
  291. TextEditEvent.ClipImage = false;
  292. TextEditEvent.UpdatePDFEditByEventArgs();
  293. IsCrop = false;
  294. }
  295. }
  296. private void ExportImg()
  297. {
  298. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  299. //SaveFileDialog saveFileDialog = new SaveFileDialog();
  300. //saveFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  301. //saveFileDialog.FileName = "编辑PDF导出图片";
  302. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  303. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  304. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  305. {
  306. if (string.IsNullOrEmpty(folder.SelectedPath))
  307. return;
  308. var keyValueList = PDFViewer.GetSelectedImages();
  309. int i = 0;
  310. foreach (var bitmap in keyValueList)
  311. {
  312. foreach(var bitmapItem in bitmap.Value)
  313. {
  314. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  315. i++;
  316. }
  317. }
  318. }
  319. }
  320. private void AddImg()
  321. {
  322. OpenFileDialog openFileDialog = new OpenFileDialog();
  323. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  324. openFileDialog.Multiselect = true;
  325. if ((bool)openFileDialog.ShowDialog())
  326. {
  327. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  328. {
  329. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  330. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  331. }
  332. }
  333. }
  334. private void ReplaceImg()
  335. {
  336. OpenFileDialog openFileDialog = new OpenFileDialog();
  337. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  338. openFileDialog.Multiselect = true;
  339. if ((bool)openFileDialog.ShowDialog())
  340. {
  341. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  342. {
  343. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  344. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  345. TextEditEvent.UpdatePDFEditByEventArgs();
  346. }
  347. }
  348. }
  349. private void AddText()
  350. {
  351. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  352. }
  353. private void TextAlignChecked(object obj)
  354. {
  355. if((string)obj != null && TextEditEvent != null)
  356. {
  357. switch((string)obj)
  358. {
  359. case "AlignLeft":
  360. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  361. break;
  362. case "AlignCenter":
  363. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  364. break;
  365. case "AlignRight":
  366. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  367. break;
  368. case "Align":
  369. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  370. break;
  371. }
  372. TextEditEvent.UpdatePDFEditByEventArgs();
  373. }
  374. }
  375. private void Clockwise()
  376. {
  377. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate + 90;
  378. TextEditEvent.UpdatePDFEditByEventArgs();
  379. }
  380. private void AntiClockwise()
  381. {
  382. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate - 90;
  383. TextEditEvent.UpdatePDFEditByEventArgs();
  384. }
  385. private void CropMode()
  386. {
  387. IsCrop = true;
  388. if(TextEditEvent != null)
  389. {
  390. TextEditEvent.ClipImage = true;
  391. TextEditEvent.UpdatePDFEditByEventArgs();
  392. }
  393. }
  394. private void SelectedColor(object obj)
  395. {
  396. if (obj != null)
  397. {
  398. var colorValue = (Color)obj;
  399. if (colorValue != null)
  400. {
  401. SelectColor = new SolidColorBrush(colorValue);
  402. }
  403. }
  404. }
  405. private void SelectedFontStyle(object obj)
  406. {
  407. if (obj != null && (FontStyleItem)obj != null)
  408. {
  409. var item = (FontStyleItem)obj;
  410. }
  411. }
  412. private void FontFamilyChanged(object obj)
  413. {
  414. if (obj != null)
  415. {
  416. if ((int)obj > -1)
  417. {
  418. if ((int)obj == 0)
  419. {
  420. TextFontFamily = new FontFamily("Courier");
  421. }
  422. if ((int)obj == 1)
  423. {
  424. TextFontFamily = new FontFamily("Helvetica");
  425. }
  426. if ((int)obj == 2)
  427. {
  428. TextFontFamily = new FontFamily("Times");
  429. }
  430. }
  431. }
  432. }
  433. private void FontStyleChanged(object obj)
  434. {
  435. if (obj != null)
  436. {
  437. var item = (ComboBoxItem)obj;
  438. var content = (string)item.Content;
  439. if (content != null)
  440. {
  441. if (content == "Regular")
  442. {
  443. TextFontWeights = FontWeights.Normal;
  444. TextFontStyle = FontStyles.Normal;
  445. }
  446. if (content == "Bold")
  447. {
  448. TextFontWeights = FontWeights.Bold;
  449. TextFontStyle = FontStyles.Normal;
  450. }
  451. if (content == "Italic")
  452. {
  453. TextFontWeights = FontWeights.Normal;
  454. TextFontStyle = FontStyles.Italic;
  455. }
  456. if (content == "Bold Italic")
  457. {
  458. TextFontWeights = FontWeights.Bold;
  459. TextFontStyle = FontStyles.Italic;
  460. }
  461. }
  462. }
  463. }
  464. private void FontSizeChanged(object obj)
  465. {
  466. if (obj != null)
  467. {
  468. var item = (ComboBoxItem)obj;
  469. var content = (string)item.Content;
  470. if (content != null)
  471. {
  472. var intData = int.Parse(content);
  473. TextFontSize = intData;
  474. }
  475. }
  476. }
  477. private CPDFViewer PDFViewer;
  478. public void OnNavigatedTo(NavigationContext navigationContext)
  479. {
  480. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  481. if (PDFViewer != null)
  482. {
  483. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  484. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  485. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  486. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  487. }
  488. }
  489. public bool IsNavigationTarget(NavigationContext navigationContext)
  490. {
  491. return true;
  492. }
  493. public void OnNavigatedFrom(NavigationContext navigationContext)
  494. {
  495. }
  496. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  497. {
  498. if (e == null)
  499. return;
  500. switch (e.CommandType)
  501. {
  502. case CommandType.Context:
  503. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  504. {
  505. e.PopupMenu = EmptyStateMenu(sender);
  506. }
  507. else if(e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  508. {
  509. e.PopupMenu = SelectPDFEdit(sender);
  510. }
  511. else if(e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  512. {
  513. //文字编辑
  514. if(e.PressOnBorder == true)
  515. {
  516. e.PopupMenu = SelectTextBorder(sender);
  517. }
  518. if(e.PressOnSelectedText == true)
  519. {
  520. e.PopupMenu = SelectTextContent(sender);
  521. }
  522. }
  523. break;
  524. default:
  525. e.DoCommand();
  526. break;
  527. }
  528. if (e.PopupMenu != null)
  529. {
  530. e.Handle = true;
  531. }
  532. }
  533. private void EditTextMode()
  534. {
  535. }
  536. private ContextMenu EmptyStateMenu(object sender)
  537. {
  538. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  539. if (popMenu != null && popMenu.Items.Count == 3)
  540. {
  541. //复制
  542. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  543. menuItem.CommandTarget = (UIElement)sender;
  544. menuItem.Command = ApplicationCommands.Paste;
  545. //添加文本
  546. menuItem = popMenu.Items[1] as MenuItem;
  547. menuItem.CommandTarget = (UIElement)sender;
  548. menuItem.Command = AddTextCommand;
  549. //添加图像
  550. menuItem = popMenu.Items[2] as MenuItem;
  551. menuItem.CommandTarget = (UIElement)sender;
  552. menuItem.Command = AddImgCommand;
  553. }
  554. return popMenu;
  555. }
  556. private ContextMenu SelectPDFEdit(object sender)
  557. {
  558. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  559. if (popMenu != null && popMenu.Items.Count == 7)
  560. {
  561. //复制
  562. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  563. menuItem.CommandTarget = (UIElement)sender;
  564. menuItem.Command = ApplicationCommands.Copy;
  565. //剪切
  566. menuItem = popMenu.Items[1] as MenuItem;
  567. menuItem.CommandTarget = (UIElement)sender;
  568. menuItem.Command = ApplicationCommands.Cut;
  569. //粘贴
  570. menuItem = popMenu.Items[2] as MenuItem;
  571. menuItem.CommandTarget = (UIElement)sender;
  572. menuItem.Command = ApplicationCommands.Paste;
  573. //删除
  574. menuItem = popMenu.Items[3] as MenuItem;
  575. menuItem.CommandTarget = (UIElement)sender;
  576. menuItem.Command = ApplicationCommands.Delete;
  577. //裁剪
  578. menuItem = popMenu.Items[4] as MenuItem;
  579. menuItem.CommandTarget = (UIElement)sender;
  580. menuItem.Command = CropModeCommand;
  581. //替换
  582. menuItem = popMenu.Items[5] as MenuItem;
  583. menuItem.CommandTarget = (UIElement)sender;
  584. menuItem.Command = ReplaceImgCommand;
  585. //导出
  586. menuItem = popMenu.Items[6] as MenuItem;
  587. menuItem.CommandTarget = (UIElement)sender;
  588. menuItem.Command = ExportImgCommand;
  589. }
  590. return popMenu;
  591. }
  592. private ContextMenu SelectTextBorder(object sender)
  593. {
  594. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  595. if (popMenu != null && popMenu.Items.Count == 5)
  596. {
  597. //编辑
  598. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  599. menuItem.CommandTarget = (UIElement)sender;
  600. menuItem.Command = EditTextModeCommand;
  601. //复制
  602. menuItem = popMenu.Items[1] as MenuItem;
  603. menuItem.CommandTarget = (UIElement)sender;
  604. menuItem.Command = ApplicationCommands.Copy;
  605. //剪切
  606. menuItem = popMenu.Items[2] as MenuItem;
  607. menuItem.CommandTarget = (UIElement)sender;
  608. menuItem.Command = ApplicationCommands.Cut;
  609. //粘贴
  610. menuItem = popMenu.Items[3] as MenuItem;
  611. menuItem.CommandTarget = (UIElement)sender;
  612. menuItem.Command = ApplicationCommands.Paste;
  613. //删除
  614. menuItem = popMenu.Items[4] as MenuItem;
  615. menuItem.CommandTarget = (UIElement)sender;
  616. menuItem.Command = ApplicationCommands.Delete;
  617. }
  618. return popMenu;
  619. }
  620. private ContextMenu SelectTextContent(object sender)
  621. {
  622. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  623. if (popMenu != null && popMenu.Items.Count == 6)
  624. {
  625. //复制
  626. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  627. menuItem.CommandTarget = (UIElement)sender;
  628. menuItem.Command = ApplicationCommands.Copy;
  629. //剪切
  630. menuItem = popMenu.Items[1] as MenuItem;
  631. menuItem.CommandTarget = (UIElement)sender;
  632. menuItem.Command = ApplicationCommands.Cut;
  633. //粘贴
  634. menuItem = popMenu.Items[2] as MenuItem;
  635. menuItem.CommandTarget = (UIElement)sender;
  636. menuItem.Command = ApplicationCommands.Paste;
  637. //粘贴并匹配样式
  638. menuItem = popMenu.Items[3] as MenuItem;
  639. menuItem.CommandTarget = (UIElement)sender;
  640. menuItem.Command = ApplicationCommands.Paste;
  641. //删除
  642. menuItem = popMenu.Items[4] as MenuItem;
  643. menuItem.CommandTarget = (UIElement)sender;
  644. menuItem.Command = ApplicationCommands.Delete;
  645. //全部选定
  646. menuItem = popMenu.Items[4] as MenuItem;
  647. menuItem.CommandTarget = (UIElement)sender;
  648. menuItem.Command = ApplicationCommands.SelectAll;
  649. }
  650. return popMenu;
  651. }
  652. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  653. {
  654. if(e != null && e.Count > 0)
  655. {
  656. ReLoadLayoutAlign(e.Count);
  657. IsTextEdit = (e[0].EditType == ComPDFKit.PDFPage.CPDFEditType.EditText);
  658. TextEditEvent = e[0];
  659. if (IsTextEdit == false)
  660. {
  661. var list = PDFViewer.GetSelectedImages();
  662. if (list != null && list.Count > 0)
  663. {
  664. System.Drawing.Bitmap bitmap = null;
  665. foreach (var item in list)
  666. {
  667. if (item.Value.Count > 0)
  668. {
  669. bitmap = item.Value[0];
  670. break;
  671. }
  672. }
  673. if (bitmap != null)
  674. {
  675. IntPtr ip = bitmap.GetHbitmap();
  676. System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
  677. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  678. CurrentImg = bitmapSource;
  679. }
  680. }
  681. }
  682. bool isText = false;
  683. bool isImg = false;
  684. foreach (var item in e)
  685. {
  686. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  687. {
  688. isText = true;
  689. }
  690. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  691. {
  692. isImg = true;
  693. }
  694. }
  695. if (isImg == true && isText == true)
  696. IsSelectTextAndImg = true;
  697. else
  698. IsSelectTextAndImg = false;
  699. }
  700. else
  701. {
  702. IsLayoutAlign = false;
  703. IsLayoutAvgAlign = false;
  704. IsSelectTextAndImg = false;
  705. IsTextEdit = true;
  706. ClearCheckedAglin?.Invoke(null, null);
  707. }
  708. }
  709. private void ReLoadLayoutAlign(int count)
  710. {
  711. if (count >= 2)
  712. IsLayoutAlign = true;
  713. else
  714. IsLayoutAlign = false;
  715. if (count >= 3)
  716. IsLayoutAvgAlign = true;
  717. else
  718. IsLayoutAvgAlign = false;
  719. }
  720. }
  721. }