123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Office.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- class CustomCreateDialogViewModel : BindableBase, IDialogAware
- {
- public string Title => ".";
- public event Action<IDialogResult> RequestClose;
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand CreateCommnad { get; set; }
- public DelegateCommand UpDataDynamicCommnad { get; set; }
- public DelegateCommand OpenImageCommnad { get; set; }
- public DelegateCommand<object> KeyDown { get; set; }
- private BitmapSource textImageSource;
- public BitmapSource TextImageSource
- {
- get { return textImageSource; }
- set
- {
- SetProperty(ref textImageSource, value);
- }
- }
- private BitmapSource imagePreviewSource;
- public BitmapSource ImagePreviewSource
- {
- get { return imagePreviewSource; }
- set
- {
- SetProperty(ref imagePreviewSource, value);
- }
- }
- private string stampText = "Stamp Text";
- public string StampText
- {
- get { return stampText; }
- set
- {
- SetProperty(ref stampText, value);
- }
- }
- private bool isCheckedDate;
- public bool IsCheckedDate
- {
- get { return isCheckedDate; }
- set
- {
- SetProperty(ref isCheckedDate, value);
- UpDataStamp();
- }
- }
- private bool isCheckedTime;
- public bool IsCheckedTime
- {
- get { return isCheckedTime; }
- set
- {
- SetProperty(ref isCheckedTime, value);
- UpDataStamp();
- }
- }
- private Visibility showImageButton = Visibility.Visible;
- public Visibility ShowImageButton
- {
- get { return showImageButton; }
- set
- {
- SetProperty(ref showImageButton, value);
- }
- }
- private C_TEXTSTAMP_SHAPE shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
- public C_TEXTSTAMP_SHAPE Shape
- {
- get { return shape; }
- set { shape = value; }
- }
- private C_TEXTSTAMP_COLOR color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
- public C_TEXTSTAMP_COLOR Color
- {
- get { return color; }
- set { color = value; }
- }
- public string SaveToPath { get; private set; }
- public string StampTextDate { get; private set; }
- public int StampWidth { get; private set; }
- public int StampHeight { get; private set; }
- public StampType Type { get; private set; }
- public void SetStampStyle(int index)
- {
- switch (index)
- {
- case 1:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
- break;
- case 2:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
- break;
- case 3:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
- break;
- case 4:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
- break;
- case 5:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
- break;
- case 6:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
- break;
- case 7:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
- break;
- case 8:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
- break;
- case 9:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
- break;
- case 10:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
- break;
- case 11:
- Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
- Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
- break;
- default:
- break;
- }
- UpDataStamp();
- }
- public CustomCreateDialogViewModel()
- {
- CancelCommand = new DelegateCommand(Cancel);
- CreateCommnad = new DelegateCommand(Create);
- UpDataDynamicCommnad = new DelegateCommand(UpDataStamp);
- KeyDown = new DelegateCommand<object>(PreviewKeyDown);
- OpenImageCommnad = new DelegateCommand(OpenImage);
- }
- private void Cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- private void Create()
- {
- if (Type == StampType.TEXT_STAMP)
- {
- SaveImageToPath();
- }
- DialogParameters valuePairs = new DialogParameters();
- valuePairs.Add(ParameterNames.DataModel, this);
- RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
- }
- private void SaveImageToPath()
- {
- string path = App.CachePath.CustomStampPath;
- string name = Guid.NewGuid().ToString();
- if (!string.IsNullOrEmpty(path))
- {
- BitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(TextImageSource));
- path = System.IO.Path.Combine(path, name);
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- SaveToPath = path;
- }
- else
- {
- SaveToPath = "";
- }
- }
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- return;
- }
- private void PreviewKeyDown(object e)
- {
- var args = e as KeyEventArgs;
- if (args == null)
- {
- return;
- }
- if (args.Key == Key.Enter)
- {
- StampText = (args.OriginalSource as TextBox).Text;
- UpDataStamp();
- }
- }
- public void UpDataStamp()
- {
- string date = "";
- string dateType = "";
- if (IsCheckedDate)
- {
- dateType = "yyyy-MM-dd";
- }
- if (IsCheckedTime)
- {
- dateType = dateType + " HH:mm:ss";
- }
- if (!String.IsNullOrEmpty(dateType))
- {
- date = DateTime.Now.ToString(dateType);
- }
- var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
- Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
- if (bytes.Length > 0)
- {
- PixelFormat fmt = PixelFormats.Bgra32;
- BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
- TextImageSource = bps;
- StampTextDate = date;
- StampWidth = stampWidth;
- StampHeight = stampHeight;
- Type = StampType.TEXT_STAMP;
- }
- else
- {
- TextImageSource = null;
- }
- }
- public void OpenImage()
- {
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
- "(*.gif)|*.gif|" +
- "(*.jpeg)|*.jpeg|" +
- "(*.jpg)|*.jpg|" +
- "(*.png)|*.png|" +
- "(*.tiff)|*.tiff";
- if (openFile.ShowDialog() == false)
- {
- return;
- }
- string path = App.CachePath.CustomStampPath;
- string name = Guid.NewGuid().ToString();
- if (!string.IsNullOrEmpty(path))
- {
- BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
- double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
- scale = Math.Min(scale, 1);
- string ext = Path.GetExtension(openFile.FileName);
- if (ext.ToUpper() == ".PNG")
- {
- BitmapEncoder encoder = new PngBitmapEncoder();
- var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
- encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
- path = System.IO.Path.Combine(path, name);
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- if (!string.IsNullOrEmpty(SaveToPath))
- {
- App.CachePath.AddToDeleteFiles(SaveToPath);
- }
- SaveToPath = path;
- ImagePreviewSource = targetBitmap;
- StampWidth = targetBitmap.PixelWidth;
- StampHeight = targetBitmap.PixelHeight;
- Type = StampType.IMAGE_STAMP;
- ShowImageButton = Visibility.Collapsed;
- }
- else
- {
- BitmapEncoder encoder = new JpegBitmapEncoder();
- TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
- encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
- path = System.IO.Path.Combine(path, name);
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- if (!string.IsNullOrEmpty(SaveToPath))
- {
- App.CachePath.AddToDeleteFiles(SaveToPath);
- }
- SaveToPath = path;
- ImagePreviewSource = targetBitmap;
- StampWidth = targetBitmap.PixelWidth;
- StampHeight = targetBitmap.PixelHeight;
- Type = StampType.IMAGE_STAMP;
- ShowImageButton = Visibility.Collapsed;
- }
- }
- else
- {
- SaveToPath = "";
- }
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- UpDataStamp();
- return;
- }
- }
- }
|