SnapshotEditMenuViewModel.cs 13 KB

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