TextEditPropertyViewModel.cs 29 KB

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