AnnotToolContentViewModel.cs 26 KB

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