FillAndSignContentViewModel.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using PDF_Office.ViewModels.Tools;
  15. using ComPDFKitViewer;
  16. using PDF_Office.Helper;
  17. using PDF_Office.Properties;
  18. using PDFSettings;
  19. using System.Windows.Media;
  20. using ComPDFKit.PDFDocument.Action;
  21. using ComPDFKit.PDFDocument;
  22. using System.Diagnostics;
  23. using ComPDFKit.PDFAnnotation;
  24. using PDF_Office.ViewModels.BOTA;
  25. using PDF_Office.Views.BOTA;
  26. using System.Windows.Forms;
  27. using Control = System.Windows.Controls.Control;
  28. using Microsoft.Office.Interop.Word;
  29. using Point = System.Windows.Point;
  30. using System.Windows.Ink;
  31. using PDF_Office.EventAggregators;
  32. using System.Management.Instrumentation;
  33. using Prism.Events;
  34. using System.Windows.Input;
  35. using ContextMenu = System.Windows.Controls.ContextMenu;
  36. using MenuItem = System.Windows.Controls.MenuItem;
  37. using System.Drawing;
  38. using Color = System.Windows.Media.Color;
  39. using Brush = System.Windows.Media.Brush;
  40. using FontFamily = System.Windows.Media.FontFamily;
  41. using ToolTip = System.Windows.Controls.ToolTip;
  42. using ContentControl = System.Windows.Controls.ContentControl;
  43. using MouseEventArgs = System.Windows.Input.MouseEventArgs;
  44. using Pen = System.Windows.Media.Pen;
  45. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  46. using RadioButton = System.Windows.Controls.RadioButton;
  47. using Style = System.Windows.Style;
  48. using PDF_Office.CustomControl;
  49. using System.Windows.Controls.Primitives;
  50. using Window = System.Windows.Window;
  51. using Task = System.Threading.Tasks.Task;
  52. using Size = System.Windows.Size;
  53. using System.Windows.Markup;
  54. namespace PDF_Office.ViewModels.FillAndSign
  55. {
  56. public class FillAndSignContentViewModel : BindableBase, INavigationAware
  57. {
  58. #region
  59. private CPDFViewer PDFViewer;
  60. private AnnotPropertyPanel propertyPanel = new AnnotPropertyPanel();
  61. private ViewContentViewModel viewContentViewModel;
  62. private bool isRightMenuAddAnnot = false;
  63. private IEventAggregator events;
  64. public List<List<Point>> ShapePoints = new List<List<Point>>();
  65. private string Shape = "HookShape";
  66. private PopMenu FreeHandAnnotPopMenu;
  67. private PopMenu FreeTextAnnotPopMenu;
  68. private PopMenu MultiAnnotPopMenu;
  69. public bool isFirst=true;
  70. private IRegionManager regions { get; set; }
  71. private Dictionary<string, AnnotArgsType> ToolExpandDict = new Dictionary<string, AnnotArgsType>();
  72. public static bool IsEdit = false;
  73. public static bool IsSignPoint = false;
  74. public Brush SelectColor = new SolidColorBrush(Colors.GreenYellow);
  75. public double FillOpacity = 1;
  76. public double LineWidth = 1;
  77. public double LineWidthMultiple = 1;
  78. private bool _HookShapeIsCheck = true;
  79. public bool HookShapeIsCheck
  80. {
  81. get { return _HookShapeIsCheck; }
  82. set { SetProperty(ref _HookShapeIsCheck, value); }
  83. }
  84. #endregion
  85. public DelegateCommand<object> AnnotDefaultValue_MenuCommand { get; set; }
  86. public DelegateCommand<object> FreeTextFontFamily_MenuCommand { get; set; }
  87. public DelegateCommand<object> AnnotColorPalette_MenuCommand { get; set; }
  88. public DelegateCommand<object> FreeTextAglin_MenuCommand { get; set; }
  89. #region Command
  90. public DelegateCommand<RoutedEventArgs> CheckCommand { get; set; }
  91. /// <summary>
  92. /// 按钮名称和属性面板映射字典
  93. /// </summary>
  94. public Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  95. #endregion
  96. public FillAndSignContentViewModel(IRegionManager regionManager, IEventAggregator events)
  97. {
  98. regions = regionManager;
  99. this.events = events;
  100. CheckCommand = new DelegateCommand<RoutedEventArgs>(CheckedEvent);
  101. ToolExpandDict.Add("Freetext", AnnotArgsType.AnnotFreeText);
  102. InitDictionary();
  103. AnnotDefaultValue_MenuCommand = new DelegateCommand<object>(AnnotDefaultValues_Menu);
  104. AnnotColorPalette_MenuCommand = new DelegateCommand<object>(AnnotColorPalette_Menu);
  105. FreeTextFontFamily_MenuCommand = new DelegateCommand<object>(FreeTextFontFamily_Menu);
  106. FreeTextAglin_MenuCommand = new DelegateCommand<object>(FreeTextAglin_Menu);
  107. InitSelectFreeHandAnnotMenu();
  108. InitSelectFreeTextAnnotMenu();
  109. }
  110. private void InitFillAndSignProperty()
  111. {
  112. var FreehandDefault = GetAnnotDefault(AnnotArgsType.AnnotFreehand, "FillAndSign");
  113. SelectColor = new SolidColorBrush(FreehandDefault.BorderColor != Color.FromArgb(0, 0, 0, 0) ? FreehandDefault.BorderColor : Colors.GreenYellow);
  114. FillOpacity = FreehandDefault.Opacity > 0 ? FreehandDefault.Opacity : 1;
  115. LineWidth = FreehandDefault.Thickness > 0 ? FreehandDefault.Thickness : 1;
  116. }
  117. private System.Windows.Controls.Primitives.Popup popup = null;
  118. private async void AnnotColorPalette_Menu(object obj)
  119. {
  120. if (obj as CusMenuItem != null)
  121. {
  122. var menu = obj as CusMenuItem;
  123. var annot = menu.Parameter as AnnotHandlerEventArgs;
  124. if (annot != null)
  125. {
  126. var item = new ColorDropBoxPop();
  127. item.DataContext = menu;
  128. item.ColorSelected -= AnnotMenu_ColorSelected;
  129. item.ColorSelected += AnnotMenu_ColorSelected;
  130. if (popup == null)
  131. popup = new System.Windows.Controls.Primitives.Popup();
  132. ContentControl window = null;
  133. if (PDFViewer.Parent as ContentControl != null)
  134. window = PDFViewer.Parent as ContentControl;
  135. else
  136. window = App.Current.MainWindow;
  137. popup.Child = item;
  138. popup.PlacementRectangle = new Rect(Mouse.GetPosition(window), new Size(item.Width, item.Height));
  139. popup.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
  140. popup.PlacementTarget = window;
  141. popup.IsOpen = true;
  142. Window parentWnd = Window.GetWindow(App.Current.MainWindow);
  143. if (parentWnd != null)
  144. {
  145. parentWnd.MouseDown -= parentWnd_MouseDown;
  146. parentWnd.MouseDown += parentWnd_MouseDown;
  147. }
  148. while (popup.IsOpen)
  149. await Task.Delay(20);
  150. parentWnd.MouseDown -= parentWnd_MouseDown;
  151. popup = null;
  152. }
  153. }
  154. }
  155. private void AnnotMenu_ColorSelected(object sender, Color e)
  156. {
  157. if (sender != null)
  158. {
  159. var menu = (sender as FrameworkElement).DataContext as CusMenuItem;
  160. if (menu == null) return;
  161. var annot = menu.Parameter as AnnotHandlerEventArgs;
  162. if (annot != null)
  163. {
  164. if (annot is FreehandAnnotArgs || annot is StickyAnnotArgs || annot is LineAnnotArgs)
  165. {
  166. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  167. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  168. AnnotEvent?.UpdateAnnot();
  169. }
  170. else if (annot is FreeTextAnnotArgs)
  171. {
  172. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  173. if (menu.tag.ToString() == "FontColor")
  174. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, e);
  175. else
  176. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, e);
  177. AnnotEvent?.UpdateAnnot();
  178. }
  179. else if (annot is SquareAnnotArgs || annot is CircleAnnotArgs)
  180. {
  181. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  182. if (menu.tag.ToString() == "FillColor")
  183. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, e);
  184. else
  185. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  186. AnnotEvent?.UpdateAnnot();
  187. }
  188. else if (annot is TextHighlightAnnotArgs || annot is TextUnderlineAnnotArgs || annot is TextStrikeoutAnnotArgs)
  189. {
  190. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  191. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  192. AnnotEvent?.UpdateAnnot();
  193. }
  194. }
  195. }
  196. }
  197. private void parentWnd_MouseDown(object sender, MouseButtonEventArgs e)
  198. {
  199. var ui = Mouse.DirectlyOver as FrameworkElement;
  200. if (ui != null)
  201. {
  202. if (popup != null)
  203. {
  204. popup.IsOpen = false;
  205. }
  206. }
  207. }
  208. private void FreeTextFontFamily_Menu(object obj)
  209. {
  210. if (obj as CusMenuItem != null)
  211. {
  212. var menu = obj as CusMenuItem;
  213. var annot = menu.Parameter as AnnotHandlerEventArgs;
  214. if (annot != null)
  215. {
  216. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  217. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily(menu.tag.ToString()));
  218. AnnotEvent?.UpdateAnnot();
  219. }
  220. }
  221. }
  222. private void FreeTextAglin_Menu(object obj)
  223. {
  224. if (obj as CusMenuItem != null)
  225. {
  226. var menu = obj as CusMenuItem;
  227. var annot = menu.Parameter as AnnotHandlerEventArgs;
  228. var tag = menu.control.Tag;
  229. if (annot != null && tag != null)
  230. {
  231. var strTag = tag.ToString();
  232. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  233. if (strTag == "Left")
  234. {
  235. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  236. }
  237. else if (strTag == "Center")
  238. {
  239. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  240. }
  241. else if (strTag == "Right")
  242. {
  243. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  244. }
  245. else if (strTag == "Justify")
  246. {
  247. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify);
  248. }
  249. AnnotEvent?.UpdateAnnot();
  250. }
  251. }
  252. }
  253. private void AnnotDefaultValues_Menu(object obj)
  254. {
  255. if (obj as CusMenuItem != null)
  256. {
  257. var menu = obj as CusMenuItem;
  258. var annot = menu.Parameter as AnnotHandlerEventArgs;
  259. if (annot != null)
  260. {
  261. if (annot is FreehandAnnotArgs)
  262. {
  263. var freeHand = annot as FreehandAnnotArgs;
  264. var color = freeHand.InkColor;
  265. //Settings.Default.AppProperties.Annotate.FreeHandColor = color;
  266. var FreehandDefault = GetAnnotDefault(AnnotArgsType.AnnotFreehand, "FillAndSign");
  267. FreehandDefault.BorderColor = color;
  268. FreehandDefault.Opacity = freeHand.Transparency;
  269. FreehandDefault.Thickness = freeHand.LineWidth;
  270. FreehandDefault.SaveKey = "FillAndSign";
  271. var isSolid = AnnotPropertyPanel.IsSolidStyle(freeHand.LineDash);
  272. if (isSolid)
  273. {
  274. FreehandDefault.DashArray = null;
  275. }
  276. else
  277. {
  278. FreehandDefault.DashArray = new List<double>();
  279. foreach (var item in freeHand.LineDash.Dashes)
  280. {
  281. FreehandDefault.DashArray.Add(item);
  282. }
  283. }
  284. SettingHelper.SetAnnotDefaultProperty(FreehandDefault);
  285. }
  286. else if (annot is FreeTextAnnotArgs)
  287. {
  288. var freeText = annot as FreeTextAnnotArgs;
  289. Settings.Default.AppProperties.Annotate.TextAnnoteColor = freeText.FontColor;
  290. Settings.Default.AppProperties.Annotate.TextFontFamaily = freeText.FontFamily.ToString();
  291. Settings.Default.AppProperties.Annotate.TextAlign = freeText.Align;
  292. var FreeTextDefalut = GetAnnotDefault(AnnotArgsType.AnnotFreeText);
  293. FreeTextDefalut.ForgoundColor = freeText.FontColor;
  294. FreeTextDefalut.FontFamily = freeText.FontFamily.ToString();
  295. FreeTextDefalut.TextAlign = freeText.Align;
  296. FreeTextDefalut.FontSize = freeText.FontSize;
  297. FreeTextDefalut.FontWeight = freeText.FontWeight;
  298. FreeTextDefalut.FontStyle = freeText.FontStyle;
  299. FreeTextDefalut.BackgroundColor = freeText.BgColor;
  300. FreeTextDefalut.Opacity = (annot as FreeTextAnnotArgs).Transparency;
  301. SettingHelper.SetAnnotDefaultProperty(FreeTextDefalut);
  302. }
  303. }
  304. }
  305. }
  306. private DefaultAnnotProperty GetAnnotDefault(AnnotArgsType annotArgsType,string savaKey="")
  307. {
  308. var Annot = SettingHelper.GetAnnotDefaultProperty(annotArgsType, savaKey);
  309. if (Annot == null)
  310. {
  311. Annot = new DefaultAnnotProperty();
  312. Annot.AnnotToolType = annotArgsType;
  313. }
  314. return Annot;
  315. }
  316. private void CheckedEvent(RoutedEventArgs e)
  317. {
  318. IsEdit = false;
  319. var control = e.OriginalSource as Control;
  320. //NavigateToProperty(control.Name);
  321. //不创建注释,属于注释模板
  322. bool isTemplateAnnot = false;
  323. bool isSnapshotEdit = false;
  324. AnnotHandlerEventArgs annotArgs = null;
  325. var tag = control.Name;
  326. FindAnnotTypeKey(control.Name, ref annotArgs);
  327. if (control.Name == "RbtnSign") { isTemplateAnnot = true; }
  328. if (tag == "SnapshotEdit")
  329. {
  330. isSnapshotEdit = true;
  331. }
  332. else if (tag == "Signature" || tag == "Stamp")
  333. {
  334. isTemplateAnnot = true;
  335. }
  336. if (annotArgs != null)
  337. {
  338. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  339. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  340. PDFViewer.SetToolParam(annotArgs);
  341. isRightMenuAddAnnot = false;
  342. }
  343. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  344. if (isTemplateAnnot == false && annotArgs == null)
  345. {
  346. PDFViewer.SetMouseMode(MouseModes.PanTool);
  347. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  348. }
  349. else
  350. {
  351. if (isSnapshotEdit == true)
  352. {
  353. ShowPropertyPanel(false);
  354. isSnapshotEdit = false;
  355. }
  356. else
  357. {
  358. ShowPropertyPanel(true);
  359. }
  360. }
  361. }
  362. private void InitCheckedEvent(string e)
  363. {
  364. IsEdit = false;
  365. //NavigateToProperty(control.Name);
  366. //不创建注释,属于注释模板
  367. bool isTemplateAnnot = false;
  368. bool isSnapshotEdit = false;
  369. AnnotHandlerEventArgs annotArgs = null;
  370. var tag = e;
  371. FindAnnotTypeKey(e, ref annotArgs);
  372. if (e == "RbtnSign") { isTemplateAnnot = true; }
  373. if (tag == "SnapshotEdit")
  374. {
  375. isSnapshotEdit = true;
  376. }
  377. else if (tag == "Signature" || tag == "Stamp")
  378. {
  379. isTemplateAnnot = true;
  380. }
  381. if (annotArgs != null)
  382. {
  383. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  384. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  385. PDFViewer.SetToolParam(annotArgs);
  386. isRightMenuAddAnnot = false;
  387. }
  388. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  389. if (isTemplateAnnot == false && annotArgs == null)
  390. {
  391. PDFViewer.SetMouseMode(MouseModes.PanTool);
  392. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  393. }
  394. else
  395. {
  396. if (isSnapshotEdit == true)
  397. {
  398. ShowPropertyPanel(false);
  399. isSnapshotEdit = false;
  400. }
  401. else
  402. {
  403. ShowPropertyPanel(true);
  404. }
  405. }
  406. }
  407. //注释工具
  408. private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false)
  409. {
  410. LineWidthMultiple = 1;
  411. IsSignPoint = false;
  412. switch (tag)
  413. {
  414. case "RbtnDate":
  415. case "RbtnText"://文本
  416. annotArgs = GetFreetext();
  417. break;
  418. case "Freetext"://文本
  419. annotArgs = GetFreetext();
  420. break;
  421. case "RbtnSign"://签名
  422. annotArgs = GetSignature();
  423. PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
  424. break;
  425. case "RbtnTick"://勾
  426. Shape = "HookShape";
  427. ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };
  428. annotArgs = GetStamp();
  429. break;
  430. case "RbtnFork"://叉
  431. Shape = "ForkShape";
  432. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(12.8, 12.8) }, new List<Point> { new Point(12.8, 3.20001), new Point(3.20005, 12.8) } };
  433. annotArgs = GetStamp();
  434. break;
  435. case "RbtnRectangle"://矩形
  436. Shape = "RectShape";
  437. ShapePoints = new List<List<Point>> { new List<Point> { new Point(5, 5), new Point(28, 5) }, new List<Point> { new Point(28, 5), new Point(28, 27) }, new List<Point> { new Point(28, 27), new Point(5, 27) }, new List<Point> { new Point(5, 27), new Point(5, 5) } };
  438. annotArgs = GetStamp();
  439. break;
  440. case "RbtnLine"://下划线
  441. Shape = "LineShape";
  442. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(28, 3.20001) } };
  443. annotArgs = GetStamp();
  444. break;
  445. case "RbtnPoint":
  446. IsSignPoint = true;
  447. Shape = "DotShape";
  448. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.19995), new Point(3.19995, 3.19995) } };
  449. LineWidthMultiple = 5;
  450. annotArgs = GetStamp();
  451. break;
  452. default://图章
  453. break;
  454. }
  455. }
  456. /// <summary>
  457. /// 签名
  458. /// </summary>
  459. /// <returns></returns>
  460. private AnnotHandlerEventArgs GetSignature()
  461. {
  462. AddToPropertyPanel("SignatureAnnotProperty");
  463. return null;
  464. }
  465. /// <summary>
  466. /// 图章
  467. /// </summary>
  468. /// <returns></returns>
  469. private AnnotHandlerEventArgs GetStamp(List<AnnotHandlerEventArgs> selectedArgs = null)
  470. {
  471. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  472. stampAnnotArgs.SetInkData(ShapePoints, LineWidth * LineWidthMultiple, (SelectColor as SolidColorBrush).Color);
  473. //PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  474. //PDFViewer.SetToolParam(stampArgs);
  475. //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  476. //stampAnnotArgs.Opacity = 1;
  477. //stampAnnotArgs.StampText = "APPROVED";
  478. //stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  479. // stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  480. stampAnnotArgs.IsContinueMode = true;
  481. stampAnnotArgs.IsSignPoint = IsSignPoint;
  482. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  483. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  484. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp);
  485. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  486. if (stampAnnotArgs != null)
  487. stampAnnotArgsList.Add(stampAnnotArgs);
  488. //if (selectedArgs != null) {
  489. //var annot = selectedArgs[0];
  490. //if (annot != null)
  491. //{
  492. // foreach (var item in selectedArgs)
  493. // {
  494. // selectedArgs[selectedArgs.IndexOf(item)] = stampAnnotArgs;
  495. // }
  496. // AddToPropertyPanel("ShapFillProperty", "", selectedArgs, annotAttribsList);
  497. // return stampAnnotArgs;
  498. //}
  499. // }
  500. AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList);
  501. return stampAnnotArgs;
  502. }
  503. public void SetStamp()
  504. {
  505. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  506. stampAnnotArgs.SetInkData(ShapePoints, LineWidth * LineWidthMultiple, (SelectColor as SolidColorBrush).Color);
  507. //PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  508. //PDFViewer.SetToolParam(stampArgs);
  509. //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  510. //stampAnnotArgs.Opacity = 1;
  511. //stampAnnotArgs.StampText = "APPROVED";
  512. //stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  513. stampAnnotArgs.IsContinueMode = true;
  514. stampAnnotArgs.IsSignPoint = IsSignPoint;
  515. stampAnnotArgs.Opacity = FillOpacity;
  516. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  517. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  518. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp);
  519. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  520. if (stampAnnotArgs != null)
  521. stampAnnotArgsList.Add(stampAnnotArgs);
  522. //AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList);
  523. bool isTemplateAnnot = false;
  524. bool isSnapshotEdit = false;
  525. AnnotHandlerEventArgs annotArgs = stampAnnotArgs;
  526. if (annotArgs != null)
  527. {
  528. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  529. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  530. PDFViewer.SetToolParam(annotArgs);
  531. isRightMenuAddAnnot = false;
  532. }
  533. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  534. if (isTemplateAnnot == false && annotArgs == null)
  535. {
  536. PDFViewer.SetMouseMode(MouseModes.PanTool);
  537. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  538. }
  539. else
  540. {
  541. if (isSnapshotEdit == true)
  542. {
  543. ShowPropertyPanel(false);
  544. isSnapshotEdit = false;
  545. }
  546. else
  547. {
  548. ShowPropertyPanel(true);
  549. }
  550. }
  551. }
  552. /// <summary>
  553. /// 手绘
  554. /// </summary>
  555. /// <param name="selectedArgs"></param>
  556. /// <returns></returns>
  557. private AnnotHandlerEventArgs GetFreehand(List<AnnotHandlerEventArgs> selectedArgs = null)
  558. {
  559. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  560. List<AnnotHandlerEventArgs> newSelectedArgs = new List<AnnotHandlerEventArgs>();
  561. FreehandAnnotArgs freehandArgs = null;
  562. if (selectedArgs == null || selectedArgs.Count == 0)
  563. {
  564. freehandArgs = new FreehandAnnotArgs();
  565. var annotate = Settings.Default.AppProperties.Annotate;
  566. if (annotate != null)
  567. {
  568. freehandArgs.InkColor = annotate.FreeHandColor;
  569. }
  570. else
  571. {
  572. freehandArgs.InkColor = Color.FromRgb(0x38, 0xE0, 0x2E);
  573. }
  574. freehandArgs.RawPointList = new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };
  575. freehandArgs.Transparency = 1;
  576. freehandArgs.LineWidth = 2;
  577. if (freehandArgs != null)
  578. {
  579. selectedArgs = new List<AnnotHandlerEventArgs>();
  580. selectedArgs.Add(freehandArgs);
  581. }
  582. }
  583. else
  584. {
  585. freehandArgs = selectedArgs[0] as FreehandAnnotArgs;
  586. foreach (var item in selectedArgs)
  587. {
  588. //if (ListPoint((item as FreehandAnnotArgs).RawPointList, ShapePoints)){
  589. foreach (var point in viewContentViewModel.FillAndSign)
  590. {
  591. if ((item as FreehandAnnotArgs).PageIndex == point.X && (item as FreehandAnnotArgs).AnnotIndex == point.Y)
  592. {
  593. newSelectedArgs.Add(item);
  594. }
  595. }
  596. }
  597. freehandArgs.RawPointList = ShapePoints;
  598. freehandArgs.InkColor = (SelectColor as SolidColorBrush).Color;
  599. freehandArgs.Transparency = 1;
  600. freehandArgs.LineWidth = LineWidth;
  601. annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  602. annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  603. annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  604. annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  605. annotAttribsList[AnnotAttrib.Path] = freehandArgs.RawPointList;
  606. annotAttribsList[AnnotAttrib.Width] = freehandArgs.Width;
  607. annotAttribsList[AnnotAttrib.Height] = freehandArgs.Height;
  608. AddToPropertyPanel("ShapFillProperty", "Freehand", newSelectedArgs, annotAttribsList);
  609. //GetStamp();
  610. return freehandArgs;
  611. }
  612. //annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  613. //annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  614. //annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  615. //annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  616. //AddToPropertyPanel("ShapFillProperty", "Freehand", selectedArgs, annotAttribsList);
  617. return freehandArgs;
  618. }
  619. private bool ListPoint(List<List<Point>> leftpoints, List<List<Point>> rightpoints)
  620. {
  621. if (leftpoints.Count == rightpoints.Count)
  622. {
  623. if (leftpoints[0][0].X == rightpoints[0][0].X) { return true; }
  624. }
  625. return false;
  626. }
  627. /// <summary>
  628. /// 文本
  629. /// </summary>
  630. /// <param name="selectedfreetextArgs"></param>
  631. /// <returns></returns>
  632. private AnnotHandlerEventArgs GetFreetext(List<AnnotHandlerEventArgs> selectedArgs = null)
  633. {
  634. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  635. FreeTextAnnotArgs freetextArgs = null;
  636. TextAlignment textAlignment;
  637. if (selectedArgs == null || selectedArgs.Count == 0)
  638. {
  639. freetextArgs = new FreeTextAnnotArgs();
  640. freetextArgs.Align = TextAlignment.Left;
  641. freetextArgs.BgColor = Colors.Transparent;
  642. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  643. freetextArgs.FontColor = Colors.Black;
  644. freetextArgs.FontSize = 14;
  645. freetextArgs.Transparency = 1;
  646. freetextArgs.LineColor = Colors.Black;
  647. freetextArgs.LineWidth = 0;
  648. freetextArgs.TextContent = string.Empty;
  649. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  650. if (annotProperty != null)
  651. {
  652. freetextArgs.Align = annotProperty.TextAlign;
  653. freetextArgs.BgColor = annotProperty.BackgroundColor;
  654. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  655. freetextArgs.FontColor = annotProperty.ForgoundColor;
  656. freetextArgs.FontSize = annotProperty.FontSize;
  657. freetextArgs.Transparency = annotProperty.Opacity;
  658. freetextArgs.LineColor = annotProperty.BorderColor;
  659. freetextArgs.LineWidth = annotProperty.Thickness;
  660. freetextArgs.TextContent = annotProperty.NoteText;
  661. freetextArgs.FontWeight = annotProperty.FontWeight;
  662. freetextArgs.FontStyle = annotProperty.FontStyle;
  663. }
  664. int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
  665. if (align == 0)
  666. textAlignment = TextAlignment.Left;
  667. else if (align == 1)
  668. textAlignment = TextAlignment.Center;
  669. else
  670. textAlignment = TextAlignment.Right;
  671. if (freetextArgs != null)
  672. {
  673. selectedArgs = new List<AnnotHandlerEventArgs>();
  674. selectedArgs.Add(freetextArgs);
  675. }
  676. }
  677. else
  678. {
  679. freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
  680. textAlignment = freetextArgs.Align;
  681. }
  682. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  683. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  684. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  685. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  686. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  687. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  688. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  689. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  690. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  691. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  692. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  693. annotAttribsList[AnnotAttrib.Text] = freetextArgs.TextContent;
  694. AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList);
  695. return freetextArgs;
  696. }
  697. /// <summary>
  698. /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线
  699. /// </summary>
  700. /// <param name="viewContent">对应的注释面板</param>
  701. /// <param name="toolTag">导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线</param>
  702. /// <param name="annot">注释</param>
  703. /// <param name="annotAttribsList">更改注释属性的键值对,更改值后会自动记录undoRedo容器里</param>
  704. private void AddToPropertyPanel(string viewContent, string toolTag = null, List<AnnotHandlerEventArgs> annots = null, Dictionary<AnnotAttrib, object> annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null, bool isUpData = false)
  705. {
  706. if (annots != null && annots.Count != 0)
  707. {
  708. propertyPanel.annotlists = annots;
  709. propertyPanel.annot = annots[0];
  710. }
  711. else
  712. {
  713. propertyPanel.annotlists = null;
  714. propertyPanel.annot = null;
  715. }
  716. if (annotAttribsList != null && annotAttribsList.Count != 0 && annots != null && annots.Count != 0)
  717. {
  718. if (annots.Count > 1)
  719. {
  720. if (propertyPanel.AnnotEvents == null)
  721. propertyPanel.AnnotEvents = new List<AnnotAttribEvent>();
  722. propertyPanel.AnnotEvents.Clear();
  723. foreach (var itemAnnot in annots)
  724. {
  725. var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList);
  726. PDFViewer.SelectAnnotation(itemAnnot.PageIndex, itemAnnot.AnnotIndex);
  727. propertyPanel.AnnotEvents.Add(eventitem);
  728. }
  729. }
  730. propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList);
  731. if (annots[0] is LinkAnnotArgs && annotAttribEvent != null)
  732. {
  733. propertyPanel.AnnotEvent = annotAttribEvent;
  734. }
  735. }
  736. if (toolTag == "Freetext")
  737. {
  738. propertyPanel.SetIsTextFill(true);
  739. }
  740. else
  741. {
  742. propertyPanel.SetIsTextFill(false);
  743. }
  744. if (string.IsNullOrEmpty(viewContent) == false)
  745. {
  746. if (isUpData) { return; }
  747. if (viewContent == "ShapFillProperty") {
  748. NavigateToProperty(viewContent, propertyPanel); return; }
  749. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  750. }
  751. }
  752. private void NavigateToProperty(string btnName, AnnotPropertyPanel annotPropertyPanel)
  753. {
  754. NavigationParameters values = new NavigationParameters();
  755. values.Add(ParameterNames.PDFViewer, PDFViewer);
  756. values.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  757. values.Add("Shape", Shape);
  758. values.Add("FillAndSignContentViewModel", this);
  759. regions.RequestNavigate(RegionNames.PropertyRegionName, btnName, values);
  760. viewContentViewModel.IsPropertyOpen = true;
  761. }
  762. /// <summary>
  763. /// 初始化按钮名称-属性面板字典
  764. /// </summary>
  765. private void InitDictionary()
  766. {
  767. btnToProperty["RbtnText"] = "TextFillProperty";
  768. btnToProperty["RbtnTick"] = "ShapFillProperty";
  769. btnToProperty["RbtnFork"] = "ShapFillProperty";
  770. btnToProperty["RbtnRectangle"] = "ShapFillProperty";
  771. btnToProperty["RbtnLine"] = "ShapFillProperty";
  772. btnToProperty["RbtnPoint"] = "ShapFillProperty";
  773. btnToProperty["RbtnDate"] = "DateFillProperty";
  774. btnToProperty["RbtnSign"] = "";
  775. }
  776. /// <summary>
  777. /// 手绘
  778. /// </summary>
  779. private void InitSelectFreeHandAnnotMenu()
  780. {
  781. var popMenu = new ContextMenu();
  782. PopMenu pop = new PopMenu(popMenu);
  783. var menuItem = new MenuItem();
  784. menuItem.Name = "FreeHandCopy";
  785. menuItem.Header = "复制";
  786. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Copy);
  787. menuItem = new MenuItem();
  788. menuItem.Name = "FreeHandCut";
  789. menuItem.Header = "剪切";
  790. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Cut);
  791. menuItem = new MenuItem();
  792. menuItem.Name = "FreeHandPaste";
  793. menuItem.Header = "粘贴";
  794. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Paste);
  795. menuItem = new MenuItem();
  796. menuItem.Name = "FreeHandDelete";
  797. menuItem.Header = "删除";
  798. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Delete);
  799. pop.AddItem(GetSeparator());
  800. menuItem = new MenuItem();
  801. menuItem.Name = "FreeHandDefault";
  802. menuItem.Header = "设置当前属性为默认值";
  803. pop.BindingEvent(pop.AddItem(menuItem), AnnotDefaultValue_MenuCommand);
  804. FreeHandAnnotPopMenu = pop;
  805. }
  806. /// <summary>
  807. /// 文本
  808. /// </summary>
  809. private void InitSelectFreeTextAnnotMenu()
  810. {
  811. var popMenu = new ContextMenu();
  812. PopMenu pop = new PopMenu(popMenu);
  813. var menuItem = new MenuItem();
  814. menuItem.Name = "FreeTextCopy";
  815. menuItem.Header = "复制";
  816. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Copy);
  817. menuItem = new MenuItem();
  818. menuItem.Name = "FreeTextCut";
  819. menuItem.Header = "剪切";
  820. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Cut);
  821. menuItem = new MenuItem();
  822. menuItem.Name = "FreeTextPaste";
  823. menuItem.Header = "粘贴";
  824. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Paste);
  825. menuItem = new MenuItem();
  826. menuItem.Name = "FreeTextDelete";
  827. menuItem.Header = "删除";
  828. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Delete);
  829. pop.AddItem(GetSeparator());
  830. menuItem = new MenuItem();
  831. menuItem.Name = "FreeTextColor";
  832. menuItem.Header = "字体颜色";
  833. menuItem.Tag = "FontColor";
  834. pop.BindingEvent(pop.AddItem(menuItem), AnnotColorPalette_MenuCommand);
  835. menuItem = new MenuItem();
  836. menuItem.Name = "FreeTextColor";
  837. menuItem.Header = "填充颜色";
  838. menuItem.Tag = "FillColor";
  839. pop.BindingEvent(pop.AddItem(menuItem), AnnotColorPalette_MenuCommand);
  840. menuItem = new MenuItem();
  841. menuItem.Name = "FreeTextFontFamily";
  842. menuItem.Header = "字体";
  843. pop.AddItem(menuItem);
  844. var family = TextFont.GetFamily();
  845. foreach (var item in family)
  846. {
  847. RadioButton familyRdioBtn = new RadioButton();
  848. familyRdioBtn.Style = App.Current.Resources["MenuRadioBtnStyle"] as Style;
  849. familyRdioBtn.Background = new SolidColorBrush(Colors.Transparent);
  850. familyRdioBtn.Name = item.ValueStr;
  851. familyRdioBtn.Tag = item.ValueStr;
  852. familyRdioBtn.GroupName = "FontFamily";
  853. familyRdioBtn.Content = item.Content;
  854. pop.BindingEvent(pop.AddChild("FreeTextFontFamily", familyRdioBtn), FreeTextFontFamily_MenuCommand);
  855. }
  856. menuItem = new MenuItem();
  857. menuItem.Name = "FreeTextAglin";
  858. menuItem.Header = "文本对齐";
  859. pop.AddItem(menuItem);
  860. var radioButton = new RadioButton();
  861. radioButton.Style = App.Current.Resources["MenuRadioBtnStyle"] as Style;
  862. radioButton.Background = new SolidColorBrush(Colors.Transparent);
  863. radioButton.Name = "FreeTextAglinLeft";
  864. radioButton.Tag = "Left";
  865. radioButton.GroupName = "Aglin";
  866. radioButton.Content = "左对齐";
  867. pop.BindingEvent(pop.AddChild("FreeTextAglin", radioButton), FreeTextAglin_MenuCommand);
  868. radioButton = new RadioButton();
  869. radioButton.Style = App.Current.Resources["MenuRadioBtnStyle"] as Style;
  870. radioButton.Background = new SolidColorBrush(Colors.Transparent);
  871. radioButton.Name = "FreeTextAglinCenter";
  872. radioButton.Tag = "Center";
  873. radioButton.GroupName = "Aglin";
  874. radioButton.Content = "居中对齐";
  875. pop.BindingEvent(pop.AddChild("FreeTextAglin", radioButton), FreeTextAglin_MenuCommand);
  876. radioButton = new RadioButton();
  877. radioButton.Style = App.Current.Resources["MenuRadioBtnStyle"] as Style;
  878. radioButton.Background = new SolidColorBrush(Colors.Transparent);
  879. radioButton.Name = "FreeTextAglinRight";
  880. radioButton.Tag = "Right";
  881. radioButton.GroupName = "Aglin";
  882. radioButton.Content = "右对齐";
  883. pop.BindingEvent(pop.AddChild("FreeTextAglin", radioButton), FreeTextAglin_MenuCommand);
  884. radioButton = new RadioButton();
  885. radioButton.Style = App.Current.Resources["MenuRadioBtnStyle"] as Style;
  886. radioButton.Background = new SolidColorBrush(Colors.Transparent);
  887. radioButton.Name = "FreeTextAglinJustify";
  888. radioButton.Tag = "Justify";
  889. radioButton.GroupName = "Aglin";
  890. radioButton.Content = "两端对齐";
  891. pop.BindingEvent(pop.AddChild("FreeTextAglin", radioButton), FreeTextAglin_MenuCommand);
  892. menuItem = new MenuItem();
  893. menuItem.Name = "FreeHandDefault";
  894. menuItem.Header = "设置当前属性为默认值";
  895. pop.BindingEvent(pop.AddItem(menuItem), AnnotDefaultValue_MenuCommand);
  896. FreeTextAnnotPopMenu = pop;
  897. }
  898. private Separator GetSeparator()
  899. {
  900. Separator separator = new Separator();
  901. separator.Height = 1;
  902. separator.BorderBrush = new SolidColorBrush(Color.FromArgb(0x33, 0x00, 0x00, 0x00));
  903. separator.BorderThickness = new Thickness(1);
  904. return separator;
  905. }
  906. private void BindingPDFViewerHandler()
  907. {
  908. //来自PDFViewer的响应事件
  909. if (PDFViewer != null)
  910. {
  911. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  912. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  913. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  914. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  915. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  916. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  917. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  918. PDFViewer.AnnotHoverHandler += PDFViewer_AnnotHoverHandler;
  919. }
  920. }
  921. private void UnBindingPDFViewerHandler()
  922. {
  923. if (PDFViewer != null)
  924. {
  925. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  926. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  927. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  928. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  929. }
  930. }
  931. //在注释工具的状态下,右键菜单
  932. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  933. {
  934. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  935. return;
  936. switch (e.CommandType)
  937. {
  938. case CommandType.Context:
  939. if (e.AnnotEventArgsList.Count > 0)
  940. {
  941. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  942. {
  943. //绑定标记密文处右键菜单
  944. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  945. }
  946. else
  947. {
  948. if (e.AnnotEventArgsList.Count == 1)
  949. {
  950. var selectedAnnot = e.AnnotEventArgsList[0];
  951. switch (selectedAnnot.EventType)
  952. {
  953. case AnnotArgsType.AnnotFreehand:
  954. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  955. break;
  956. case AnnotArgsType.AnnotFreeText:
  957. e.PopupMenu= FreeTextAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  958. break;
  959. }
  960. }
  961. else
  962. {
  963. bool isHigh = true;//是否为高亮
  964. foreach (var item in e.AnnotEventArgsList)
  965. {
  966. if (isHightAnnot(item) == false)
  967. {
  968. isHigh = false;
  969. break;
  970. }
  971. }
  972. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  973. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  974. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  975. }
  976. }
  977. if (e.PopupMenu != null)
  978. {
  979. e.Handle = true;
  980. }
  981. }
  982. else
  983. {
  984. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  985. {
  986. if (e.PopupMenu != null)
  987. {
  988. e.Handle = true;
  989. }
  990. }
  991. else
  992. {
  993. e.PopupMenu = ViewerContextMenu(sender);
  994. if (e.PopupMenu != null)
  995. {
  996. e.Handle = true;
  997. }
  998. }
  999. }
  1000. break;
  1001. }
  1002. }
  1003. private void PDFViewer_AnnotHoverHandler(object sender, AnnotHoverData e)
  1004. {
  1005. if (e != null && e.DrawContext != null)
  1006. {
  1007. Rect hoverRect = new Rect(
  1008. e.PaintRect.Left - 2,
  1009. e.PaintRect.Top - 2,
  1010. e.PaintRect.Width + 4,
  1011. e.PaintRect.Height + 4);
  1012. Pen hoverPen = new Pen(new SolidColorBrush(Color.FromArgb(0xff, 0x11, 0x8A, 0xff)), 1);
  1013. hoverPen.DashStyle = DashStyles.Dash;
  1014. e.DrawContext?.DrawRectangle(null, hoverPen, hoverRect);
  1015. //Todo:目前ComPDFKit.Viewer没有e.Annot参数
  1016. //便签,显示ToolTip内容
  1017. //if (e.Annot != null && e.Annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  1018. //{
  1019. // if (PDFViewer.ToolTip == null)
  1020. // {
  1021. // var content = e.Annot.GetContent();
  1022. // if (string.IsNullOrEmpty(content) == false)
  1023. // {
  1024. // ToolTip TipChild = new ToolTip();
  1025. // TipChild.Style = App.Current.Resources["FlowToolTip"] as Style;
  1026. // TipChild.MaxWidth = 246;
  1027. // TipChild.Content = content;
  1028. // TipChild.Visibility = Visibility.Visible;
  1029. // TipChild.IsOpen = true;
  1030. // TipChild.Placement = PlacementMode.Right;
  1031. // var rec = e.Annot.GetRect();
  1032. // TipChild.PlacementRectangle = new Rect(rec.left, rec.top, rec.right, rec.bottom);
  1033. // TipChild.PlacementTarget = PDFViewer.Parent as ContentControl;
  1034. // TipChild.Placement = PlacementMode.MousePoint;
  1035. // PDFViewer.ToolTip = TipChild;
  1036. // oldRect = hoverRect;
  1037. // PDFViewer.MouseMove -= PDFViewer_MouseMove;
  1038. // PDFViewer.MouseMove += PDFViewer_MouseMove;
  1039. // return;
  1040. // }
  1041. // }
  1042. //}
  1043. }
  1044. CloseAnnotToolTip();
  1045. }
  1046. private Rect oldRect = new Rect(0, 0, 0, 0);
  1047. private void CloseAnnotToolTip()
  1048. {
  1049. if (PDFViewer.ToolTip != null && PDFViewer.ToolTip is ToolTip)
  1050. {
  1051. ToolTip oldTips = (ToolTip)PDFViewer.ToolTip;
  1052. oldTips.IsOpen = false;
  1053. oldTips.Visibility = Visibility.Collapsed;
  1054. PDFViewer.ToolTip = null;
  1055. PDFViewer.MouseMove -= PDFViewer_MouseMove;
  1056. oldRect = new Rect(0, 0, 0, 0);
  1057. }
  1058. }
  1059. private void PDFViewer_MouseMove(object sender, MouseEventArgs e)
  1060. {
  1061. if (sender != null && PDFViewer.ToolTip != null && PDFViewer.Parent != null)
  1062. {
  1063. var newPoint = e.GetPosition(PDFViewer.Parent as ContentControl);
  1064. var isOutw = newPoint.X > (oldRect.X + oldRect.Width + 4);
  1065. var isOuth = newPoint.Y > (oldRect.Y + oldRect.Height + 4);
  1066. if (newPoint.X < oldRect.X || newPoint.Y < oldRect.Y || isOutw || isOuth)
  1067. {
  1068. CloseAnnotToolTip();
  1069. }
  1070. }
  1071. }
  1072. private ContextMenu ViewerContextMenu(object sender)
  1073. {
  1074. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  1075. ViewerContextMenu_Loaded(contextMenu, sender);
  1076. return contextMenu;
  1077. }
  1078. private void ViewerContextMenu_Loaded(object sender, object e, Visibility visibility = Visibility.Collapsed)
  1079. {
  1080. ContextMenu contextMenu = sender as ContextMenu;
  1081. if (contextMenu.Items.Count > 0)
  1082. {
  1083. int index = PDFViewer.CurrentIndex;
  1084. //检测是否已存在相同数据
  1085. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  1086. foreach (var item in contextMenu.Items)
  1087. {
  1088. if (item is Separator separator)
  1089. {
  1090. separator.Visibility = visibility;
  1091. }
  1092. if (item is MenuItem menuItem1)
  1093. {
  1094. //if (menuItem1.Tag.ToString() == "DisplayAnnot" || menuItem1.Tag.ToString() == "HiddenAnnot")
  1095. //{
  1096. // SetMenuItemVisibility(menuItem1, "DisplayAnnot", "HiddenAnnot", isHiddenAnnot);
  1097. //}
  1098. switch (menuItem1.Tag.ToString())
  1099. {
  1100. case "Paste":
  1101. //粘贴
  1102. //if (!ApplicationCommands.Paste.CanExecute(null, (UIElement)sender))
  1103. //{
  1104. // menuItem1.IsEnabled = false;
  1105. // menuItem1.Opacity = 0.5;
  1106. //}
  1107. //else
  1108. //{
  1109. // menuItem1.IsEnabled = true;
  1110. // menuItem1.Opacity = 1;
  1111. //}
  1112. menuItem1.CommandTarget = (UIElement)e;
  1113. menuItem1.Command = ApplicationCommands.Paste;
  1114. break;
  1115. case "AddAnnotation":
  1116. menuItem1.Visibility = visibility;
  1117. break;
  1118. case "HiddenAnnot":
  1119. menuItem1.Visibility = visibility;
  1120. break;
  1121. case "DisplayAnnot":
  1122. menuItem1.Visibility = visibility;
  1123. break;
  1124. case "AddBookMark":
  1125. menuItem1.Visibility = visibility;
  1126. break;
  1127. case "DelBookMark":
  1128. menuItem1.Visibility = visibility;
  1129. break;
  1130. case "ToolMode":
  1131. menuItem1.Visibility = visibility;
  1132. break;
  1133. case "ReadModel":
  1134. menuItem1.Visibility = visibility;
  1135. break;
  1136. case "UnReadModel":
  1137. menuItem1.Visibility = visibility;
  1138. break;
  1139. case "ViewZoom":
  1140. menuItem1.Visibility = visibility;
  1141. break;
  1142. case "PageDisplay":
  1143. menuItem1.Visibility = visibility;
  1144. break;
  1145. case "Select":
  1146. menuItem1.Click -= Select_Click;
  1147. menuItem1.Click += Select_Click;
  1148. break;
  1149. case "Print":
  1150. menuItem1.Command = viewContentViewModel.PrintCommand;
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. }
  1157. private void Select_Click(object sender, RoutedEventArgs e)
  1158. {
  1159. if (viewContentViewModel.OpenBOTA == false)
  1160. {
  1161. viewContentViewModel.OpenBOTA = true;
  1162. }
  1163. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  1164. if (isBook == false)
  1165. {
  1166. bOTAContent.TabItemSearch.IsSelected = true;
  1167. }
  1168. }
  1169. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  1170. {
  1171. bool isTabItem = false;
  1172. bOTAContentViewModel = null;
  1173. bOTAContent = null;
  1174. if (regions.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  1175. {
  1176. var views = regions.Regions[viewContentViewModel.BOTARegionName].Views;
  1177. var model = views.FirstOrDefault(q => q is BOTAContent);
  1178. if (model is BOTAContent bOTAContent1)
  1179. {
  1180. bOTAContent = bOTAContent1;
  1181. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  1182. if (bOTAContentViewModel.CurrentBar == tabItemText)
  1183. {
  1184. isTabItem = true;
  1185. }
  1186. }
  1187. //foreach (var item in views)
  1188. //{
  1189. // if (item is BOTAContent bOTAContent1)
  1190. // {
  1191. // bOTAContent = bOTAContent1;
  1192. // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  1193. // if (bOTAContentViewModel.CurrentBar == tabItemText)
  1194. // {
  1195. // isTabItem = true;
  1196. // break;
  1197. // }
  1198. // }
  1199. //}
  1200. }
  1201. return isTabItem;
  1202. }
  1203. private bool isHightAnnot(AnnotHandlerEventArgs annot)
  1204. {
  1205. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  1206. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  1207. annot.EventType == AnnotArgsType.AnnotHighlight ||
  1208. annot.EventType == AnnotArgsType.AnnotStrikeout
  1209. )
  1210. {
  1211. return true;
  1212. }
  1213. else
  1214. {
  1215. return false;
  1216. }
  1217. }
  1218. //注释编辑事件
  1219. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  1220. {
  1221. if (e != null && e.Count > 0)
  1222. {
  1223. for (int i = 0; i < e.Count; i++)
  1224. {
  1225. AnnotEditEvent editEvent = e[i];
  1226. switch (editEvent.EditAction)
  1227. {
  1228. case ActionType.Add:
  1229. int pageindex = editEvent.PageIndex;
  1230. int annotindex = editEvent.AnnotIndex;
  1231. viewContentViewModel.FillAndSign.Add(new Point(pageindex, annotindex));
  1232. PDFViewer.SelectAnnotation(pageindex, annotindex);
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. //选中和非选中注释,右键菜单
  1239. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  1240. {
  1241. if (e != null)
  1242. {
  1243. var annot = e.AnnotItemsList[0];
  1244. if (annot != null)
  1245. {
  1246. if (e.AnnotItemsList.Count == 1)
  1247. {
  1248. //IsEdit = false;
  1249. //IsAnnotCreateReset:是否为创建注释的状态
  1250. if (e.IsAnnotCreateReset == false)
  1251. {
  1252. GetSelectedAnnots(e);
  1253. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  1254. //PDFViewer.SetToolParam(annot);
  1255. }
  1256. else
  1257. {
  1258. //TODO: 设计已重新调整为(仅限高亮注释):修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  1259. if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  1260. annot.EventType != AnnotArgsType.AnnotUnderline &&
  1261. annot.EventType != AnnotArgsType.AnnotHighlight &&
  1262. annot.EventType != AnnotArgsType.AnnotSquiggly &&
  1263. annot.EventType != AnnotArgsType.AnnotLink &&
  1264. annot.EventType != AnnotArgsType.AnnotFreehand &&
  1265. annot.EventType != AnnotArgsType.AnnotSticky
  1266. )
  1267. {
  1268. if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  1269. {
  1270. foreach (var item in ToolExpandDict)
  1271. {
  1272. if (item.Value == e.AnnotItemsList[0].EventType)
  1273. {
  1274. annot = null;//新建注释时,回到默认值
  1275. FindAnnotTypeKey(item.Key, ref annot);
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. }
  1281. //else
  1282. //PDFViewer.SetToolParam(annot);
  1283. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  1284. if (isRightMenuAddAnnot)
  1285. {
  1286. ShowPropertyPanel(false);
  1287. }
  1288. else
  1289. {
  1290. ShowPropertyPanel();
  1291. }
  1292. }
  1293. }
  1294. else
  1295. {
  1296. bool isDifferentAnnotTyle = false;
  1297. var lastAnnot = annot;
  1298. foreach (var item in e.AnnotItemsList)
  1299. {
  1300. if (lastAnnot.EventType != item.EventType)
  1301. {
  1302. if (isShapAnnot(annot) == true && isShapAnnot(item) == true)
  1303. {
  1304. lastAnnot = item;
  1305. continue;
  1306. }
  1307. lastAnnot = item;
  1308. isDifferentAnnotTyle = true;
  1309. break;
  1310. }
  1311. }
  1312. if (isDifferentAnnotTyle)
  1313. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1314. else
  1315. GetSelectedAnnots(e);
  1316. }
  1317. }
  1318. }
  1319. else
  1320. {
  1321. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1322. }
  1323. }
  1324. //private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  1325. //{
  1326. // if (e != null && e.Count > 0)
  1327. // {
  1328. // for (int i = 0; i < e.Count; i++)
  1329. // {
  1330. // AnnotEditEvent editEvent = e[i];
  1331. // switch (editEvent.EditAction)
  1332. // {
  1333. // case ActionType.Add:
  1334. // // bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  1335. // if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  1336. // {
  1337. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  1338. // if (viewModel != null)
  1339. // {
  1340. // int pageindex = editEvent.PageIndex;
  1341. // int annotindex = editEvent.AnnotIndex;
  1342. // viewModel.UpdateAddedAnnot(pageindex, annotindex);
  1343. // }
  1344. // }
  1345. // break;
  1346. // case ActionType.Del:
  1347. // //isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  1348. // if (isTabItemAnnotation)
  1349. // {
  1350. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  1351. // if (viewModel != null)
  1352. // {
  1353. // int pageindex = editEvent.PageIndex;
  1354. // int annotindex = editEvent.AnnotIndex;
  1355. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  1356. // }
  1357. // }
  1358. // break;
  1359. // case ActionType.Modify:
  1360. // isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  1361. // if (bOTAContent.TabItemAnnotation.IsSelected)
  1362. // {
  1363. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  1364. // if (viewModel != null)
  1365. // {
  1366. // int pageindex = editEvent.PageIndex;
  1367. // int annotindex = editEvent.AnnotIndex;
  1368. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  1369. // }
  1370. // }
  1371. // break;
  1372. // case ActionType.TextEdit:
  1373. // break;
  1374. // default:
  1375. // break;
  1376. // }
  1377. // }
  1378. // }
  1379. //}
  1380. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  1381. {
  1382. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  1383. annot.EventType == AnnotArgsType.AnnotSquare ||
  1384. annot.EventType == AnnotArgsType.AnnotLine
  1385. )
  1386. {
  1387. return true;
  1388. }
  1389. else
  1390. {
  1391. return false;
  1392. }
  1393. }
  1394. private void GetSelectedAnnots(AnnotAttribEvent e)
  1395. {
  1396. IsEdit = true;
  1397. var annot = e.AnnotItemsList[0];
  1398. switch (annot.EventType)
  1399. {
  1400. case AnnotArgsType.AnnotFreeText:
  1401. GetFreetext(e.AnnotItemsList);
  1402. break;
  1403. case AnnotArgsType.AnnotFreehand:
  1404. GetFreehand(e.AnnotItemsList);
  1405. break;
  1406. }
  1407. }
  1408. private void ShowPropertyPanel(bool show = true)
  1409. {
  1410. viewContentViewModel.IsPropertyOpen = show;
  1411. }
  1412. #region Navigation
  1413. public bool IsNavigationTarget(NavigationContext navigationContext)
  1414. {
  1415. return true;
  1416. }
  1417. public void OnNavigatedFrom(NavigationContext navigationContext)
  1418. {
  1419. UnBindingPDFViewerHandler();
  1420. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  1421. ViewerContextMenu_Loaded(contextMenu, null, Visibility.Visible);
  1422. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1423. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1424. ShowPropertyPanel(false);
  1425. }
  1426. public void OnNavigatedTo(NavigationContext navigationContext)
  1427. {
  1428. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  1429. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  1430. BindingPDFViewerHandler();
  1431. HookShapeIsCheck = true;
  1432. InitFillAndSignProperty();
  1433. InitCheckedEvent("RbtnTick");
  1434. IsEdit = false;
  1435. }
  1436. #endregion
  1437. }
  1438. }