SignatureAnnotPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.AnnotPanel;
  7. using PDF_Office.Properties;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Ink;
  21. using System.Windows.Media.Imaging;
  22. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  23. {
  24. class SignatureAnnotPropertyViewModel : BindableBase, INavigationAware
  25. {
  26. private CPDFViewer PDFViewer;
  27. private IDialogService dialogs;
  28. public DelegateCommand ShowDialogCommand { get; set; }
  29. private Signature currentSignature=null;
  30. private int currentListboxIndex = -1;
  31. private ViewContentViewModel viewContentViewModel;
  32. public int CurrentListboxIndex
  33. {
  34. get { return currentListboxIndex; }
  35. set
  36. {
  37. SetProperty(ref currentListboxIndex, value);
  38. }
  39. }
  40. public ObservableCollection<Signature> SignatureList { get; set; }
  41. public SignatureAnnotPropertyViewModel(IDialogService dialogService)
  42. {
  43. dialogs = dialogService;
  44. ShowDialogCommand = new DelegateCommand(ShowDialog);
  45. SignatureList = new ObservableCollection<Signature>();
  46. LoadSettings();
  47. }
  48. /// <summary>
  49. /// 加载缓存的签名
  50. /// </summary>
  51. private void LoadSettings()
  52. {
  53. PDFSettings.SignatureList items = Settings.Default.SignatureList;
  54. if (items != null)
  55. {
  56. for (int i = 0; i < items.Count; i++)
  57. {
  58. Signature customStamp = new Signature();
  59. customStamp.SourcePath = items[i].SourcePath;
  60. customStamp.DrawingPath = items[i].DrawingPath;
  61. customStamp.inkThickness= items[i].inkThickness;
  62. customStamp.inkColor= items[i].inkColor;
  63. customStamp.Type = (SignatureType)items[i].Type;
  64. SignatureList.Add(customStamp);
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 应用签名
  70. /// </summary>
  71. public void SetSignature(Signature signature)
  72. {
  73. switch (signature.Type)
  74. {
  75. case SignatureType.TextType:
  76. case SignatureType.ImageType:
  77. {
  78. StampAnnotArgs stampArgs = new StampAnnotArgs();
  79. stampArgs.Opacity = 1;
  80. stampArgs.Type = StampType.IMAGE_STAMP;
  81. stampArgs.ImagePath = signature.SourcePath;
  82. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  83. PDFViewer.SetToolParam(stampArgs);
  84. currentSignature = signature;
  85. }
  86. break;
  87. case SignatureType.Drawing:
  88. {
  89. StampAnnotArgs stampArgs = new StampAnnotArgs();
  90. stampArgs.SetInkData(GetPoints(signature.DrawingPath), signature.inkThickness, signature.inkColor);
  91. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  92. PDFViewer.SetToolParam(stampArgs);
  93. currentSignature = signature;
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. private List<List<Point>> GetPoints(string Path)
  101. {
  102. StrokeCollection Strokes;
  103. List<List<Point>> RawPointList = new List<List<Point>>();
  104. using (FileStream strokeStream = File.OpenRead(Path))
  105. {
  106. Strokes = new StrokeCollection(strokeStream);
  107. }
  108. for (int kk = 0; kk < Strokes.Count; kk++)
  109. {
  110. List<Point> p = new List<Point>();
  111. RawPointList.Add(p);
  112. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  113. {
  114. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  115. if (point.X >= 0 && point.Y >= 0)
  116. RawPointList[kk].Add(point);
  117. }
  118. }
  119. return RawPointList;
  120. }
  121. private void ShowDialog()
  122. {
  123. bool result = true;
  124. DialogParameters value = new DialogParameters();
  125. value.Add(ParameterNames.PDFViewer, PDFViewer);
  126. dialogs.ShowDialog(DialogNames.SignatureCreateDialog, value, e =>
  127. {
  128. if (e.Result != ButtonResult.OK)
  129. {
  130. result = false;
  131. }
  132. SignatureCreateDialogViewModel DialogVM = e.Parameters.GetValue<SignatureCreateDialogViewModel>(ParameterNames.DataModel);
  133. if (DialogVM != null)
  134. {
  135. CreateSignature(DialogVM);
  136. }
  137. });
  138. if (!result)
  139. {
  140. return;
  141. }
  142. }
  143. /// <summary>
  144. /// 创建签名,并保存到APP缓存
  145. /// </summary>
  146. /// <param name="viewModel"></param>
  147. private void CreateSignature(SignatureCreateDialogViewModel viewModel)
  148. {
  149. Signature Signature = new Signature();
  150. if (viewModel.IsRemoveBackground)
  151. {
  152. if (!string.IsNullOrEmpty(viewModel.SaveToPath))
  153. {
  154. App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
  155. }
  156. Signature.SourcePath = viewModel.RemoveBackgroundSaveToPath;
  157. }
  158. else
  159. {
  160. Signature.SourcePath = viewModel.SaveToPath;
  161. }
  162. Signature.DrawingPath = viewModel.DrawingSaveToPath;
  163. Signature.Type = (SignatureType)viewModel.TabItemIndex;
  164. Signature.inkThickness = viewModel.DrawingAttributeObject.Width;
  165. Signature.inkColor = viewModel.DrawingAttributeObject.Color;
  166. SignatureList.Add(Signature);
  167. //缓存数据
  168. PDFSettings.SignatureList stamps = Settings.Default.SignatureList;
  169. if (stamps == null)
  170. {
  171. stamps = Settings.Default.SignatureList = new PDFSettings.SignatureList();
  172. }
  173. PDFSettings.SignatureItem signature = new PDFSettings.SignatureItem();
  174. signature.DrawingPath = Signature.DrawingPath;
  175. signature.SourcePath = Signature.SourcePath;
  176. signature.inkThickness = Signature.inkThickness;
  177. signature.inkColor = Signature.inkColor;
  178. signature.Type = (PDFSettings.SignatureType)Signature.Type;
  179. stamps.Add(signature);
  180. Settings.Default.Save();
  181. SetSignature(Signature);
  182. CurrentListboxIndex = SignatureList.Count - 1;
  183. }
  184. public void Close()
  185. {
  186. PDFViewer.SetMouseMode(MouseModes.PanTool);
  187. viewContentViewModel.IsPropertyOpen = false;
  188. }
  189. /// <summary>
  190. /// 导出签名
  191. /// </summary>
  192. public void SaveToPath(string FileType, Signature Item)
  193. {
  194. SaveFileDialog saveFileDialog = new SaveFileDialog();
  195. saveFileDialog.Title = "保存" + FileType + "文件";
  196. saveFileDialog.Filter = "(*)|*." + FileType;
  197. if (saveFileDialog.ShowDialog() == false)
  198. {
  199. return;
  200. }
  201. BitmapEncoder encoder;
  202. if (FileType.ToUpper() == "JPG")
  203. {
  204. //如果要导出JPG,并且要白色背景,https://social.msdn.microsoft.com/Forums/en-US/2d574827-7eb7-488e-a86b-861ccb4f0275/set-image-background-white-and-save-it?forum=wpf
  205. encoder = new JpegBitmapEncoder();
  206. }
  207. else
  208. {
  209. encoder = new PngBitmapEncoder();
  210. }
  211. encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
  212. string path = saveFileDialog.FileName;
  213. switch (FileType.ToUpper())
  214. {
  215. case "PDF":
  216. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  217. BitmapFrame frame = BitmapFrame.Create(new Uri(Item.SourcePath));
  218. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  219. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  220. newDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  221. newDoc.WriteToFilePath(path);
  222. break;
  223. case "PNG":
  224. using (FileStream stream = new FileStream(path, FileMode.Create))
  225. {
  226. encoder.Save(stream);
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
  233. }
  234. public void DeleteSignature(Signature Item)
  235. {
  236. if (currentSignature.Equals(Item))
  237. {
  238. PDFViewer.SetMouseMode(MouseModes.PanTool);
  239. }
  240. if (SignatureList.IndexOf(currentSignature)<0)
  241. {
  242. PDFViewer.SetMouseMode(MouseModes.PanTool);
  243. }
  244. int index = SignatureList.IndexOf(Item);
  245. App.CachePath.AddToDeleteFiles(Item.SourcePath);
  246. if (!string.IsNullOrEmpty(Item.DrawingPath))
  247. {
  248. App.CachePath.AddToDeleteFiles(Item.DrawingPath);
  249. }
  250. SignatureList.RemoveAt(index);
  251. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  252. signature.RemoveAt(index);
  253. Settings.Default.Save();
  254. }
  255. public void DeleteAll()
  256. {
  257. foreach (Signature item in SignatureList)
  258. {
  259. App.CachePath.AddToDeleteFiles(item.SourcePath);
  260. if (!string.IsNullOrEmpty(item.DrawingPath))
  261. {
  262. App.CachePath.AddToDeleteFiles(item.DrawingPath);
  263. }
  264. }
  265. SignatureList.Clear();
  266. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  267. signature.Clear();
  268. Settings.Default.Save();
  269. PDFViewer.SetMouseMode(MouseModes.PanTool);
  270. currentSignature = null;
  271. }
  272. public bool IsNavigationTarget(NavigationContext navigationContext)
  273. {
  274. return true;
  275. }
  276. public void OnNavigatedFrom(NavigationContext navigationContext)
  277. {
  278. return;
  279. }
  280. public void OnNavigatedTo(NavigationContext navigationContext)
  281. {
  282. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  283. if (PDFViewer == null)
  284. {
  285. return;
  286. }
  287. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  288. }
  289. }
  290. }