123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Prism.Commands;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.Drawing;
- using static Dropbox.Api.Sharing.MemberAction;
- using System.IO;
- using PDF_Master.Helper;
- using Microsoft.Win32;
- using System.Windows.Interop;
- using PDF_Master.CustomControl;
- using System.Windows.Controls;
- using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
- using ComPDFKit.PDFPage;
- using System.Drawing.Printing;
- using System.Security.Policy;
- using Microsoft.AppCenter.Utils.Files;
- using File = System.IO.File;
- using Directory = System.IO.Directory;
- using System.Drawing.Imaging;
- using Microsoft.Office.Interop.Word;
- using static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
- using Prism.Regions;
- using System.Windows.Input;
- namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
- {
- public class SnapshotEditMenuViewModel : BindableBase
- {
- public CustomIconToggleBtn ToggleBtn { get; set; }
- public SnapshotEditToolArgs SnapToolArgs { get; set; }
- public CPDFViewer PDFViewer { get; set; }
- private CPDFViewer saveToPDFViewer = new CPDFViewer();
- public event EventHandler<KeyValuePair<string, object>> SnapToolEvent;
- public DelegateCommand SnapCopyCommand { get; set; }
- public DelegateCommand ExportPNGCommand { get; set; }
- public DelegateCommand ExportJPGCommand { get; set; }
- public DelegateCommand ExportPDFCommand { get; set; }
- public DelegateCommand CroppingCommand { get; set; }
- public DelegateCommand PrintCommand { get; set; }
- public SnapshotEditMenuViewModel()
- {
- SnapCopyCommand = new DelegateCommand(CopyEvent);
- ExportPNGCommand = new DelegateCommand(ExportPNGEvent);
- ExportJPGCommand = new DelegateCommand(ExportJPGEvent);
- ExportPDFCommand = new DelegateCommand(ExportPDFEvent);
- CroppingCommand = new DelegateCommand(CroppingEvent);
- PrintCommand = new DelegateCommand(PrintEvent);
- }
- private void PrintEvent()
- {
- if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
- {
- try
- {
- WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
- if (saveBitmap != null)
- {
- PrintDialog printDlg = new PrintDialog();
- if (printDlg.ShowDialog() == true)
- {
- DrawingVisual visualItem = new DrawingVisual();
- DrawingContext drawContext = visualItem.RenderOpen();
- drawContext.DrawImage(saveBitmap, new Rect(0, 0, saveBitmap.Width, saveBitmap.Height));
- drawContext.Close();
- printDlg.PrintVisual(visualItem, "Snapshot");
- }
- }
- PDFViewer.RemoveTool(false);
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- if (SnapToolEvent != null)
- {
- KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
- SnapToolEvent.Invoke(this, param);
- }
- }
- catch (Exception ex)
- {
- }
- }
- }
- private void CroppingEvent()
- {
- if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
- {
- Rect rect = SnapToolArgs.GetSnapshotPDFRect(out int CurrentIndex);
- List<int> cropPageList = new List<int>();
- cropPageList.Add(CurrentIndex);
- PDFViewer?.CropPage(0, rect, cropPageList);
- PDFViewer.GoToPage(CurrentIndex);
- PDFViewer.UndoManager.CanSave = true;
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- if (SnapToolEvent != null)
- {
- KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
- SnapToolEvent.Invoke(this, param);
- }
- }
- }
- private void ExportPDFEvent()
- {
- SnapshotEditExport("PDF");
- }
- private void ExportJPGEvent()
- {
- SnapshotEditExport("JPG");
- }
- private void ExportPNGEvent()
- {
- SnapshotEditExport("PNG");
- }
- private void SnapshotEditExport(string type)
- {
- if (SnapToolArgs != null && PDFViewer != null && PDFViewer.ToolManager != null)
- {
- SaveFileDialog dlg = new SaveFileDialog();
- switch (type)
- {
- case "PNG":
- dlg.Filter = "PNG|*.png";
- break;
- case "JPG":
- dlg.Filter = "JPG|*.jpg";
- break;
- case "PDF":
- dlg.Filter = "PDF|*.pdf";
- break;
- }
- dlg.FileName = PDFViewer.Document.FileName;
- if (dlg.FileName == null || dlg.FileName.Trim().Length == 0)
- {
- dlg.FileName = "Blank" + DateTime.Now.ToString("yyyyMMddHHmmss");
- }
- if (dlg.ShowDialog() == true)
- {
- string fileName = dlg.FileName;
- WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
- if (saveBitmap != null)
- {
- if (dlg.SafeFileName.ToLower().EndsWith(".jpg"))
- {
- Stream saveStream = dlg.OpenFile();
- JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
- BitmapFrame frame = BitmapFrame.Create(saveBitmap);
- jpgEncoder.Frames.Add(frame);
- jpgEncoder.Save(saveStream);
- saveStream.Dispose();
- //导出后打开对应文件夹
- CommonHelper.ExplorerFile(dlg.FileName);
- }
- else if (dlg.SafeFileName.ToLower().EndsWith(".png"))
- {
- Stream saveStream = dlg.OpenFile();
- PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
- BitmapFrame frame = BitmapFrame.Create(saveBitmap);
- pngEncoder.Frames.Add(frame);
- pngEncoder.Save(saveStream);
- saveStream.Dispose();
- //导出后打开对应文件夹
- CommonHelper.ExplorerFile(dlg.FileName);
- }
- else if (dlg.SafeFileName.ToLower().EndsWith(".pdf"))
- {
- //Stream saveStream = dlg.OpenFile();
- string imagePath = SaveImage(saveBitmap);
- if (CreateFile(imagePath))
- {
- bool result = saveToPDFViewer.Document.WriteToFilePath(dlg.FileName);
- }
- //saveStream.Dispose();
- //导出后打开对应文件夹
- CommonHelper.ExplorerFile(dlg.FileName);
- }
- }
- if (PDFViewer != null && PDFViewer.ToolManager != null)
- {
- PDFViewer.RemoveTool(false);
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- if (SnapToolEvent != null)
- {
- KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
- SnapToolEvent.Invoke(this, param);
- }
- }
- }
- }
- }
- /// <summary>
- /// 创建文件,路径为空时表示创建空白文档
- /// 否则表示从图片路径创建PDf
- /// </summary>
- /// <param name="imagePath"></param>
- /// <returns></returns>
- public bool CreateFile(string imagePath = null)
- {
- string fileName = null;
- saveToPDFViewer.CreateDocument();
- if (saveToPDFViewer.Document == null)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "创建文件失败.", "OK");
- return false;
- }
- if (string.IsNullOrEmpty(imagePath))
- {
- fileName = "Blank Page.pdf";
- //默认插入595*842 大小的页面
- saveToPDFViewer.Document.InsertPage(0, 595, 842, null);
- }
- else
- {
- fileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1, imagePath.LastIndexOf(".") - imagePath.LastIndexOf("\\") - 1) + ".pdf";
- Bitmap pic = new Bitmap(imagePath);
- int width = pic.Size.Width;
- int height = pic.Size.Height;
- string ex = System.IO.Path.GetExtension(imagePath);
- //其他格式或者名称中含空格的图片 在本地先转存一下
- if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(imagePath, UriKind.Absolute))
- {
- //将其他格式图片转换成jpg
- string folderPath = Path.GetTempPath();
- if (File.Exists(folderPath))
- {
- File.Delete(folderPath);
- }
- DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
- if (!tempfolder.Exists)
- {
- tempfolder.Create();
- }
- string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
- imagePath = targetPath;
- pic.Save(targetPath, ImageFormat.Jpeg);
- }
- pic.Dispose();
- var result = saveToPDFViewer.Document.InsertPage(0, width, height, imagePath);
- if (!result)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "创建文件失败.", "OK");
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 保存WriteableBitmap图像
- /// </summary>
- /// <param name="wtbBmp"></param>
- private string SaveImage(WriteableBitmap wtbBmp)
- {
- string isSave = null;
- if (wtbBmp == null)
- {
- return isSave;
- }
- try
- {
- DirectoryInfo tempfolder = new DirectoryInfo(Path.Combine(App.CurrentPath, "Temp"));
- string strpath = tempfolder + DateTime.Now.ToString("yyyyMMddfff") + ".jpg";
- if (!File.Exists(tempfolder.FullName))
- {
- Directory.CreateDirectory(tempfolder.FullName);
- }
- using (FileStream stream = new FileStream(strpath, FileMode.Create))
- {
- JpegBitmapEncoder bitmapEncoder = new JpegBitmapEncoder();
- bitmapEncoder.Frames.Add(BitmapFrame.Create(wtbBmp));
- bitmapEncoder.Save(stream);
- isSave = strpath;
- }
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine(ex.ToString());
- isSave = null;
- }
- return isSave;
- }
- /// <summary>
- /// 复制
- /// </summary>
- private void CopyEvent()
- {
- if (SnapToolArgs != null)
- {
- WriteableBitmap saveBitmap = SnapToolArgs.GetSnapshotImage();
- if (saveBitmap != null)
- {
- Bitmap bitmap = BitmapFromWriteableBitmap(saveBitmap);
- Clipboard.SetImage(ChangeBitmapToBitmapSource(bitmap));
- PDFViewer.RemoveTool(false);
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- if (SnapToolEvent != null)
- {
- KeyValuePair<string, object> param = new KeyValuePair<string, object>("CloseSnap", null);
- SnapToolEvent.Invoke(this, param);
- }
- }
- }
- }
- private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
- {
- System.Drawing.Bitmap bmp;
- using (MemoryStream outStream = new MemoryStream())
- {
- BitmapEncoder enc = new BmpBitmapEncoder();
- enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
- enc.Save(outStream);
- bmp = new System.Drawing.Bitmap(outStream);
- }
- return bmp;
- }
- /// <summary>
- /// 从Bitmap转换成BitmapSource
- /// </summary>
- /// <param name="bmp"></param>
- /// <returns></returns>
- public BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
- {
- BitmapSource returnSource;
- try
- {
- returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
- }
- catch
- {
- returnSource = null;
- }
- return returnSource;
- }
- }
- }
|