TextEditPropertyViewModel.cs 26 KB

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