123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using ComPDFKit.PDFDocument;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Master.Model;
- using PDF_Master.Model.AnnotPanel;
- using PDF_Master.Properties;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Ink;
- using System.Windows.Media.Imaging;
- namespace PDF_Master.ViewModels.PropertyPanel.AnnotPanel
- {
- class SignatureAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- private CPDFViewer PDFViewer;
- private IDialogService dialogs;
- public DelegateCommand ShowDialogCommand { get; set; }
- private Signature currentSignature = null;
- private int currentListboxIndex = -1;
- private ViewContentViewModel viewContentViewModel;
- public int CurrentListboxIndex
- {
- get { return currentListboxIndex; }
- set
- {
- SetProperty(ref currentListboxIndex, value);
- }
- }
- public ObservableCollection<Signature> SignatureList { get; set; }
- public SignatureAnnotPropertyViewModel(IDialogService dialogService)
- {
- dialogs = dialogService;
- ShowDialogCommand = new DelegateCommand(ShowDialog);
- SignatureList = new ObservableCollection<Signature>();
- LoadSettings();
- }
- /// <summary>
- /// 加载缓存的签名
- /// </summary>
- private void LoadSettings()
- {
- PDFSettings.SignatureList items = Settings.Default.SignatureList;
- if (items != null)
- {
- for (int i = 0; i < items.Count; i++)
- {
- Signature customStamp = new Signature();
- customStamp.SourcePath = items[i].SourcePath;
- customStamp.DrawingPath = items[i].DrawingPath;
- customStamp.inkThickness = items[i].inkThickness;
- customStamp.inkColor = items[i].inkColor;
- customStamp.Type = (SignatureType)items[i].Type;
- if (File.Exists(items[i].SourcePath))
- {
- SignatureList.Add(customStamp);
- }
-
- }
- }
- }
- /// <summary>
- /// 应用签名
- /// </summary>
- public void SetSignature(Signature signature)
- {
- switch (signature.Type)
- {
- case SignatureType.TextType:
- case SignatureType.ImageType:
- {
- StampAnnotArgs stampArgs = new StampAnnotArgs();
- stampArgs.Opacity = 1;
- stampArgs.Type = StampType.IMAGE_STAMP;
- stampArgs.ImagePath = signature.SourcePath;
- //stampArgs.ImageHeight = 140;
- //stampArgs.ImageWidth = 140;
- stampArgs.MaxHeight = 140;
- stampArgs.MaxWidth = 140;
- PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- PDFViewer.SetToolParam(stampArgs);
- currentSignature = signature;
- }
- break;
- case SignatureType.Drawing:
- {
- StampAnnotArgs stampArgs = new StampAnnotArgs();
- stampArgs.SetInkData(GetPoints(signature.DrawingPath), signature.inkThickness, signature.inkColor);
- PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- PDFViewer.SetToolParam(stampArgs);
- currentSignature = signature;
- }
- break;
- default:
- break;
- }
- }
- private List<List<Point>> GetPoints(string Path)
- {
- StrokeCollection Strokes;
- List<List<Point>> RawPointList = new List<List<Point>>();
- using (FileStream strokeStream = File.OpenRead(Path))
- {
- Strokes = new StrokeCollection(strokeStream);
- }
- for (int kk = 0; kk < Strokes.Count; kk++)
- {
- List<Point> p = new List<Point>();
- RawPointList.Add(p);
- for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
- {
- var point = Strokes[kk].StylusPoints[gg].ToPoint();
- if (point.X >= 0 && point.Y >= 0)
- RawPointList[kk].Add(point);
- }
- }
- return RawPointList;
- }
- private void ShowDialog()
- {
- bool result = true;
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFViewer, PDFViewer);
- dialogs.ShowDialog(DialogNames.SignatureCreateDialog, value, e =>
- {
- if (e.Result != ButtonResult.OK)
- {
- result = false;
- }
- SignatureCreateDialogViewModel DialogVM = e.Parameters.GetValue<SignatureCreateDialogViewModel>(ParameterNames.DataModel);
- if (DialogVM != null)
- {
- CreateSignature(DialogVM);
- }
- });
- if (!result)
- {
- return;
- }
- }
- /// <summary>
- /// 创建签名,并保存到APP缓存
- /// </summary>
- /// <param name="viewModel"></param>
- private void CreateSignature(SignatureCreateDialogViewModel viewModel)
- {
- Signature Signature = new Signature();
- if (viewModel.IsRemoveBackground)
- {
- if (!string.IsNullOrEmpty(viewModel.SaveToPath))
- {
- App.CachePath.AddToDeleteFiles(viewModel.SaveToPath);
- }
- Signature.SourcePath = viewModel.RemoveBackgroundSaveToPath;
- }
- else
- {
- Signature.SourcePath = viewModel.SaveToPath;
- }
- Signature.DrawingPath = viewModel.DrawingSaveToPath;
- Signature.Type = (SignatureType)viewModel.TabItemIndex;
- //根据当前选项设置粗细
- double inkThickness;
- if (viewModel.ThicknessListIndex >= 0)
- {
- inkThickness = Convert.ToDouble(viewModel.ThicknessList[viewModel.ThicknessListIndex].Substring(0, 3));
- }
- else
- {
- inkThickness = viewModel.DrawingAttributeObject.Width;
- }
- Signature.inkThickness = inkThickness;
- Signature.inkColor = viewModel.DrawingAttributeObject.Color;
- SignatureList.Add(Signature);
- //缓存数据
- PDFSettings.SignatureList stamps = Settings.Default.SignatureList;
- if (stamps == null)
- {
- stamps = Settings.Default.SignatureList = new PDFSettings.SignatureList();
- }
- PDFSettings.SignatureItem signature = new PDFSettings.SignatureItem();
- signature.DrawingPath = Signature.DrawingPath;
- signature.SourcePath = Signature.SourcePath;
- signature.inkThickness = Signature.inkThickness;
- signature.inkColor = Signature.inkColor;
- signature.Type = (PDFSettings.SignatureType)Signature.Type;
- stamps.Add(signature);
- Settings.Default.Save();
- SetSignature(Signature);
- CurrentListboxIndex = SignatureList.Count - 1;
- }
- public void Close()
- {
- //PDFViewer.SetMouseMode(MouseModes.PanTool);
- PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- var annotArgs = new SelectToolArgs();
- PDFViewer.SetToolParam(annotArgs);
- //PDFViewer.SetMouseMode(MouseModes.PanTool);
- PDFViewer.DisableClearSelectAnnot(false);
- PDFViewer.ClearSelectAnnots();
- viewContentViewModel.IsPropertyOpen = false;
- }
- /// <summary>
- /// 导出签名
- /// </summary>
- public void SaveToPath(string FileType, Signature Item)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "保存" + FileType + "文件";
- saveFileDialog.Filter = "(*)|*." + FileType;
- if (saveFileDialog.ShowDialog() == false)
- {
- return;
- }
- BitmapEncoder encoder;
- if (FileType.ToUpper() == "JPG")
- {
- //如果要导出JPG,并且要白色背景,https://social.msdn.microsoft.com/Forums/en-US/2d574827-7eb7-488e-a86b-861ccb4f0275/set-image-background-white-and-save-it?forum=wpf
- encoder = new JpegBitmapEncoder();
- }
- else
- {
- encoder = new PngBitmapEncoder();
- }
- encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
- string path = saveFileDialog.FileName;
- switch (FileType.ToUpper())
- {
- case "PDF":
- CPDFDocument newDoc = CPDFDocument.CreateDocument();
- BitmapFrame frame = BitmapFrame.Create(new Uri(Item.SourcePath));
- byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- newDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
- newDoc.WriteToFilePath(path);
- break;
- case "PNG":
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- break;
- default:
- break;
- }
- System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
- }
- public void DeleteSignature(Signature Item)
- {
- if (currentSignature.Equals(Item))
- {
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- }
- if (SignatureList.IndexOf(currentSignature) < 0)
- {
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- }
- int index = SignatureList.IndexOf(Item);
- App.CachePath.AddToDeleteFiles(Item.SourcePath);
- if (!string.IsNullOrEmpty(Item.DrawingPath))
- {
- App.CachePath.AddToDeleteFiles(Item.DrawingPath);
- }
- SignatureList.RemoveAt(index);
- PDFSettings.SignatureList signature = Settings.Default.SignatureList;
- signature.RemoveAt(index);
- Settings.Default.Save();
- }
- public void DeleteAll()
- {
- foreach (Signature item in SignatureList)
- {
- App.CachePath.AddToDeleteFiles(item.SourcePath);
- if (!string.IsNullOrEmpty(item.DrawingPath))
- {
- App.CachePath.AddToDeleteFiles(item.DrawingPath);
- }
- }
- SignatureList.Clear();
- PDFSettings.SignatureList signature = Settings.Default.SignatureList;
- signature.Clear();
- Settings.Default.Save();
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- currentSignature = null;
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- return;
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer == null)
- {
- return;
- }
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- }
- }
- }
|