SnapshotEditMenuViewModel.cs 16 KB

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