TextEditPropertyViewModel.cs 40 KB

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