SnapshotEditMenuViewModel.cs 14 KB

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