SnapshotEditMenuViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Media;
  11. using System.Windows;
  12. using System.Windows.Media.Imaging;
  13. using System.Drawing;
  14. using static Dropbox.Api.Sharing.MemberAction;
  15. using System.IO;
  16. using PDF_Master.Helper;
  17. using Microsoft.Win32;
  18. using System.Windows.Interop;
  19. using PDF_Master.CustomControl;
  20. using System.Windows.Controls;
  21. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  22. using ComPDFKit.PDFPage;
  23. using System.Drawing.Printing;
  24. using System.Security.Policy;
  25. using Microsoft.AppCenter.Utils.Files;
  26. using File = System.IO.File;
  27. using Directory = System.IO.Directory;
  28. using System.Drawing.Imaging;
  29. using Microsoft.Office.Interop.Word;
  30. using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  31. using Prism.Regions;
  32. using System.Windows.Input;
  33. using Microsoft.Office.Interop.Excel;
  34. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  35. using Prism.Services.Dialogs;
  36. namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
  37. {
  38. public class SnapshotEditMenuViewModel : BindableBase
  39. {
  40. public CustomIconToggleBtn ToggleBtn { get; set; }
  41. public SnapshotEditToolArgs SnapToolArgs { get; set; }
  42. public CPDFViewer PDFViewer { get; set; }
  43. private CPDFViewer saveToPDFViewer = new CPDFViewer();
  44. public event EventHandler<KeyValuePair<string, object>> SnapToolEvent;
  45. public DelegateCommand SnapCopyCommand { get; set; }
  46. public DelegateCommand ExportPNGCommand { get; set; }
  47. public DelegateCommand ExportJPGCommand { get; set; }
  48. public DelegateCommand ExportPDFCommand { get; set; }
  49. public DelegateCommand CroppingCommand { get; set; }
  50. public DelegateCommand PrintCommand { get; set; }
  51. public SnapshotEditMenuViewModel()
  52. {
  53. SnapCopyCommand = new DelegateCommand(CopyEvent);
  54. ExportPNGCommand = new DelegateCommand(ExportPNGEvent);
  55. ExportJPGCommand = new DelegateCommand(ExportJPGEvent);
  56. ExportPDFCommand = new DelegateCommand(ExportPDFEvent);
  57. CroppingCommand = new DelegateCommand(CroppingEvent);
  58. PrintCommand = new DelegateCommand(PrintEvent);
  59. }
  60. private void PrintEvent()
  61. {
  62. if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
  63. {
  64. try
  65. {
  66. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  67. if (saveBitmap != null)
  68. {
  69. PrintDialog printDlg = new PrintDialog();
  70. if (printDlg.ShowDialog() == true)
  71. {
  72. DrawingVisual visualItem = new DrawingVisual();
  73. DrawingContext drawContext = visualItem.RenderOpen();
  74. drawContext.DrawImage(saveBitmap, new Rect(0, 0, saveBitmap.Width, saveBitmap.Height));
  75. drawContext.Close();
  76. printDlg.PrintVisual(visualItem, "Snapshot");
  77. }
  78. }
  79. PDFViewer.RemoveTool(false);
  80. PDFViewer.SetMouseMode(MouseModes.PanTool);
  81. if (SnapToolEvent != null)
  82. {
  83. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  84. SnapToolEvent.Invoke(this, param);
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. }
  90. }
  91. }
  92. private void CroppingEvent()
  93. {
  94. if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
  95. {
  96. Rect rect = SnapToolArgs.GetSnapshotPDFRect(out int CurrentIndex);
  97. List<int> cropPageList = new List<int>();
  98. cropPageList.Add(CurrentIndex);
  99. PDFViewer?.CropPage(0, rect, cropPageList);
  100. PDFViewer.GoToPage(CurrentIndex);
  101. PDFViewer.UndoManager.CanSave = true;
  102. PDFViewer.SetMouseMode(MouseModes.PanTool);
  103. if (SnapToolEvent != null)
  104. {
  105. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  106. SnapToolEvent.Invoke(this, param);
  107. }
  108. }
  109. }
  110. private void ExportPDFEvent()
  111. {
  112. SnapshotEditExport("PDF");
  113. }
  114. private void ExportJPGEvent()
  115. {
  116. SnapshotEditExport("JPG");
  117. }
  118. private void ExportPNGEvent()
  119. {
  120. SnapshotEditExport("PNG");
  121. }
  122. private void SnapshotEditExport(string type)
  123. {
  124. if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
  125. {
  126. SaveFileDialog dlg = new SaveFileDialog();
  127. switch (type)
  128. {
  129. case "PNG":
  130. dlg.Filter = "PNG|*.png";
  131. break;
  132. case "JPG":
  133. dlg.Filter = "JPG|*.jpg";
  134. break;
  135. case "PDF":
  136. dlg.Filter = "PDF|*.pdf";
  137. break;
  138. }
  139. dlg.FileName = PDFViewer.Document.FileName;
  140. if (dlg.FileName == null || dlg.FileName.Trim().Length == 0)
  141. {
  142. dlg.FileName = "Blank" + DateTime.Now.ToString("yyyyMMddHHmmss");
  143. }
  144. if (dlg.ShowDialog() == true)
  145. {
  146. string fileName = dlg.FileName;
  147. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  148. if (saveBitmap != null)
  149. {
  150. if (dlg.SafeFileName.ToLower().EndsWith(".jpg"))
  151. {
  152. Stream saveStream = dlg.OpenFile();
  153. JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
  154. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  155. jpgEncoder.Frames.Add(frame);
  156. jpgEncoder.Save(saveStream);
  157. saveStream.Dispose();
  158. //导出后打开对应文件夹
  159. CommonHelper.ExplorerFile(dlg.FileName);
  160. }
  161. else if (dlg.SafeFileName.ToLower().EndsWith(".png"))
  162. {
  163. Stream saveStream = dlg.OpenFile();
  164. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  165. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  166. pngEncoder.Frames.Add(frame);
  167. pngEncoder.Save(saveStream);
  168. saveStream.Dispose();
  169. //导出后打开对应文件夹
  170. CommonHelper.ExplorerFile(dlg.FileName);
  171. }
  172. else if (dlg.SafeFileName.ToLower().EndsWith(".pdf"))
  173. {
  174. //Stream saveStream = dlg.OpenFile();
  175. string imagePath = SaveImage(saveBitmap);
  176. if (CreateFile(imagePath))
  177. {
  178. bool result = saveToPDFViewer.Document.WriteToFilePath(dlg.FileName);
  179. }
  180. //saveStream.Dispose();
  181. //导出后打开对应文件夹
  182. CommonHelper.ExplorerFile(dlg.FileName);
  183. }
  184. }
  185. if (PDFViewer != null && PDFViewer.ToolManager != null)
  186. {
  187. PDFViewer.RemoveTool(false);
  188. PDFViewer.SetMouseMode(MouseModes.PanTool);
  189. if (SnapToolEvent != null)
  190. {
  191. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  192. SnapToolEvent.Invoke(this, param);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// 创建文件,路径为空时表示创建空白文档
  200. /// 否则表示从图片路径创建PDf
  201. /// </summary>
  202. /// <param name="imagePath"></param>
  203. /// <returns></returns>
  204. public bool CreateFile(string imagePath = null)
  205. {
  206. string fileName = null;
  207. saveToPDFViewer.CreateDocument();
  208. if (saveToPDFViewer.Document == null)
  209. {
  210. AlertsMessage alertsMessage = new AlertsMessage();
  211. alertsMessage.ShowDialog("", "创建文件失败.", "OK");
  212. return false;
  213. }
  214. if (string.IsNullOrEmpty(imagePath))
  215. {
  216. fileName = "Blank Page.pdf";
  217. //默认插入595*842 大小的页面
  218. saveToPDFViewer.Document.InsertPage(0, 595, 842, null);
  219. }
  220. else
  221. {
  222. fileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("\\") - 1) + ".pdf";
  223. Bitmap pic = new Bitmap(imagePath);
  224. int width = pic.Size.Width;
  225. int height = pic.Size.Height;
  226. string ex = System.IO.Path.GetExtension(imagePath);
  227. //其他格式或者名称中含空格的图片 在本地先转存一下
  228. if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
  229. {
  230. //将其他格式图片转换成jpg
  231. string folderPath = Path.GetTempPath();
  232. if (File.Exists(folderPath))
  233. {
  234. File.Delete(folderPath);
  235. }
  236. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  237. if (!tempfolder.Exists)
  238. {
  239. tempfolder.Create();
  240. }
  241. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
  242. imagePath = targetPath;
  243. pic.Save(targetPath, ImageFormat.Jpeg);
  244. }
  245. pic.Dispose();
  246. var result = saveToPDFViewer.Document.InsertPage(0, width, height, imagePath);
  247. if (!result)
  248. {
  249. AlertsMessage alertsMessage = new AlertsMessage();
  250. alertsMessage.ShowDialog("", "创建文件失败.", "OK");
  251. return false;
  252. }
  253. }
  254. return true;
  255. }
  256. /// <summary>
  257. /// 保存WriteableBitmap图像
  258. /// </summary>
  259. /// <param name="wtbBmp"></param>
  260. private string SaveImage(WriteableBitmap wtbBmp)
  261. {
  262. string isSave = null;
  263. if (wtbBmp == null)
  264. {
  265. return isSave;
  266. }
  267. try
  268. {
  269. DirectoryInfo tempfolder = new DirectoryInfo(Path.Combine(App.CurrentPath, "Temp"));
  270. string strpath = tempfolder + DateTime.Now.ToString("yyyyMMddfff") + ".jpg";
  271. if (!File.Exists(tempfolder.FullName))
  272. {
  273. Directory.CreateDirectory(tempfolder.FullName);
  274. }
  275. using (FileStream stream = new FileStream(strpath, FileMode.Create))
  276. {
  277. JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder();
  278. bitmapEncoder.Frames.Add(BitmapFrame.Create(wtbBmp));
  279. bitmapEncoder.Save(stream);
  280. isSave = strpath;
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. System.Diagnostics.Debug.WriteLine(ex.ToString());
  286. isSave = null;
  287. }
  288. return isSave;
  289. }
  290. /// <summary>
  291. /// 复制
  292. /// </summary>
  293. private void CopyEvent()
  294. {
  295. if (SnapToolArgs != null)
  296. {
  297. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  298. if (saveBitmap != null)
  299. {
  300. Bitmap bitmap = BitmapFromWriteableBitmap(saveBitmap);
  301. Clipboard.SetImage(ChangeBitmapToBitmapSource(bitmap));
  302. PDFViewer.RemoveTool(false);
  303. PDFViewer.SetMouseMode(MouseModes.PanTool);
  304. if (SnapToolEvent != null)
  305. {
  306. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  307. SnapToolEvent.Invoke(this, param);
  308. }
  309. }
  310. }
  311. }
  312. private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
  313. {
  314. System.Drawing.Bitmap bmp;
  315. using (MemoryStream outStream = new MemoryStream())
  316. {
  317. BitmapEncoder enc = new BmpBitmapEncoder();
  318. enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
  319. enc.Save(outStream);
  320. bmp = new System.Drawing.Bitmap(outStream);
  321. }
  322. return bmp;
  323. }
  324. /// <summary>
  325. /// 从Bitmap转换成BitmapSource
  326. /// </summary>
  327. /// <param name="bmp"></param>
  328. /// <returns></returns>
  329. public BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
  330. {
  331. BitmapSource returnSource;
  332. try
  333. {
  334. returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  335. }
  336. catch
  337. {
  338. returnSource = null;
  339. }
  340. return returnSource;
  341. }
  342. }
  343. }