AnnotToolContentViewModel.Function.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using Microsoft.Win32;
  5. using PDF_Office.CustomControl;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Properties;
  8. using PDFSettings;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Media;
  18. namespace PDF_Office.ViewModels.Tools
  19. {
  20. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  21. {
  22. #region 初始化
  23. private void BindingEvent()
  24. {
  25. propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  26. propertyPanel.DefaultStored += AnnotProperty_DefaultStored;
  27. }
  28. private void InitDefaultValue()
  29. {
  30. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotHighlight);
  31. if (annotProperty == null)
  32. {
  33. annotProperty = new DefaultAnnotProperty();
  34. annotProperty.AnnotToolType = AnnotArgsType.AnnotHighlight;
  35. annotProperty.BackgroundColor = Color.FromRgb(0xFF, 0xBB, 0x00);
  36. annotProperty.ForgoundColor = Color.FromRgb(0xFF, 0xBB, 0x00);
  37. annotProperty.Opacity = 0.5;
  38. SettingHelper.SetAnnotDefaultProperty(annotProperty);
  39. }
  40. else
  41. {
  42. HighLightColor = new SolidColorBrush(annotProperty.BackgroundColor);
  43. HighLightOpacity = annotProperty.Opacity;
  44. }
  45. }
  46. #endregion
  47. #region 注释工具
  48. /// <summary>
  49. /// 高亮注释
  50. /// </summary>
  51. /// <returns></returns>
  52. private AnnotHandlerEventArgs GetHighLight()
  53. {
  54. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  55. TextHighlightAnnotArgs highlightArgs = new TextHighlightAnnotArgs();
  56. highlightArgs.Color = Color.FromRgb(0xFF, 0xBB, 0x00);
  57. highlightArgs.Transparency = 0.5;
  58. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotHighlight);
  59. if (annotProperty != null)
  60. {
  61. highlightArgs.Color = annotProperty.ForgoundColor;
  62. highlightArgs.Transparency = annotProperty.Opacity;
  63. highlightArgs.Content = annotProperty.NoteText;
  64. }
  65. annotAttribsList[AnnotAttrib.Color] = highlightArgs.Color;
  66. annotAttribsList[AnnotAttrib.Transparency] = highlightArgs.Transparency;
  67. annotAttribsList[AnnotAttrib.NoteText] = string.Empty;
  68. AddToPropertyPanel(highlightArgs, "HighLight", "TextAnnotProperty");
  69. return highlightArgs;
  70. }
  71. private AnnotHandlerEventArgs GetUnderLine()
  72. {
  73. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  74. TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
  75. underlineArgs.Color = Color.FromRgb(0x12, 0xFD, 0xFF);
  76. underlineArgs.Transparency = 1;
  77. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotUnderline);
  78. if (annotProperty != null)
  79. {
  80. underlineArgs.Color = annotProperty.ForgoundColor;
  81. underlineArgs.Transparency = annotProperty.Opacity;
  82. underlineArgs.Content = annotProperty.NoteText;
  83. }
  84. annotAttribsList[AnnotAttrib.Color] = underlineArgs.Color;
  85. annotAttribsList[AnnotAttrib.Transparency] = underlineArgs.Transparency;
  86. annotAttribsList[AnnotAttrib.NoteText] = string.Empty;
  87. AddToPropertyPanel(underlineArgs, "UnderLine", "TextAnnotProperty");
  88. return underlineArgs;
  89. }
  90. private AnnotHandlerEventArgs GetSquiggly()
  91. {
  92. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  93. TextSquigglyAnnotArgs squigglyArgs = new TextSquigglyAnnotArgs();
  94. squigglyArgs.Color = Color.FromRgb(0xFF, 0x87, 0x16);
  95. squigglyArgs.Transparency = 1;
  96. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotSquiggly);
  97. if (annotProperty != null)
  98. {
  99. squigglyArgs.Color = annotProperty.ForgoundColor;
  100. squigglyArgs.Transparency = annotProperty.Opacity;
  101. squigglyArgs.Content = annotProperty.NoteText;
  102. }
  103. annotAttribsList[AnnotAttrib.Color] = squigglyArgs.Color;
  104. annotAttribsList[AnnotAttrib.Transparency] = squigglyArgs.Transparency;
  105. annotAttribsList[AnnotAttrib.NoteText] = string.Empty;
  106. AddToPropertyPanel(squigglyArgs, "Squiggly", "TextAnnotProperty");
  107. return squigglyArgs;
  108. }
  109. private AnnotHandlerEventArgs GetStrikeout()
  110. {
  111. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  112. TextStrikeoutAnnotArgs strikeoutArgs = new TextStrikeoutAnnotArgs();
  113. strikeoutArgs.Color = Color.FromRgb(0xFF, 0x3B, 0x30);
  114. strikeoutArgs.Transparency = 1;
  115. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStrikeout);
  116. if (annotProperty != null)
  117. {
  118. strikeoutArgs.Color = annotProperty.ForgoundColor;
  119. strikeoutArgs.Transparency = annotProperty.Opacity;
  120. strikeoutArgs.Content = annotProperty.NoteText;
  121. }
  122. annotAttribsList[AnnotAttrib.Color] = strikeoutArgs.Color;
  123. annotAttribsList[AnnotAttrib.Transparency] = strikeoutArgs.Transparency;
  124. annotAttribsList[AnnotAttrib.NoteText] = string.Empty;
  125. AddToPropertyPanel(strikeoutArgs, "Strikeout", "TextAnnotProperty");
  126. return strikeoutArgs;
  127. }
  128. private AnnotHandlerEventArgs GetFreehand()
  129. {
  130. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  131. FreehandAnnotArgs freehandArgs = new FreehandAnnotArgs();
  132. freehandArgs.InkColor = Color.FromRgb(0x38, 0xE0, 0x2E);
  133. freehandArgs.Transparency = 1;
  134. freehandArgs.LineWidth = 1;
  135. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreehand);
  136. if (annotProperty != null)
  137. {
  138. freehandArgs.InkColor = annotProperty.ForgoundColor;
  139. freehandArgs.Transparency = annotProperty.Opacity;
  140. freehandArgs.LineWidth = annotProperty.Thickness;
  141. freehandArgs.Content = annotProperty.NoteText;
  142. }
  143. annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  144. annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  145. annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  146. annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  147. AddToPropertyPanel(freehandArgs, "Freehand", "FreehandAnnotProperty");
  148. return freehandArgs;
  149. }
  150. private AnnotHandlerEventArgs GetFreetext()
  151. {
  152. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  153. FreeTextAnnotArgs freetextArgs = new FreeTextAnnotArgs();
  154. freetextArgs.Align = TextAlignment.Left;
  155. freetextArgs.BgColor = Colors.Transparent;
  156. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  157. freetextArgs.FontColor = Colors.Black;
  158. freetextArgs.FontSize = 14;
  159. freetextArgs.Transparency = 1;
  160. freetextArgs.LineColor = Colors.Black;
  161. freetextArgs.LineWidth = 0;
  162. freetextArgs.TextContent = string.Empty;
  163. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  164. if (annotProperty != null)
  165. {
  166. freetextArgs.Align = annotProperty.TextAlign;
  167. freetextArgs.BgColor = annotProperty.BackgroundColor;
  168. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  169. freetextArgs.FontColor = annotProperty.ForgoundColor;
  170. freetextArgs.FontSize = annotProperty.FontSize;
  171. freetextArgs.Transparency = annotProperty.Opacity;
  172. freetextArgs.LineColor = annotProperty.BorderColor;
  173. freetextArgs.LineWidth = annotProperty.Thickness;
  174. freetextArgs.TextContent = annotProperty.NoteText;
  175. freetextArgs.FontWeight = annotProperty.FontWeight;
  176. freetextArgs.FontStyle = annotProperty.FontStyle;
  177. }
  178. int align = Settings.Default.AppProperties.Annotate.TextAlign;
  179. TextAlignment textAlignment;
  180. if (align == 0)
  181. textAlignment = TextAlignment.Left;
  182. else if (align == 1)
  183. textAlignment = TextAlignment.Center;
  184. else
  185. textAlignment = TextAlignment.Right;
  186. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  187. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  188. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  189. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  190. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  191. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  192. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  193. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  194. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  195. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  196. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  197. AddToPropertyPanel(freetextArgs, "Freetext", "FreetextAnnotProperty");
  198. return freetextArgs;
  199. }
  200. private AnnotHandlerEventArgs GetStickyNote()
  201. {
  202. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  203. StickyAnnotArgs stickyAnnotArgs = new StickyAnnotArgs();
  204. stickyAnnotArgs.Color = Color.FromRgb(0xFF, 0x81, 0x33);
  205. stickyAnnotArgs.StickyNote = string.Empty;
  206. stickyAnnotArgs.Transparency = 1;
  207. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotSticky);
  208. if (annotProperty != null)
  209. {
  210. stickyAnnotArgs.Color = annotProperty.ForgoundColor;
  211. stickyAnnotArgs.StickyNote = annotProperty.NoteText;
  212. stickyAnnotArgs.Transparency = annotProperty.Opacity;
  213. }
  214. annotAttribsList[AnnotAttrib.Color] = stickyAnnotArgs.Color;
  215. annotAttribsList[AnnotAttrib.Transparency] = stickyAnnotArgs.Transparency;
  216. annotAttribsList[AnnotAttrib.NoteText] = stickyAnnotArgs.StickyNote;
  217. AddToPropertyPanel(stickyAnnotArgs, "StickyNote", "FreetextAnnotProperty");
  218. return stickyAnnotArgs;
  219. }
  220. private AnnotHandlerEventArgs GetRect()
  221. {
  222. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  223. SquareAnnotArgs squareArgs = new SquareAnnotArgs();
  224. squareArgs.LineColor = Colors.Red;
  225. squareArgs.BgColor = Colors.Transparent;
  226. squareArgs.LineWidth = 1;
  227. squareArgs.Transparency = 1;
  228. squareArgs.LineDash = DashStyles.Solid;
  229. squareArgs.Content = string.Empty;
  230. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotSquare);
  231. if (annotProperty != null)
  232. {
  233. squareArgs.LineColor = annotProperty.BorderColor;
  234. squareArgs.BgColor = annotProperty.BackgroundColor;
  235. if (annotProperty.DashArray == null || annotProperty.DashArray.Count == 0)
  236. {
  237. squareArgs.LineDash = DashStyles.Solid;
  238. }
  239. else
  240. {
  241. DashStyle dash = new DashStyle();
  242. foreach (var offset in annotProperty.DashArray)
  243. {
  244. dash.Dashes.Add(offset);
  245. }
  246. squareArgs.LineDash = dash;
  247. }
  248. squareArgs.LineWidth = annotProperty.Thickness;
  249. squareArgs.Transparency = annotProperty.Opacity;
  250. squareArgs.Content = annotProperty.NoteText;
  251. }
  252. annotAttribsList[AnnotAttrib.Color] = squareArgs.LineColor;
  253. annotAttribsList[AnnotAttrib.FillColor] = squareArgs.BgColor;
  254. annotAttribsList[AnnotAttrib.LineStyle] = squareArgs.LineDash;
  255. annotAttribsList[AnnotAttrib.Thickness] = squareArgs.LineWidth;
  256. annotAttribsList[AnnotAttrib.Transparency] = squareArgs.Transparency;
  257. annotAttribsList[AnnotAttrib.NoteText] = squareArgs.Content;
  258. AddToPropertyPanel(squareArgs, "Rect", "SharpsAnnotProperty");
  259. return squareArgs;
  260. }
  261. private AnnotHandlerEventArgs GetCircle()
  262. {
  263. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  264. CircleAnnotArgs circleAnnotArgs = new CircleAnnotArgs();
  265. circleAnnotArgs.LineColor = Colors.Red;
  266. circleAnnotArgs.BgColor = Colors.Transparent;
  267. circleAnnotArgs.LineWidth = 1;
  268. circleAnnotArgs.Transparency = 1;
  269. circleAnnotArgs.LineDash = DashStyles.Solid;
  270. circleAnnotArgs.Content = string.Empty;
  271. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotCircle);
  272. if (annotProperty != null)
  273. {
  274. circleAnnotArgs.LineColor = annotProperty.BorderColor;
  275. circleAnnotArgs.BgColor = annotProperty.BackgroundColor;
  276. if (annotProperty.DashArray == null || annotProperty.DashArray.Count == 0)
  277. {
  278. circleAnnotArgs.LineDash = DashStyles.Solid;
  279. }
  280. else
  281. {
  282. DashStyle dash = new DashStyle();
  283. foreach (var offset in annotProperty.DashArray)
  284. {
  285. dash.Dashes.Add(offset);
  286. }
  287. circleAnnotArgs.LineDash = dash;
  288. }
  289. circleAnnotArgs.LineWidth = annotProperty.Thickness;
  290. circleAnnotArgs.Transparency = annotProperty.Opacity;
  291. circleAnnotArgs.Content = annotProperty.NoteText;
  292. }
  293. annotAttribsList[AnnotAttrib.Color] = circleAnnotArgs.LineColor;
  294. annotAttribsList[AnnotAttrib.FillColor] = circleAnnotArgs.BgColor;
  295. annotAttribsList[AnnotAttrib.LineStyle] = circleAnnotArgs.LineDash;
  296. annotAttribsList[AnnotAttrib.Thickness] = circleAnnotArgs.LineWidth;
  297. annotAttribsList[AnnotAttrib.Transparency] = circleAnnotArgs.Transparency;
  298. annotAttribsList[AnnotAttrib.NoteText] = circleAnnotArgs.Content;
  299. AddToPropertyPanel(circleAnnotArgs, "Circle", "SharpsAnnotProperty");
  300. return circleAnnotArgs;
  301. }
  302. private AnnotHandlerEventArgs GetArrowLine(string TagStr)
  303. {
  304. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  305. LineAnnotArgs lineArgs = new LineAnnotArgs();
  306. lineArgs.LineColor = Colors.Red;
  307. lineArgs.HeadLineType = C_LINE_TYPE.LINETYPE_NONE;
  308. if (TagStr == "Line")
  309. {
  310. lineArgs.TailLineType = C_LINE_TYPE.LINETYPE_NONE;
  311. }
  312. else
  313. {
  314. lineArgs.TailLineType = C_LINE_TYPE.LINETYPE_ARROW;
  315. }
  316. lineArgs.LineDash = DashStyles.Solid;
  317. lineArgs.LineWidth = 1;
  318. lineArgs.Transparency = 1;
  319. lineArgs.Content = string.Empty;
  320. DefaultAnnotProperty annotProperty = null;
  321. if (TagStr == "Line")
  322. {
  323. annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotLine, "Line");
  324. if (annotProperty != null)
  325. {
  326. annotProperty.HeadLineType = C_LINE_TYPE.LINETYPE_NONE;
  327. annotProperty.TailLineType = C_LINE_TYPE.LINETYPE_NONE;
  328. }
  329. }
  330. else
  331. {
  332. annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotLine, "Arrow");
  333. }
  334. if (annotProperty != null)
  335. {
  336. lineArgs.LineColor = annotProperty.BorderColor;
  337. lineArgs.HeadLineType = annotProperty.HeadLineType;
  338. lineArgs.TailLineType = annotProperty.TailLineType;
  339. DashStyle dash = new DashStyle();
  340. foreach (var offset in annotProperty.DashArray)
  341. {
  342. dash.Dashes.Add(offset);
  343. }
  344. lineArgs.LineDash = dash;
  345. lineArgs.LineWidth = annotProperty.Thickness;
  346. lineArgs.Transparency = annotProperty.Opacity;
  347. lineArgs.Content = annotProperty.NoteText;
  348. }
  349. annotAttribsList[AnnotAttrib.Color] = lineArgs.LineColor;
  350. annotAttribsList[AnnotAttrib.LineStart] = lineArgs.HeadLineType;
  351. annotAttribsList[AnnotAttrib.LineEnd] = lineArgs.TailLineType;
  352. annotAttribsList[AnnotAttrib.LineStyle] = lineArgs.LineDash;
  353. annotAttribsList[AnnotAttrib.Thickness] = lineArgs.LineWidth;
  354. annotAttribsList[AnnotAttrib.Transparency] = lineArgs.Transparency;
  355. annotAttribsList[AnnotAttrib.NoteText] = lineArgs.Content;
  356. AddToPropertyPanel(lineArgs, TagStr, "SharpsAnnotProperty");
  357. return lineArgs;
  358. }
  359. private AnnotHandlerEventArgs GetStamp()
  360. {
  361. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  362. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  363. stampAnnotArgs.Opacity = 1;
  364. stampAnnotArgs.StampText = "APPROVED";
  365. stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  366. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  367. AddToPropertyPanel(stampAnnotArgs, null, "StampAnnotProperty");
  368. return stampAnnotArgs;
  369. }
  370. private AnnotHandlerEventArgs GetImage(CustomIconToggleBtn annotBtn)
  371. {
  372. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  373. annotBtn.IsChecked = false;
  374. StampAnnotArgs stampArgs = new StampAnnotArgs();
  375. stampArgs.Opacity = 1;
  376. stampArgs.Type = StampType.IMAGE_STAMP;
  377. OpenFileDialog openFileDialog = new OpenFileDialog();
  378. openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
  379. if (openFileDialog.ShowDialog() == true)
  380. {
  381. stampArgs.ImagePath = openFileDialog.FileName;
  382. }
  383. AddToPropertyPanel(stampArgs, null, "ImageAnnotProperty");
  384. return stampArgs;
  385. }
  386. private void AddToPropertyPanel(AnnotHandlerEventArgs annot, string toolTag, string viewContent)
  387. {
  388. if (string.IsNullOrEmpty(toolTag) == false)
  389. {
  390. if (!ToolExpandDict.ContainsKey("HighLight"))
  391. {
  392. ToolExpandDict["HighLight"] = true;
  393. }
  394. }
  395. if (annot != null)
  396. propertyPanel.annot = annot;
  397. if (string.IsNullOrEmpty(viewContent) == false)
  398. {
  399. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  400. }
  401. }
  402. #endregion
  403. }
  404. }