SignatureAnnotPropertyViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Ink;
  20. using System.Windows.Media.Imaging;
  21. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  22. {
  23. class SignatureAnnotPropertyViewModel : BindableBase, INavigationAware
  24. {
  25. private CPDFViewer PDFViewer;
  26. private IDialogService dialogs;
  27. public DelegateCommand ShowDialogCommand { get; set; }
  28. public ObservableCollection<Signature> SignatureList { get; set; }
  29. public SignatureAnnotPropertyViewModel(IDialogService dialogService)
  30. {
  31. dialogs = dialogService;
  32. ShowDialogCommand = new DelegateCommand(ShowDialog);
  33. SignatureList = new ObservableCollection<Signature>();
  34. }
  35. public void SetSignature(Signature signature)
  36. {
  37. switch (signature.Type)
  38. {
  39. case SignatureType.TextType:
  40. case SignatureType.ImageType:
  41. {
  42. StampAnnotArgs stampArgs = new StampAnnotArgs();
  43. stampArgs.Opacity = 1;
  44. stampArgs.Type = StampType.IMAGE_STAMP;
  45. stampArgs.ImagePath = signature.SourcePath;
  46. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  47. PDFViewer.SetToolParam(stampArgs);
  48. }
  49. break;
  50. case SignatureType.Drawing:
  51. {
  52. StampAnnotArgs stampArgs = new StampAnnotArgs();
  53. stampArgs.SetInkData(GetPoints(signature.DrawingPath), signature.DrawingAttributesObject.Width, signature.DrawingAttributesObject.Color);
  54. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  55. PDFViewer.SetToolParam(stampArgs);
  56. }
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. private List<List<Point>> GetPoints(string Path)
  63. {
  64. StrokeCollection Strokes;
  65. List<List<Point>> RawPointList = new List<List<Point>>();
  66. using (FileStream strokeStream = File.OpenRead(Path))
  67. {
  68. Strokes = new StrokeCollection(strokeStream);
  69. }
  70. for (int kk = 0; kk < Strokes.Count; kk++)
  71. {
  72. List<Point> p = new List<Point>();
  73. RawPointList.Add(p);
  74. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  75. {
  76. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  77. if (point.X >= 0 && point.Y >= 0)
  78. RawPointList[kk].Add(point);
  79. }
  80. }
  81. return RawPointList;
  82. }
  83. private void ShowDialog()
  84. {
  85. bool result = true;
  86. DialogParameters value = new DialogParameters();
  87. value.Add(ParameterNames.PDFViewer, PDFViewer);
  88. dialogs.ShowDialog(DialogNames.SignatureCreateDialog, value, e =>
  89. {
  90. if (e.Result != ButtonResult.OK)
  91. {
  92. result = false;
  93. }
  94. SignatureCreateDialogViewModel DialogVM = e.Parameters.GetValue<SignatureCreateDialogViewModel>(ParameterNames.DataModel);
  95. if (DialogVM != null)
  96. {
  97. CreateSignature(DialogVM);
  98. }
  99. });
  100. if (!result)
  101. {
  102. return;
  103. }
  104. }
  105. /// <summary>
  106. /// 创建签名,并保存到APP缓存
  107. /// </summary>
  108. /// <param name="viewModel"></param>
  109. private void CreateSignature(SignatureCreateDialogViewModel viewModel)
  110. {
  111. Signature Signature = new Signature();
  112. if (viewModel.IsRemoveBackground)
  113. {
  114. if (!string.IsNullOrEmpty(viewModel.SaveToPath))
  115. {
  116. App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
  117. }
  118. Signature.SourcePath = viewModel.RemoveBackgroundSaveToPath;
  119. }
  120. else
  121. {
  122. Signature.SourcePath = viewModel.SaveToPath;
  123. }
  124. Signature.DrawingPath = viewModel.DrawingSaveToPath;
  125. Signature.Type = (SignatureType)viewModel.TabItemIndex;
  126. Signature.DrawingAttributesObject = viewModel.DrawingAttributeObject;
  127. SignatureList.Add(Signature);
  128. }
  129. /// <summary>
  130. /// 导出签名
  131. /// </summary>
  132. public void SaveToPath(string FileType, Signature Item)
  133. {
  134. SaveFileDialog saveFileDialog = new SaveFileDialog();
  135. saveFileDialog.Title = "保存" + FileType + "文件";
  136. saveFileDialog.Filter = "(*)|*." + FileType;
  137. if (saveFileDialog.ShowDialog() == false)
  138. {
  139. return;
  140. }
  141. BitmapEncoder encoder;
  142. if (FileType.ToUpper() == "JPG")
  143. {
  144. encoder = new JpegBitmapEncoder();
  145. }
  146. else
  147. {
  148. encoder = new PngBitmapEncoder();
  149. }
  150. encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
  151. string path = saveFileDialog.FileName;
  152. switch (FileType.ToUpper())
  153. {
  154. case "PDF":
  155. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  156. BitmapFrame frame = BitmapFrame.Create(new Uri(Item.SourcePath));
  157. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  158. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  159. newDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  160. newDoc.WriteToFilePath(path);
  161. break;
  162. case "PNG":
  163. using (FileStream stream = new FileStream(path, FileMode.Create))
  164. {
  165. encoder.Save(stream);
  166. }
  167. break;
  168. default:
  169. break;
  170. }
  171. System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
  172. }
  173. public void DeleteSignature(Signature Item)
  174. {
  175. int index = SignatureList.IndexOf(Item);
  176. App.CachePath.AddToDeleteFiles(Item.SourcePath);
  177. if (!string.IsNullOrEmpty(Item.DrawingPath))
  178. {
  179. App.CachePath.AddToDeleteFiles(Item.DrawingPath);
  180. }
  181. SignatureList.RemoveAt(index);
  182. }
  183. public void DeleteAll()
  184. {
  185. foreach (Signature item in SignatureList)
  186. {
  187. App.CachePath.AddToDeleteFiles(item.SourcePath);
  188. if (!string.IsNullOrEmpty(item.DrawingPath))
  189. {
  190. App.CachePath.AddToDeleteFiles(item.DrawingPath);
  191. }
  192. }
  193. SignatureList.Clear();
  194. }
  195. public bool IsNavigationTarget(NavigationContext navigationContext)
  196. {
  197. return true;
  198. }
  199. public void OnNavigatedFrom(NavigationContext navigationContext)
  200. {
  201. return;
  202. }
  203. public void OnNavigatedTo(NavigationContext navigationContext)
  204. {
  205. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  206. if (PDFViewer == null)
  207. {
  208. return;
  209. }
  210. }
  211. }
  212. }