TextEditPropertyViewModel.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Master.CustomControl.CompositeControl;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.AnnotPanel;
  9. using PDF_Master.Model.PropertyPanel.AnnotPanel;
  10. using PDF_Master.ViewModels.Tools.AnnotManager;
  11. using PDFSettings;
  12. using Prism.Commands;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
  25. {
  26. public class TextEditPropertyViewModel : PDFEditVM, INavigationAware
  27. {
  28. #region 属性
  29. public TextEditPropertyViewModel()
  30. {
  31. InitVariable();
  32. InitCommand();
  33. }
  34. private void InitVariable()
  35. {
  36. InitBaseVariable();
  37. }
  38. private bool isSelectedEmpty;
  39. public bool IsSelectedEmpty
  40. {
  41. get { return isSelectedEmpty; }
  42. set { SetProperty(ref isSelectedEmpty, value); }
  43. }
  44. private string title;
  45. public string Title
  46. {
  47. get { return title; }
  48. set
  49. {
  50. SetProperty(ref title, value);
  51. }
  52. }
  53. private FontBoardVm _fontVm = new FontBoardVm(true);
  54. public FontBoardVm FontVm
  55. {
  56. get { return _fontVm; }
  57. set { SetProperty(ref _fontVm, value); }
  58. }
  59. private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign;
  60. public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign
  61. {
  62. get { return _textAlign; }
  63. set { _textAlign = value; }
  64. }
  65. #endregion
  66. #region Command
  67. public DelegateCommand AddTextCommand { get; set; }
  68. public DelegateCommand AddImgCommand { get; set; }
  69. public DelegateCommand<object> SelectedColorCommand { get; set; }
  70. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  71. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  72. public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
  73. //进入属性面板编辑
  74. public DelegateCommand EditTextModeCommand { get; set; }
  75. public DelegateCommand FontFamilyChangedCommand { get; set; }
  76. public DelegateCommand CustomFontStyleCommand { get; set; }
  77. public DelegateCommand FontStyleWeightChangedCommand { get; set; }
  78. public DelegateCommand SelectedPresetFontCommand { get; set; }
  79. //重定义
  80. public DelegateCommand ReDefineFontStyleCommand { get; set; }
  81. public DelegateCommand RestoreDefaultStyleCommand { get; set; }
  82. public DelegateCommand<object> LayoutAlignCheckedCommand { get; set; }
  83. #endregion
  84. #region Command初始化
  85. private void InitCommand()
  86. {
  87. //预设文本样式
  88. SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
  89. //字体
  90. FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
  91. //文本字重、字体
  92. FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
  93. //添加文本
  94. AddTextCommand = new DelegateCommand(AddText);
  95. //添加图片
  96. AddImgCommand = new DelegateCommand(AddImg);
  97. //选择颜色
  98. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  99. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  100. //大小
  101. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  102. //内容对齐
  103. TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
  104. //编辑模式
  105. EditTextModeCommand = new DelegateCommand(EditTextMode);
  106. //自定义
  107. CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
  108. //重新定义
  109. ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
  110. //恢复默认
  111. RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
  112. LayoutAlignCheckedCommand = new DelegateCommand<object>(LayoutAlignChecked);
  113. }
  114. #endregion
  115. #region 文本处理
  116. private void AddText()
  117. {
  118. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  119. }
  120. private void AddImg()
  121. {
  122. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  123. }
  124. private void EditTextMode()
  125. {
  126. }
  127. /// <summary>
  128. /// 从预设样式设置文本
  129. /// </summary>
  130. private void SelectedPresetFont()
  131. {
  132. if (CurrentPresetFont != null)
  133. {
  134. var itemComboxItem = CurrentPresetFont;
  135. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  136. if (item != null)
  137. {
  138. if (item.mTag != "Custom")
  139. {
  140. GetCurrentFontSize(item.mFontSize);
  141. if (item.mFontFamily != null)
  142. {
  143. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  144. }
  145. GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
  146. CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  147. CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  148. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  149. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 自定义文字样式
  156. /// </summary>
  157. private void CustomFontStyle()
  158. {
  159. if (CurrentPresetFont != null)
  160. {
  161. ContextMenu menu;
  162. if (CurrentPresetFont.ValueStr == "Custom")
  163. {
  164. menu = SelectAnnotContextMenu(false);
  165. }
  166. else
  167. {
  168. menu = SelectAnnotContextMenu(false);
  169. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  170. if (currentItem.mTag != "Custom")
  171. {
  172. if (FontWeightItem != currentItem.mFontWeight ||
  173. FontStyleItem != currentItem.mFontStyle ||
  174. CurrentFontSize.Value != currentItem.mFontSize ||
  175. CurrentFontFamily.ValueStr != currentItem.mFontFamily.Source)
  176. {
  177. menu = SelectAnnotContextMenu(true);
  178. }
  179. }
  180. }
  181. if (menu != null)
  182. {
  183. menu.IsOpen = true;
  184. }
  185. }
  186. }
  187. //设置字体样式
  188. private void FontFamilyChanged()
  189. {
  190. if (string.IsNullOrEmpty(CurrentFontFamily.ValueStr) == false)
  191. {
  192. //GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  193. }
  194. if (CurrentPresetFont != null)
  195. {
  196. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  197. if (currentItem.mTag != "Custom")
  198. {
  199. foreach (var item in PresetFontList)
  200. {
  201. if (item.mTag == currentItem.mTag)
  202. {
  203. if (CurrentFontFamily.ValueStr != item.mFontFamily.Source)
  204. {
  205. string txt = string.Format($" * {currentItem.mTagContent}");
  206. if (Title == txt)
  207. {
  208. Title = "";
  209. }
  210. Title = txt;
  211. break;
  212. }
  213. else
  214. {
  215. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  216. {
  217. string txt = currentItem.mTagContent;
  218. if (Title == txt)
  219. {
  220. Title = "";
  221. }
  222. Title = txt;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. else
  229. {
  230. //bool isExist = GetCurrentPresetFont(Annot);
  231. //if (isExist)
  232. //{
  233. //}
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// 设置文本字重、样式
  239. /// </summary>
  240. private void FontStyleWeightChanged()
  241. {
  242. if (CurrrentFontWeightStyle != null)
  243. {
  244. UpdateFontWeight_Style();
  245. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  246. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  247. //Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
  248. //AnnotAttribDir.Add(AnnotAttrib.IsItalic,FontStyleItem == FontStyles.Italic);
  249. //AnnotAttribDir.Add(AnnotAttrib.IsBold, FontWeightItem == FontWeights.Bold);
  250. //PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
  251. //GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  252. if (CurrentPresetFont != null)
  253. {
  254. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  255. if (currentItem.mTag != "Custom")
  256. {
  257. foreach (var item in PresetFontList)
  258. {
  259. if (item.mTag == currentItem.mTag)
  260. {
  261. if (FontWeightItem != item.mFontWeight || FontStyleItem != item.mFontStyle)
  262. {
  263. string txt = string.Format($" * {currentItem.mTagContent}");
  264. if (Title == txt)
  265. {
  266. Title = "";
  267. }
  268. Title = txt;
  269. break;
  270. }
  271. else
  272. {
  273. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  274. {
  275. string txt = currentItem.mTagContent;
  276. if (Title == txt)
  277. {
  278. Title = "";
  279. }
  280. Title = txt;
  281. }
  282. }
  283. }
  284. }
  285. }
  286. else
  287. {
  288. //bool isExist = GetCurrentPresetFont(Annot);
  289. //if (isExist)
  290. //{
  291. //}
  292. }
  293. }
  294. }
  295. }
  296. /// <summary>
  297. /// 用所选部分重新定义
  298. /// </summary>
  299. private void ReDefineFontStyle()
  300. {
  301. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  302. if (item == null) return;
  303. item.mFontFamily = new FontFamily(CurrentFontFamily.ValueStr);
  304. if (CurrrentFontWeightStyle.ValueStr == "Bold")
  305. {
  306. item.mFontStyle = FontStyles.Normal;
  307. item.mFontWeight = FontWeights.Bold;
  308. }
  309. else if (CurrrentFontWeightStyle.ValueStr == "Regular")
  310. {
  311. item.mFontStyle = FontStyles.Normal;
  312. item.mFontWeight = FontWeights.Normal;
  313. }
  314. else if (CurrrentFontWeightStyle.ValueStr == "Italic")
  315. {
  316. item.mFontStyle = FontStyles.Italic;
  317. item.mFontWeight = FontWeights.Normal;
  318. }
  319. else
  320. {
  321. item.mFontStyle = FontStyles.Italic;
  322. item.mFontWeight = FontWeights.Bold;
  323. }
  324. item.mFontSize = (int)CurrentFontSize.Value;
  325. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  326. SelectedPresetFont();
  327. }
  328. //重置定义
  329. private void RestoreDefaultStyle()
  330. {
  331. //var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  332. var defaultlists = TextFont.GetPresetFontStyle();
  333. if (CurrentPresetFont.ValueStr != "Custom")
  334. {
  335. var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag ==CurrentPresetFont.ValueStr);
  336. if (defaulItem != null)
  337. {
  338. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  339. if (currentItem != null)
  340. {
  341. currentItem.mTagContent = defaulItem.mTagContent;
  342. currentItem.mFontStyle = defaulItem.mFontStyle;
  343. currentItem.mFontWeight = defaulItem.mFontWeight;
  344. currentItem.mFontFamily = defaulItem.mFontFamily;
  345. currentItem.mFontSize = defaulItem.mFontSize;
  346. GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
  347. GetCurrentFontSize(currentItem.mFontSize);
  348. GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
  349. CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
  350. SelectedPresetFont();
  351. }
  352. }
  353. }
  354. }
  355. private void LayoutAlignChecked(object obj)
  356. {
  357. if (obj != null)
  358. {
  359. if (obj != null)
  360. {
  361. switch ((string)obj)
  362. {
  363. case "AlignLeft":
  364. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  365. break;
  366. case "AlignHorizonCenter":
  367. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  368. break;
  369. case "AlignRight":
  370. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  371. break;
  372. case "DistributeHorizontal":
  373. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  374. break;
  375. case "AlignTop":
  376. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  377. break;
  378. case "AlignVerticalCenter":
  379. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  380. break;
  381. case "AlignBottom":
  382. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  383. break;
  384. case "DistributeVertical":
  385. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  386. break;
  387. }
  388. }
  389. }
  390. }
  391. private void TextAlignChecked(object obj)
  392. {
  393. if ((string)obj != null && TextEditEvent != null)
  394. {
  395. switch ((string)obj)
  396. {
  397. case "AlignLeft":
  398. GetAnnotAlign(TextAlignment.Left);
  399. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
  400. break;
  401. case "AlignCenter":
  402. GetAnnotAlign(TextAlignment.Center);
  403. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
  404. break;
  405. case "AlignRight":
  406. GetAnnotAlign(TextAlignment.Right);
  407. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
  408. break;
  409. case "Align":
  410. GetAnnotAlign(TextAlignment.Justify);
  411. TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
  412. break;
  413. }
  414. TextAlign = TextEditEvent.TextAlign;
  415. TextEditEvent.UpdatePDFEditByEventArgs();
  416. }
  417. }
  418. //文本内容对齐
  419. private void GetAnnotAlign(TextAlignment align)
  420. {
  421. switch (align)
  422. {
  423. case TextAlignment.Left:
  424. StrTextAlign = "Left";
  425. break;
  426. case TextAlignment.Center:
  427. StrTextAlign = "Center";
  428. break;
  429. case TextAlignment.Right:
  430. StrTextAlign = "Right";
  431. break;
  432. case TextAlignment.Justify:
  433. StrTextAlign = "Justify";
  434. break;
  435. default:
  436. StrTextAlign = "None";
  437. break;
  438. }
  439. }
  440. private void SelectedColor(object obj)
  441. {
  442. if (obj != null)
  443. {
  444. var colorValue = (Color)obj;
  445. if (colorValue != null)
  446. {
  447. SelectColor = new SolidColorBrush(colorValue);
  448. }
  449. }
  450. }
  451. private void SelectedFontStyle(object obj)
  452. {
  453. if (obj != null && (PresetFontItem)obj != null)
  454. {
  455. var item = (PresetFontItem)obj;
  456. }
  457. }
  458. private void FontSizeChanged(object obj)
  459. {
  460. if (CurrentFontSize != null)
  461. {
  462. CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  463. //GetCurrentFontSize((int)CurrentFontSize.Value);
  464. //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize,CurrentFontSize.Value);
  465. }
  466. if (CurrentPresetFont != null)
  467. {
  468. var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
  469. if (currentItem.mTag != "Custom")
  470. {
  471. foreach (var item in PresetFontList)
  472. {
  473. if (item.mTag == currentItem.mTag)
  474. {
  475. if (CurrentFontSize.Value != item.mFontSize)
  476. {
  477. string txt = string.Format($" * {currentItem.mTagContent}");
  478. if (Title == txt)
  479. {
  480. Title = "";
  481. }
  482. Title = txt;
  483. break;
  484. }
  485. else
  486. {
  487. if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
  488. {
  489. string txt = currentItem.mTagContent;
  490. if (Title == txt)
  491. {
  492. Title = "";
  493. }
  494. Title = txt;
  495. }
  496. }
  497. }
  498. }
  499. }
  500. else
  501. {
  502. //bool isExist = GetCurrentPresetFont(Annot);
  503. //if (isExist)
  504. //{
  505. //}
  506. }
  507. }
  508. //if (obj != null)
  509. //{
  510. // var item = (ComboBoxItem)obj;
  511. // var content = (string)item.Content;
  512. // if (content != null)
  513. // {
  514. // var intData = int.Parse(content);
  515. // //TextFontSize = intData;
  516. // }
  517. //}
  518. }
  519. #endregion
  520. #region 布局处理
  521. private void ImgAlignChecked(object obj)
  522. {
  523. if (obj != null)
  524. {
  525. switch ((string)obj)
  526. {
  527. case "AlignLeft":
  528. PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
  529. break;
  530. case "AlignHorizonCenter":
  531. PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
  532. break;
  533. case "AlignRight":
  534. PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
  535. break;
  536. case "DistributeHorizontal":
  537. PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
  538. break;
  539. case "AlignTop":
  540. PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
  541. break;
  542. case "AlignVerticalCenter":
  543. PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
  544. break;
  545. case "AlignBottom":
  546. PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
  547. break;
  548. case "DistributeVertical":
  549. PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
  550. break;
  551. }
  552. }
  553. }
  554. private void ReLoadLayoutAlign(int count)
  555. {
  556. if (count >= 2)
  557. IsLayoutAlign = true;
  558. else
  559. IsLayoutAlign = false;
  560. if (count >= 3)
  561. IsLayoutAvgAlign = true;
  562. else
  563. IsLayoutAvgAlign = false;
  564. }
  565. #endregion
  566. #region 右键菜单
  567. //点击空白处时
  568. private ContextMenu EmptyStateMenu(object sender)
  569. {
  570. var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
  571. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  572. //粘贴
  573. customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
  574. //添加文本
  575. customMenu.SetMenuBinding(1, AddTextCommand);
  576. //添加图像
  577. customMenu.SetMenuBinding(2, AddImgCommand);
  578. return popMenu;
  579. }
  580. //选中文字的框
  581. private ContextMenu SelectTextBorder(object sender)
  582. {
  583. var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
  584. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  585. //编辑
  586. customMenu.SetMenuBinding(0, EditTextModeCommand);
  587. //复制
  588. customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
  589. //剪切
  590. customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
  591. //粘贴
  592. customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
  593. //删除
  594. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  595. return popMenu;
  596. }
  597. //多选文本框
  598. private ContextMenu SelectMoreText(object sender)
  599. {
  600. var popMenu = App.Current.FindResource("SelectMoreTextMenu") as ContextMenu;
  601. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  602. //复制
  603. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  604. //剪切
  605. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  606. //粘贴
  607. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  608. //删除
  609. customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
  610. return popMenu;
  611. }
  612. //选中文字内容
  613. private ContextMenu SelectTextContent(object sender)
  614. {
  615. var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
  616. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  617. //复制
  618. customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  619. //剪切
  620. customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  621. //粘贴
  622. customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
  623. //粘贴并匹配样式
  624. customMenu.SetVisibilityProperty(3, false);
  625. //删除
  626. customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  627. //全部选定
  628. customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
  629. return popMenu;
  630. }
  631. //编辑中右键
  632. private ContextMenu EditTextContent(object sender)
  633. {
  634. var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
  635. CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
  636. ////复制
  637. //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
  638. ////剪切
  639. //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
  640. //全部选定
  641. customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
  642. //粘贴
  643. customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
  644. //粘贴并匹配样式
  645. customMenu.SetVisibilityProperty(2, false);
  646. ////删除
  647. //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
  648. return popMenu;
  649. }
  650. #endregion
  651. #region 编辑PDF内容触发的事件
  652. /// <summary>
  653. /// 右键菜单的事件
  654. /// </summary>
  655. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  656. {
  657. if (e == null)
  658. return;
  659. switch (e.CommandType)
  660. {
  661. case CommandType.Context:
  662. // if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  663. // {
  664. // //文字编辑
  665. // if (e.PressOnBorder == true)
  666. // {
  667. // e.PopupMenu = SelectTextBorder(sender);
  668. // }
  669. // else if(e.PressOnSelectedText == true)
  670. // {
  671. // e.PopupMenu = SelectTextContent(sender);
  672. // }
  673. // else if(e.SelectText=="")
  674. // {
  675. // e.PopupMenu= EditTextContent(sender);
  676. // }
  677. // else
  678. // {
  679. // e.PopupMenu = SelectTextContent(sender);
  680. // }
  681. // }
  682. //else
  683. if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
  684. {
  685. e.PopupMenu = EmptyStateMenu(sender);
  686. }
  687. break;
  688. default:
  689. e.DoCommand();
  690. break;
  691. }
  692. if (e.PopupMenu != null)
  693. {
  694. e.Handle = true;
  695. }
  696. }
  697. /// <summary>
  698. /// 更多菜单
  699. /// </summary>
  700. /// <param name="isEnable"></param>
  701. /// <returns></returns>
  702. private ContextMenu SelectAnnotContextMenu(bool isEnable)
  703. {
  704. var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu;
  705. if (popMenu != null && popMenu.Items.Count == 2)
  706. {
  707. //用所选部分重新定义
  708. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  709. menuItem.IsEnabled = isEnable;
  710. menuItem.Command = ReDefineFontStyleCommand;
  711. //恢复默认预设样式
  712. menuItem = popMenu.Items[1] as MenuItem;
  713. menuItem.IsEnabled = isEnable;
  714. menuItem.Command = RestoreDefaultStyleCommand;
  715. }
  716. return popMenu;
  717. }
  718. #region 全局
  719. public event EventHandler ClearCheckedAglin;
  720. #endregion
  721. #endregion
  722. protected List<PDFEditEvent> TextEditEventList;
  723. public void OnNavigatedTo(NavigationContext navigationContext)
  724. {
  725. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  726. navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
  727. if (PDFViewer != null)
  728. {
  729. if (TextEditEventList != null && TextEditEventList.Count > 0)
  730. {
  731. TextEditEvent = TextEditEventList[0];
  732. GetPDFEdit();
  733. if (TextEditEventList.Count > 1)
  734. IsSelectedEmpty = true;
  735. else
  736. IsSelectedEmpty = false;
  737. if (TextEditEventList.Count == 2)
  738. {
  739. IsLayoutAlign = true;
  740. IsLayoutAvgAlign = false;
  741. }
  742. else if (TextEditEventList.Count > 2)
  743. {
  744. IsLayoutAlign = true;
  745. IsLayoutAvgAlign = true;
  746. }
  747. else
  748. {
  749. IsLayoutAlign = false;
  750. IsLayoutAvgAlign = false;
  751. }
  752. }
  753. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  754. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  755. ChangedValue -= FontMode_ChangedValue;
  756. ChangedValue += FontMode_ChangedValue;
  757. IsCanSave = true;
  758. }
  759. }
  760. private void FontMode_ChangedValue(object sender, FontSetModeType e)
  761. {
  762. if (sender != null)
  763. {
  764. switch (e)
  765. {
  766. case FontSetModeType.PresetFontStyes:
  767. if (PresetFontList != null && sender is string == true)
  768. {
  769. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
  770. if (item != null && TextEditEvent != null)
  771. {
  772. if (TextEditEventList.Count == 1 && IsSelectedEmpty == false)
  773. {
  774. TextEditEvent.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  775. TextEditEvent.FontSize = item.mFontSize;
  776. TextEditEvent.IsBold = item.mFontWeight == FontWeights.Bold;
  777. TextEditEvent.IsItalic = item.mFontStyle == FontStyles.Italic;
  778. TextEditEvent.UpdatePDFEditByEventArgs();
  779. }
  780. else
  781. {
  782. foreach (var itemlist in TextEditEventList)
  783. {
  784. itemlist.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
  785. itemlist.FontSize = item.mFontSize;
  786. itemlist.IsBold = item.mFontWeight == FontWeights.Bold;
  787. itemlist.IsItalic = item.mFontStyle == FontStyles.Italic;
  788. itemlist.UpdatePDFEditByEventArgs();
  789. }
  790. }
  791. }
  792. }
  793. break;
  794. case FontSetModeType.FontFamilys:
  795. if (sender is string == true)
  796. {
  797. TextEditEvent.FontName = (string)sender;
  798. TextEditEvent.UpdatePDFEditByEventArgs();
  799. }
  800. break;
  801. case FontSetModeType.FontSizes:
  802. if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
  803. {
  804. TextEditEvent.FontSize = (double)sender;
  805. TextEditEvent.UpdatePDFEditByEventArgs();
  806. }
  807. break;
  808. case FontSetModeType.FontWeight_Style:
  809. UpdateFontWeight_Style();
  810. TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
  811. TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
  812. TextEditEvent.UpdatePDFEditByEventArgs();
  813. break;
  814. case FontSetModeType.FontColor:
  815. if (sender is Color == true)
  816. {
  817. var setColor = (Color)sender;
  818. var eventColor = TextEditEvent.FontColor;
  819. bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
  820. eventColor.G != setColor.G || eventColor.R != setColor.R;
  821. if (isok)
  822. {
  823. TextEditEvent.FontColor = setColor;
  824. TextEditEvent.UpdatePDFEditByEventArgs();
  825. }
  826. }
  827. break;
  828. case FontSetModeType.TextAlignment:
  829. break;
  830. }
  831. }
  832. }
  833. private void GetPDFEdit()
  834. {
  835. SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  836. CurrentFontSize =new ComboDataItem(TextEditEvent.FontSize);
  837. CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  838. FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
  839. FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
  840. GetFontWeights_Style(FontStyleItem, FontWeightItem);
  841. bool isExist = false;
  842. //List<PresetFontItem> presetFontItems = TextFont.GetCachePresetFontList();
  843. //遍历样式
  844. foreach (var item in PresetFontList)
  845. {
  846. if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
  847. && (TextEditEvent.FontName == item.mFontFamily.Source || TextEditEvent.FontName == "Arial" && item.mFontFamily.Source == "Helvetica")
  848. )
  849. {
  850. if (item.mTag != "Custom")
  851. {
  852. CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
  853. isExist = true;
  854. }
  855. break;
  856. }
  857. }
  858. if (isExist == false)
  859. {
  860. FontVm.CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  861. CurrentPresetFont = new ComboDataItem("Custom", "Custom");
  862. GetCurrentFontSize((int)TextEditEvent.FontSize);
  863. if (TextEditEvent.FontName == ""|| TextEditEvent.FontName == null)
  864. {
  865. GetCurrentFontFamily("Helvetica", "Helvetica");
  866. }
  867. else
  868. {
  869. GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  870. }
  871. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  872. }
  873. else
  874. {
  875. if (CurrentPresetFont != null)
  876. {
  877. var itemComboxItem = CurrentPresetFont;
  878. var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
  879. if (item != null)
  880. {
  881. if (item.mTag != "Custom")
  882. {
  883. //CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
  884. GetCurrentFontSize(item.mFontSize);
  885. if (item.mFontFamily != null)
  886. {
  887. //CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
  888. GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
  889. }
  890. //FontStyleItem = item.mFontStyle;
  891. //FontWeightItem = item.mFontWeight;
  892. GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  893. }
  894. }
  895. }
  896. }
  897. //初始化传入的对齐方式
  898. switch (TextEditEvent.TextAlign.ToString())
  899. {
  900. case "AlignLeft":
  901. GetAnnotAlign(TextAlignment.Left);
  902. break;
  903. case "AlignCenter":
  904. GetAnnotAlign(TextAlignment.Center);
  905. break;
  906. case "AlignRight":
  907. GetAnnotAlign(TextAlignment.Right);
  908. break;
  909. case "Align":
  910. GetAnnotAlign(TextAlignment.Justify);
  911. break;
  912. }
  913. TextAlign = TextEditEvent.TextAlign;
  914. TextEditEvent.UpdatePDFEditByEventArgs();
  915. //if (TextEditEvent != null && TextEditEventList.Count == 1)
  916. //{
  917. // SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
  918. // GetCurrentFontSize((int)TextEditEvent.FontSize);
  919. // if (TextEditEvent.FontName != null)
  920. // {
  921. // GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
  922. // }
  923. // GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
  924. // TextAlign = TextEditEvent.TextAlign;
  925. //}
  926. }
  927. public bool IsNavigationTarget(NavigationContext navigationContext) { return true; }
  928. public void OnNavigatedFrom(NavigationContext navigationContext)
  929. {
  930. IsCanSave = false;
  931. TextEditEvent = null;
  932. IsSelectedEmpty = false;
  933. ClearCheckedAglin?.Invoke(null, null);
  934. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  935. ChangedValue -= FontMode_ChangedValue;
  936. }
  937. }
  938. }