SignatureAnnotPropertyViewModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. public ObservableCollection<Signature> SignatureList { get; set; }
  30. public SignatureAnnotPropertyViewModel(IDialogService dialogService)
  31. {
  32. dialogs = dialogService;
  33. ShowDialogCommand = new DelegateCommand(ShowDialog);
  34. SignatureList = new ObservableCollection<Signature>();
  35. LoadSettings();
  36. }
  37. /// <summary>
  38. /// 加载缓存的签名
  39. /// </summary>
  40. private void LoadSettings()
  41. {
  42. PDFSettings.SignatureList items = Settings.Default.SignatureList;
  43. if (items != null)
  44. {
  45. for (int i = 0; i < items.Count; i++)
  46. {
  47. Signature customStamp = new Signature();
  48. customStamp.SourcePath = items[i].SourcePath;
  49. customStamp.DrawingPath = items[i].DrawingPath;
  50. customStamp.inkThickness= items[i].inkThickness;
  51. customStamp.inkColor= items[i].inkColor;
  52. customStamp.Type = (SignatureType)items[i].Type;
  53. SignatureList.Add(customStamp);
  54. }
  55. }
  56. }
  57. public void SetSignature(Signature signature)
  58. {
  59. switch (signature.Type)
  60. {
  61. case SignatureType.TextType:
  62. case SignatureType.ImageType:
  63. {
  64. StampAnnotArgs stampArgs = new StampAnnotArgs();
  65. stampArgs.Opacity = 1;
  66. stampArgs.Type = StampType.IMAGE_STAMP;
  67. stampArgs.ImagePath = signature.SourcePath;
  68. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  69. PDFViewer.SetToolParam(stampArgs);
  70. }
  71. break;
  72. case SignatureType.Drawing:
  73. {
  74. StampAnnotArgs stampArgs = new StampAnnotArgs();
  75. stampArgs.SetInkData(GetPoints(signature.DrawingPath), signature.inkThickness, signature.inkColor);
  76. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  77. PDFViewer.SetToolParam(stampArgs);
  78. }
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. private List<List<Point>> GetPoints(string Path)
  85. {
  86. StrokeCollection Strokes;
  87. List<List<Point>> RawPointList = new List<List<Point>>();
  88. using (FileStream strokeStream = File.OpenRead(Path))
  89. {
  90. Strokes = new StrokeCollection(strokeStream);
  91. }
  92. for (int kk = 0; kk < Strokes.Count; kk++)
  93. {
  94. List<Point> p = new List<Point>();
  95. RawPointList.Add(p);
  96. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  97. {
  98. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  99. if (point.X >= 0 && point.Y >= 0)
  100. RawPointList[kk].Add(point);
  101. }
  102. }
  103. return RawPointList;
  104. }
  105. private void ShowDialog()
  106. {
  107. bool result = true;
  108. DialogParameters value = new DialogParameters();
  109. value.Add(ParameterNames.PDFViewer, PDFViewer);
  110. dialogs.ShowDialog(DialogNames.SignatureCreateDialog, value, e =>
  111. {
  112. if (e.Result != ButtonResult.OK)
  113. {
  114. result = false;
  115. }
  116. SignatureCreateDialogViewModel DialogVM = e.Parameters.GetValue<SignatureCreateDialogViewModel>(ParameterNames.DataModel);
  117. if (DialogVM != null)
  118. {
  119. CreateSignature(DialogVM);
  120. }
  121. });
  122. if (!result)
  123. {
  124. return;
  125. }
  126. }
  127. /// <summary>
  128. /// 创建签名,并保存到APP缓存
  129. /// </summary>
  130. /// <param name="viewModel"></param>
  131. private void CreateSignature(SignatureCreateDialogViewModel viewModel)
  132. {
  133. Signature Signature = new Signature();
  134. if (viewModel.IsRemoveBackground)
  135. {
  136. if (!string.IsNullOrEmpty(viewModel.SaveToPath))
  137. {
  138. App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
  139. }
  140. Signature.SourcePath = viewModel.RemoveBackgroundSaveToPath;
  141. }
  142. else
  143. {
  144. Signature.SourcePath = viewModel.SaveToPath;
  145. }
  146. Signature.DrawingPath = viewModel.DrawingSaveToPath;
  147. Signature.Type = (SignatureType)viewModel.TabItemIndex;
  148. Signature.inkThickness = viewModel.DrawingAttributeObject.Width;
  149. Signature.inkColor = viewModel.DrawingAttributeObject.Color;
  150. SignatureList.Add(Signature);
  151. //缓存数据
  152. PDFSettings.SignatureList stamps = Settings.Default.SignatureList;
  153. if (stamps == null)
  154. {
  155. stamps = Settings.Default.SignatureList = new PDFSettings.SignatureList();
  156. }
  157. PDFSettings.SignatureItem signature = new PDFSettings.SignatureItem();
  158. signature.DrawingPath = Signature.DrawingPath;
  159. signature.SourcePath = Signature.SourcePath;
  160. signature.inkThickness = Signature.inkThickness;
  161. signature.inkColor = Signature.inkColor;
  162. signature.Type = (PDFSettings.SignatureType)Signature.Type;
  163. stamps.Add(signature);
  164. Settings.Default.Save();
  165. }
  166. /// <summary>
  167. /// 导出签名
  168. /// </summary>
  169. public void SaveToPath(string FileType, Signature Item)
  170. {
  171. SaveFileDialog saveFileDialog = new SaveFileDialog();
  172. saveFileDialog.Title = "保存" + FileType + "文件";
  173. saveFileDialog.Filter = "(*)|*." + FileType;
  174. if (saveFileDialog.ShowDialog() == false)
  175. {
  176. return;
  177. }
  178. BitmapEncoder encoder;
  179. if (FileType.ToUpper() == "JPG")
  180. {
  181. encoder = new JpegBitmapEncoder();
  182. }
  183. else
  184. {
  185. encoder = new PngBitmapEncoder();
  186. }
  187. encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
  188. string path = saveFileDialog.FileName;
  189. switch (FileType.ToUpper())
  190. {
  191. case "PDF":
  192. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  193. BitmapFrame frame = BitmapFrame.Create(new Uri(Item.SourcePath));
  194. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  195. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  196. newDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  197. newDoc.WriteToFilePath(path);
  198. break;
  199. case "PNG":
  200. using (FileStream stream = new FileStream(path, FileMode.Create))
  201. {
  202. encoder.Save(stream);
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
  209. }
  210. public void DeleteSignature(Signature Item)
  211. {
  212. int index = SignatureList.IndexOf(Item);
  213. App.CachePath.AddToDeleteFiles(Item.SourcePath);
  214. if (!string.IsNullOrEmpty(Item.DrawingPath))
  215. {
  216. App.CachePath.AddToDeleteFiles(Item.DrawingPath);
  217. }
  218. SignatureList.RemoveAt(index);
  219. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  220. signature.RemoveAt(index);
  221. Settings.Default.Save();
  222. }
  223. public void DeleteAll()
  224. {
  225. foreach (Signature item in SignatureList)
  226. {
  227. App.CachePath.AddToDeleteFiles(item.SourcePath);
  228. if (!string.IsNullOrEmpty(item.DrawingPath))
  229. {
  230. App.CachePath.AddToDeleteFiles(item.DrawingPath);
  231. }
  232. }
  233. SignatureList.Clear();
  234. PDFSettings.SignatureList signature = Settings.Default.SignatureList;
  235. signature.Clear();
  236. Settings.Default.Save();
  237. }
  238. public bool IsNavigationTarget(NavigationContext navigationContext)
  239. {
  240. return true;
  241. }
  242. public void OnNavigatedFrom(NavigationContext navigationContext)
  243. {
  244. return;
  245. }
  246. public void OnNavigatedTo(NavigationContext navigationContext)
  247. {
  248. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  249. if (PDFViewer == null)
  250. {
  251. return;
  252. }
  253. }
  254. }
  255. }