SignatureAnnotPropertyViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.AnnotPanel;
  7. using PDF_Master.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_Master.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. if (File.Exists(items[i].SourcePath))
  65. {
  66. SignatureList.Add(customStamp);
  67. }
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 应用签名
  73. /// </summary>
  74. public void SetSignature(Signature signature)
  75. {
  76. switch (signature.Type)
  77. {
  78. case SignatureType.TextType:
  79. case SignatureType.ImageType:
  80. {
  81. StampAnnotArgs stampArgs = new StampAnnotArgs();
  82. stampArgs.Opacity = 1;
  83. stampArgs.Type = StampType.IMAGE_STAMP;
  84. stampArgs.ImagePath = signature.SourcePath;
  85. //stampArgs.ImageHeight = 140;
  86. //stampArgs.ImageWidth = 140;
  87. stampArgs.MaxHeight = 140;
  88. stampArgs.MaxWidth = 140;
  89. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  90. PDFViewer.SetToolParam(stampArgs);
  91. currentSignature = signature;
  92. }
  93. break;
  94. case SignatureType.Drawing:
  95. {
  96. StampAnnotArgs stampArgs = new StampAnnotArgs();
  97. stampArgs.SetInkData(GetPoints(signature.DrawingPath), signature.inkThickness, signature.inkColor);
  98. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  99. PDFViewer.SetToolParam(stampArgs);
  100. currentSignature = signature;
  101. }
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. private List<List<Point>> GetPoints(string Path)
  108. {
  109. StrokeCollection Strokes;
  110. List<List<Point>> RawPointList = new List<List<Point>>();
  111. using (FileStream strokeStream = File.OpenRead(Path))
  112. {
  113. Strokes = new StrokeCollection(strokeStream);
  114. }
  115. for (int kk = 0; kk < Strokes.Count; kk++)
  116. {
  117. List<Point> p = new List<Point>();
  118. RawPointList.Add(p);
  119. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  120. {
  121. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  122. if (point.X >= 0 && point.Y >= 0)
  123. RawPointList[kk].Add(point);
  124. }
  125. }
  126. return RawPointList;
  127. }
  128. private void ShowDialog()
  129. {
  130. bool result = true;
  131. DialogParameters value = new DialogParameters();
  132. value.Add(ParameterNames.PDFViewer, PDFViewer);
  133. dialogs.ShowDialog(DialogNames.SignatureCreateDialog, value, e =>
  134. {
  135. if (e.Result != ButtonResult.OK)
  136. {
  137. result = false;
  138. }
  139. SignatureCreateDialogViewModel DialogVM = e.Parameters.GetValue<SignatureCreateDialogViewModel>(ParameterNames.DataModel);
  140. if (DialogVM != null)
  141. {
  142. CreateSignature(DialogVM);
  143. }
  144. });
  145. if (!result)
  146. {
  147. return;
  148. }
  149. }
  150. /// <summary>
  151. /// 创建签名,并保存到APP缓存
  152. /// </summary>
  153. /// <param name="viewModel"></param>
  154. private void CreateSignature(SignatureCreateDialogViewModel viewModel)
  155. {
  156. Signature Signature = new Signature();
  157. if (viewModel.IsRemoveBackground)
  158. {
  159. if (!string.IsNullOrEmpty(viewModel.SaveToPath))
  160. {
  161. App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
  162. }
  163. Signature.SourcePath = viewModel.RemoveBackgroundSaveToPath;
  164. }
  165. else
  166. {
  167. Signature.SourcePath = viewModel.SaveToPath;
  168. }
  169. Signature.DrawingPath = viewModel.DrawingSaveToPath;
  170. Signature.Type = (SignatureType)viewModel.TabItemIndex;
  171. //根据当前选项设置粗细
  172. double inkThickness;
  173. if (viewModel.ThicknessListIndex >= 0)
  174. {
  175. inkThickness = Convert.ToDouble(viewModel.ThicknessList[viewModel.ThicknessListIndex].Substring(0, 3));
  176. }
  177. else
  178. {
  179. inkThickness = viewModel.DrawingAttributeObject.Width;
  180. }
  181. Signature.inkThickness = inkThickness;
  182. Signature.inkColor = viewModel.DrawingAttributeObject.Color;
  183. SignatureList.Add(Signature);
  184. //缓存数据
  185. PDFSettings.SignatureList stamps = Settings.Default.SignatureList;
  186. if (stamps == null)
  187. {
  188. stamps = Settings.Default.SignatureList = new PDFSettings.SignatureList();
  189. }
  190. PDFSettings.SignatureItem signature = new PDFSettings.SignatureItem();
  191. signature.DrawingPath = Signature.DrawingPath;
  192. signature.SourcePath = Signature.SourcePath;
  193. signature.inkThickness = Signature.inkThickness;
  194. signature.inkColor = Signature.inkColor;
  195. signature.Type = (PDFSettings.SignatureType)Signature.Type;
  196. stamps.Add(signature);
  197. Settings.Default.Save();
  198. SetSignature(Signature);
  199. CurrentListboxIndex = SignatureList.Count - 1;
  200. }
  201. public void Close()
  202. {
  203. //PDFViewer.SetMouseMode(MouseModes.PanTool);
  204. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  205. var annotArgs = new SelectToolArgs();
  206. PDFViewer.SetToolParam(annotArgs);
  207. //PDFViewer.SetMouseMode(MouseModes.PanTool);
  208. PDFViewer.DisableClearSelectAnnot(false);
  209. PDFViewer.ClearSelectAnnots();
  210. viewContentViewModel.IsPropertyOpen = false;
  211. }
  212. /// <summary>
  213. /// 导出签名
  214. /// </summary>
  215. public void SaveToPath(string FileType, Signature Item)
  216. {
  217. SaveFileDialog saveFileDialog = new SaveFileDialog();
  218. saveFileDialog.Title = "保存" + FileType + "文件";
  219. saveFileDialog.Filter = "(*)|*." + FileType;
  220. if (saveFileDialog.ShowDialog() == false)
  221. {
  222. return;
  223. }
  224. BitmapEncoder encoder;
  225. if (FileType.ToUpper() == "JPG")
  226. {
  227. //如果要导出JPG,并且要白色背景,https://social.msdn.microsoft.com/Forums/en-US/2d574827-7eb7-488e-a86b-861ccb4f0275/set-image-background-white-and-save-it?forum=wpf
  228. encoder = new JpegBitmapEncoder();
  229. }
  230. else
  231. {
  232. encoder = new PngBitmapEncoder();
  233. }
  234. encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
  235. string path = saveFileDialog.FileName;
  236. switch (FileType.ToUpper())
  237. {
  238. case "PDF":
  239. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  240. BitmapFrame frame = BitmapFrame.Create(new Uri(Item.SourcePath));
  241. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  242. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  243. newDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  244. newDoc.WriteToFilePath(path);
  245. break;
  246. case "PNG":
  247. using (FileStream stream = new FileStream(path, FileMode.Create))
  248. {
  249. encoder.Save(stream);
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
  256. }
  257. public void DeleteSignature(Signature Item)
  258. {
  259. if (currentSignature.Equals(Item))
  260. {
  261. PDFViewer.SetMouseMode(MouseModes.PanTool);
  262. }
  263. if (SignatureList.IndexOf(currentSignature) < 0)
  264. {
  265. PDFViewer.SetMouseMode(MouseModes.PanTool);
  266. }
  267. int index = SignatureList.IndexOf(Item);
  268. App.CachePath.AddToDeleteFiles(Item.SourcePath);
  269. if (!string.IsNullOrEmpty(Item.DrawingPath))
  270. {
  271. App.CachePath.AddToDeleteFiles(Item.DrawingPath);
  272. }
  273. SignatureList.RemoveAt(index);
  274. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  275. signature.RemoveAt(index);
  276. Settings.Default.Save();
  277. }
  278. public void DeleteAll()
  279. {
  280. foreach (Signature item in SignatureList)
  281. {
  282. App.CachePath.AddToDeleteFiles(item.SourcePath);
  283. if (!string.IsNullOrEmpty(item.DrawingPath))
  284. {
  285. App.CachePath.AddToDeleteFiles(item.DrawingPath);
  286. }
  287. }
  288. SignatureList.Clear();
  289. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  290. signature.Clear();
  291. Settings.Default.Save();
  292. PDFViewer.SetMouseMode(MouseModes.PanTool);
  293. currentSignature = null;
  294. }
  295. public bool IsNavigationTarget(NavigationContext navigationContext)
  296. {
  297. return true;
  298. }
  299. public void OnNavigatedFrom(NavigationContext navigationContext)
  300. {
  301. return;
  302. }
  303. public void OnNavigatedTo(NavigationContext navigationContext)
  304. {
  305. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  306. if (PDFViewer == null)
  307. {
  308. return;
  309. }
  310. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  311. }
  312. }
  313. }