SnapshotEditMenuViewModel.cs 15 KB

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