TextEditPropertyViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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)
  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)
  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)
  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 = new List<ComboDataItem>();
  275. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  276. FontFamilyItems.Add(item);
  277. item = new ComboDataItem("Helvetica", "Helvetica");
  278. FontFamilyItems.Add(item);
  279. item = new ComboDataItem("Times-Roman", "Times New Roman");
  280. FontFamilyItems.Add(item);
  281. }
  282. private void InitFontStyleComboBox()
  283. {
  284. FontStyleItems = new List<ComboDataItem>();
  285. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  286. FontStyleItems.Add(item);
  287. item = new ComboDataItem("Bold", "Bold");
  288. FontStyleItems.Add(item);
  289. item = new ComboDataItem("Italic", "Italic");
  290. FontStyleItems.Add(item);
  291. item = new ComboDataItem("Bold Italic", "Bold Italic");
  292. FontStyleItems.Add(item);
  293. }
  294. private void InitFontStyles()
  295. {
  296. PresetTextItems = new List<ComboDataItem>();
  297. FontStyleList = LoadFontStyle.Load();
  298. foreach(var item in FontStyleList)
  299. {
  300. ComboDataItem itemData = new ComboDataItem(item.mTag, item.mTagContent);
  301. PresetTextItems.Add(itemData);
  302. }
  303. }
  304. private void InitCommand()
  305. {
  306. AddTextCommand = new DelegateCommand(AddText);
  307. AddImgCommand = new DelegateCommand(AddImg);
  308. ReplaceImgCommand = new DelegateCommand(ReplaceImg);
  309. ExportImgCommand = new DelegateCommand(ExportImg);
  310. CropImgCommand = new DelegateCommand(CropImg);
  311. ImgAlignCheckedCommand = new DelegateCommand<object>(ImgAlignChecked);
  312. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  313. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  314. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
  315. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
  316. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  317. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  318. EditTextModeCommand = new DelegateCommand(EditTextMode);
  319. AntiClockwiseCommand = new DelegateCommand(AntiClockwise);
  320. ClockwiseCommand = new DelegateCommand(Clockwise);
  321. CropModeCommand = new DelegateCommand(CropMode);
  322. CancelCropCommand = new DelegateCommand(CancelCropImg);
  323. }
  324. #endregion
  325. #region 文本处理
  326. private void AddText()
  327. {
  328. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  329. }
  330. private void EditTextMode()
  331. {
  332. }
  333. private void TextAlignChecked(object obj)
  334. {
  335. if ((string)obj != null && TextEditEvent != null)
  336. {
  337. switch ((string)obj)
  338. {
  339. case "AlignLeft":
  340. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  341. break;
  342. case "AlignCenter":
  343. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  344. break;
  345. case "AlignRight":
  346. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  347. break;
  348. case "Align":
  349. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  350. break;
  351. }
  352. TextEditEvent.UpdatePDFEditByEventArgs();
  353. }
  354. }
  355. private void Clockwise()
  356. {
  357. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate + 90;
  358. TextEditEvent.UpdatePDFEditByEventArgs();
  359. }
  360. private void AntiClockwise()
  361. {
  362. if ( TextEditEvent != null)
  363. {
  364. Angle = TextEditEvent.Rotate = TextEditEvent.Rotate - 90;
  365. TextEditEvent.UpdatePDFEditByEventArgs();
  366. }
  367. }
  368. private void SelectedColor(object obj)
  369. {
  370. if (obj != null)
  371. {
  372. var colorValue = (Color)obj;
  373. if (colorValue != null)
  374. {
  375. SelectColor = new SolidColorBrush(colorValue);
  376. }
  377. }
  378. }
  379. private void SelectedFontStyle(object obj)
  380. {
  381. if (obj != null && (FontStyleItem)obj != null)
  382. {
  383. var item = (FontStyleItem)obj;
  384. }
  385. }
  386. private void FontFamilyChanged(object obj)
  387. {
  388. if (obj != null && (int)obj > -1)
  389. {
  390. switch ((int)obj)
  391. {
  392. case 0: TextFontFamily = new FontFamily("Courier"); break;
  393. case 1: TextFontFamily = new FontFamily("Helvetica"); break;
  394. case 2: TextFontFamily = new FontFamily("Times"); break;
  395. }
  396. }
  397. }
  398. private void FontStyleChanged(object obj)
  399. {
  400. if (obj != null && (ComboBoxItem)obj != null)
  401. {
  402. var item = (ComboBoxItem)obj;
  403. var content = (string)item.Content;
  404. if (content != null)
  405. {
  406. switch (content)
  407. {
  408. case "Regular":
  409. TextFontWeights = FontWeights.Normal;
  410. TextFontStyle = FontStyles.Normal;
  411. break;
  412. case "Bold":
  413. TextFontWeights = FontWeights.Bold;
  414. TextFontStyle = FontStyles.Normal;
  415. break;
  416. case "Italic":
  417. TextFontWeights = FontWeights.Normal;
  418. TextFontStyle = FontStyles.Italic;
  419. break;
  420. case "Bold Italic":
  421. TextFontWeights = FontWeights.Bold;
  422. TextFontStyle = FontStyles.Italic;
  423. break;
  424. }
  425. }
  426. }
  427. }
  428. private void FontSizeChanged(object obj)
  429. {
  430. if (obj != null)
  431. {
  432. var item = (ComboBoxItem)obj;
  433. var content = (string)item.Content;
  434. if (content != null)
  435. {
  436. var intData = int.Parse(content);
  437. TextFontSize = intData;
  438. }
  439. }
  440. }
  441. #endregion
  442. #region 图像处理
  443. private void CancelCropImg()
  444. {
  445. if (TextEditEvent != null)
  446. {
  447. TextEditEvent.ClipImage = false;
  448. TextEditEvent.CancelClip();
  449. TextEditEvent.UpdatePDFEditByEventArgs();
  450. IsCrop = false;
  451. }
  452. }
  453. private void CropImg()
  454. {
  455. if (TextEditEvent != null)
  456. {
  457. TextEditEvent.SaveClip();
  458. TextEditEvent.ClipImage = false;
  459. TextEditEvent.UpdatePDFEditByEventArgs();
  460. IsCrop = false;
  461. }
  462. }
  463. private void ExportImg()
  464. {
  465. if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
  466. System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
  467. folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
  468. if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  469. {
  470. if (string.IsNullOrEmpty(folder.SelectedPath))
  471. return;
  472. var keyValueList = PDFViewer.GetSelectedImages();
  473. int i = 0;
  474. foreach (var bitmap in keyValueList)
  475. {
  476. foreach (var bitmapItem in bitmap.Value)
  477. {
  478. bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
  479. i++;
  480. }
  481. }
  482. }
  483. }
  484. private void AddImg()
  485. {
  486. OpenFileDialog openFileDialog = new OpenFileDialog();
  487. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  488. openFileDialog.Multiselect = true;
  489. if ((bool)openFileDialog.ShowDialog())
  490. {
  491. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  492. {
  493. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  494. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  495. }
  496. }
  497. }
  498. private void ReplaceImg()
  499. {
  500. if (TextEditEvent == null) return;
  501. OpenFileDialog openFileDialog = new OpenFileDialog();
  502. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  503. openFileDialog.Multiselect = true;
  504. if ((bool)openFileDialog.ShowDialog())
  505. {
  506. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  507. {
  508. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  509. TextEditEvent.ReplaceImagePath = openFileDialog.FileName;
  510. TextEditEvent.UpdatePDFEditByEventArgs();
  511. }
  512. }
  513. }
  514. private void CropMode()
  515. {
  516. IsCrop = true;
  517. if (TextEditEvent != null)
  518. {
  519. TextEditEvent.ClipImage = true;
  520. TextEditEvent.UpdatePDFEditByEventArgs();
  521. }
  522. }
  523. #endregion
  524. #region 布局处理
  525. private void ImgAlignChecked(object obj)
  526. {
  527. if (obj != null)
  528. {
  529. switch ((string)obj)
  530. {
  531. case "AlignLeft":
  532. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  533. break;
  534. case "AlignHorizonCenter":
  535. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  536. break;
  537. case "AlignRight":
  538. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  539. break;
  540. case "DistributeHorizontal":
  541. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  542. break;
  543. case "AlignTop":
  544. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  545. break;
  546. case "AlignVerticalCenter":
  547. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  548. break;
  549. case "AlignBottom":
  550. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  551. break;
  552. case "DistributeVertical":
  553. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  554. break;
  555. }
  556. }
  557. }
  558. private void ReLoadLayoutAlign(int count)
  559. {
  560. if (count >= 2)
  561. IsLayoutAlign = true;
  562. else
  563. IsLayoutAlign = false;
  564. if (count >= 3)
  565. IsLayoutAvgAlign = true;
  566. else
  567. IsLayoutAvgAlign = false;
  568. }
  569. #endregion
  570. #region 右键菜单
  571. //点击空白处时
  572. private ContextMenu EmptyStateMenu(object sender)
  573. {
  574. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  575. if (popMenu != null && popMenu.Items.Count == 3)
  576. {
  577. //粘贴
  578. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Paste);
  579. //添加文本
  580. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, AddTextCommand);
  581. //添加图像
  582. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, AddImgCommand);
  583. }
  584. return popMenu;
  585. }
  586. //选中图像时
  587. private ContextMenu SelectImgPDFEdit(object sender)
  588. {
  589. var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
  590. if (popMenu != null && popMenu.Items.Count == 7)
  591. {
  592. //复制
  593. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy);
  594. //剪切
  595. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut);
  596. //粘贴
  597. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste);
  598. //删除
  599. SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Delete);
  600. //裁剪
  601. SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, CropModeCommand);
  602. //替换
  603. SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ReplaceImgCommand);
  604. //导出
  605. SetPopMenuItem(popMenu.Items[6] as MenuItem, sender, ExportImgCommand);
  606. }
  607. return popMenu;
  608. }
  609. //选中文字的框
  610. private ContextMenu SelectTextBorder(object sender)
  611. {
  612. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  613. if (popMenu != null && popMenu.Items.Count == 5)
  614. {
  615. //编辑
  616. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, EditTextModeCommand);
  617. //复制
  618. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Copy);
  619. //剪切
  620. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Cut);
  621. //粘贴
  622. SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Paste);
  623. //删除
  624. SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete);
  625. }
  626. return popMenu;
  627. }
  628. //选中文字内容
  629. private ContextMenu SelectTextContent(object sender)
  630. {
  631. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  632. if (popMenu != null && popMenu.Items.Count == 6)
  633. {
  634. //复制
  635. SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy);
  636. //剪切
  637. SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut);
  638. //粘贴
  639. SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste);
  640. //粘贴并匹配样式
  641. SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Paste);
  642. //删除
  643. SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete);
  644. ////全部选定
  645. SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ApplicationCommands.SelectAll);
  646. }
  647. return popMenu;
  648. }
  649. #endregion
  650. #region 编辑PDF内容触发的事件
  651. /// <summary>
  652. /// 右键菜单的事件
  653. /// </summary>
  654. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  655. {
  656. if (e == null)
  657. return;
  658. switch (e.CommandType)
  659. {
  660. case CommandType.Context:
  661. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  662. {
  663. e.PopupMenu = EmptyStateMenu(sender);
  664. }
  665. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  666. {
  667. e.PopupMenu = SelectImgPDFEdit(sender);
  668. }
  669. else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  670. {
  671. //文字编辑
  672. if (e.PressOnBorder == true)
  673. {
  674. e.PopupMenu = SelectTextBorder(sender);
  675. }
  676. if (e.PressOnSelectedText == true)
  677. {
  678. e.PopupMenu = SelectTextContent(sender);
  679. }
  680. }
  681. break;
  682. default:
  683. e.DoCommand();
  684. break;
  685. }
  686. if (e.PopupMenu != null)
  687. {
  688. e.Handle = true;
  689. }
  690. }
  691. #region 全局
  692. public event EventHandler ClearCheckedAglin;
  693. #endregion
  694. #endregion
  695. protected List<PDFEditEvent> TextEditEventList;
  696. public void OnNavigatedTo(NavigationContext navigationContext)
  697. {
  698. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  699. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  700. if (PDFViewer != null)
  701. {
  702. if (TextEditEventList != null && TextEditEventList.Count > 0)
  703. {
  704. TextEditEvent = TextEditEventList[0];
  705. GetPDFEdit();
  706. if (TextEditEventList.Count == 2)
  707. {
  708. IsLayoutAlign = true;
  709. IsLayoutAvgAlign = false;
  710. }
  711. else if(TextEditEventList.Count >2)
  712. {
  713. IsLayoutAlign = true;
  714. IsLayoutAvgAlign = true;
  715. }
  716. else
  717. {
  718. IsLayoutAlign = false;
  719. IsLayoutAvgAlign = false;
  720. }
  721. }
  722. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  723. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  724. }
  725. }
  726. private void GetPDFEdit()
  727. {
  728. if(TextEditEvent != null)
  729. {
  730. TextFontFamily = TextEditEvent.FontFamily;
  731. //TextFontSize = (int)TextEditEvent.FontSize;
  732. TextFontStyle = TextEditEvent.FontStyle;
  733. TextFontWeights = TextEditEvent.FontWeight;
  734. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  735. FontSizeData = new ComboDataItem(TextEditEvent.FontSize);
  736. }
  737. }
  738. public bool IsNavigationTarget(NavigationContext navigationContext){ return true; }
  739. public void OnNavigatedFrom(NavigationContext navigationContext)
  740. {
  741. TextEditEvent = null;
  742. ClearCheckedAglin?.Invoke(null, null);
  743. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  744. }
  745. }
  746. }