SnapshotEditMenuViewModel.cs 14 KB

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