SnapshotEditMenuViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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
  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. dlg.FileName = "Untitle";
  147. if (dlg.FileName == null || dlg.FileName.Trim().Length == 0)
  148. {
  149. dlg.FileName = "Blank" + DateTime.Now.ToString("yyyyMMddHHmmss");
  150. }
  151. if (dlg.ShowDialog() == true)
  152. {
  153. string fileName = dlg.FileName;
  154. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  155. if (saveBitmap != null)
  156. {
  157. if (dlg.SafeFileName.ToLower().EndsWith(".jpg"))
  158. {
  159. Stream saveStream = dlg.OpenFile();
  160. JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
  161. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  162. jpgEncoder.Frames.Add(frame);
  163. jpgEncoder.Save(saveStream);
  164. saveStream.Dispose();
  165. //导出后打开对应文件夹
  166. CommonHelper.ExplorerFile(dlg.FileName);
  167. }
  168. else if (dlg.SafeFileName.ToLower().EndsWith(".png"))
  169. {
  170. Stream saveStream = dlg.OpenFile();
  171. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  172. BitmapFrame frame = BitmapFrame.Create(saveBitmap);
  173. pngEncoder.Frames.Add(frame);
  174. pngEncoder.Save(saveStream);
  175. saveStream.Dispose();
  176. //导出后打开对应文件夹
  177. CommonHelper.ExplorerFile(dlg.FileName);
  178. }
  179. else if (dlg.SafeFileName.ToLower().EndsWith(".pdf"))
  180. {
  181. //Stream saveStream = dlg.OpenFile();
  182. //方法一
  183. //string imagePath = SaveImage(saveBitmap);
  184. //if (CreateFile(imagePath))
  185. //{
  186. // bool result = saveToPDFViewer.Document.WriteToFilePath(dlg.FileName);
  187. //}
  188. try
  189. {
  190. saveToPDFViewer = new CPDFViewer();
  191. saveToPDFViewer.CreateDocument();
  192. if (saveToPDFViewer.Document == null)
  193. {
  194. AlertsMessage alertsMessage = new AlertsMessage();
  195. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  196. return;
  197. }
  198. byte[] buffer = new byte[saveBitmap.PixelWidth * saveBitmap.PixelHeight * 4];
  199. saveBitmap.CopyPixels(buffer, saveBitmap.PixelWidth * 4, 0);
  200. saveToPDFViewer.Document.InsertPage(0, saveBitmap.PixelWidth, saveBitmap.PixelHeight, buffer, CPDFDocumentImageMode.CPDFDocumentImageModeScaleAspectFit);
  201. var result = saveToPDFViewer.Document.WriteToFilePath(dlg.FileName);
  202. if (result)
  203. {
  204. }
  205. }
  206. catch
  207. {
  208. }
  209. //saveStream.Dispose();
  210. //导出后打开对应文件夹
  211. CommonHelper.ExplorerFile(dlg.FileName);
  212. }
  213. }
  214. if (PDFViewer != null && PDFViewer.ToolManager != null)
  215. {
  216. PDFViewer.RemoveTool(false);
  217. PDFViewer.SetMouseMode(MouseModes.PanTool);
  218. if (SnapToolEvent != null)
  219. {
  220. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  221. SnapToolEvent.Invoke(this, param);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 创建文件,路径为空时表示创建空白文档
  229. /// 否则表示从图片路径创建PDf
  230. /// </summary>
  231. /// <param name="imagePath"></param>
  232. /// <returns></returns>
  233. public bool CreateFile(string imagePath = null)
  234. {
  235. string fileName = null;
  236. saveToPDFViewer.CreateDocument();
  237. if (saveToPDFViewer.Document == null)
  238. {
  239. AlertsMessage alertsMessage = new AlertsMessage();
  240. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  241. return false;
  242. }
  243. if (string.IsNullOrEmpty(imagePath))
  244. {
  245. fileName = "Blank Page.pdf";
  246. //默认插入595*842 大小的页面
  247. saveToPDFViewer.Document.InsertPage(0, 595, 842, null);
  248. }
  249. else
  250. {
  251. fileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("\\") - 1) + ".pdf";
  252. Bitmap pic = new Bitmap(imagePath);
  253. int width = pic.Size.Width;
  254. int height = pic.Size.Height;
  255. string ex = System.IO.Path.GetExtension(imagePath);
  256. //其他格式或者名称中含空格的图片 在本地先转存一下
  257. if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
  258. {
  259. //将其他格式图片转换成jpg
  260. string folderPath = Path.GetTempPath();
  261. if (File.Exists(folderPath))
  262. {
  263. File.Delete(folderPath);
  264. }
  265. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  266. if (!tempfolder.Exists)
  267. {
  268. tempfolder.Create();
  269. }
  270. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
  271. imagePath = targetPath;
  272. pic.Save(targetPath, ImageFormat.Jpeg);
  273. }
  274. pic.Dispose();
  275. var result = saveToPDFViewer.Document.InsertPage(0, width, height, imagePath);
  276. if (!result)
  277. {
  278. AlertsMessage alertsMessage = new AlertsMessage();
  279. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. /// <summary>
  286. /// 保存WriteableBitmap图像
  287. /// </summary>
  288. /// <param name="wtbBmp"></param>
  289. private string SaveImage(WriteableBitmap wtbBmp)
  290. {
  291. string isSave = null;
  292. if (wtbBmp == null)
  293. {
  294. return isSave;
  295. }
  296. try
  297. {
  298. DirectoryInfo tempfolder = new DirectoryInfo(Path.Combine(App.CurrentPath, "Temp"));
  299. string strpath = Path.Combine(tempfolder.FullName, DateTime.Now.ToString("yyyyMMddfff") + ".jpg");
  300. if (!File.Exists(tempfolder.FullName))
  301. {
  302. Directory.CreateDirectory(tempfolder.FullName);
  303. }
  304. using (FileStream stream = new FileStream(strpath, FileMode.Create))
  305. {
  306. JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder();
  307. bitmapEncoder.Frames.Add(BitmapFrame.Create(wtbBmp));
  308. bitmapEncoder.Save(stream);
  309. isSave = strpath;
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. System.Diagnostics.Debug.WriteLine(ex.ToString());
  315. isSave = null;
  316. }
  317. return isSave;
  318. }
  319. /// <summary>
  320. /// 复制
  321. /// </summary>
  322. private void CopyEvent()
  323. {
  324. if (SnapToolArgs != null)
  325. {
  326. WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
  327. if (saveBitmap != null)
  328. {
  329. PDFViewer.CloseContextMenu();
  330. Bitmap bitmap = BitmapFromWriteableBitmap(saveBitmap);
  331. Clipboard.SetImage(ChangeBitmapToBitmapSource(bitmap));
  332. PDFViewer.RemoveTool(false);
  333. PDFViewer.SetMouseMode(MouseModes.PanTool);
  334. if (SnapToolEvent != null)
  335. {
  336. KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
  337. SnapToolEvent.Invoke(this, param);
  338. }
  339. }
  340. }
  341. }
  342. private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
  343. {
  344. System.Drawing.Bitmap bmp;
  345. using (MemoryStream outStream = new MemoryStream())
  346. {
  347. BitmapEncoder enc = new BmpBitmapEncoder();
  348. enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
  349. enc.Save(outStream);
  350. bmp = new System.Drawing.Bitmap(outStream);
  351. }
  352. return bmp;
  353. }
  354. /// <summary>
  355. /// 从Bitmap转换成BitmapSource
  356. /// </summary>
  357. /// <param name="bmp"></param>
  358. /// <returns></returns>
  359. public BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
  360. {
  361. BitmapSource returnSource;
  362. try
  363. {
  364. returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  365. }
  366. catch
  367. {
  368. returnSource = null;
  369. }
  370. return returnSource;
  371. }
  372. }
  373. }