SnapshotEditMenuViewModel.cs 14 KB

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