SnapshotEditMenuViewModel.cs 14 KB

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