123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Office.Model;
- using PDF_Office.Model.AnnotPanel;
- using PDF_Office.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.Media;
- using System.Windows.Media.Imaging;
- using static Dropbox.Api.Files.FileCategory;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- internal class StampAnnotPropertyViewModel : BindableBase, INavigationAware
- {
- #region 标准图章相关默认配置(如果需要修改,要留意按顺序一一对应的,如果要支持多语,Path则需要替换成SDK获取图片的方法,并修改对应参数与实现代码)
- List<string> Path = new List<string>
- {
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Approved.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/NotApproved.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Completed.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Final.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Draft.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Confidential.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/ForPublicRelease.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/NotForPublicRelease.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/ForComment.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Void.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/PreliminaryResults.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/InformationOnly.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Accepted.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Rejected.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/Witness.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/InitialHere.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/SignHere.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/revised.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark1.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark2.png",
- "pack://application:,,,/PDF Office;component/Resources/StampIcons/PrivateMark3.png",
- };
- List<string> StampText = new List<string>
- {
- "Approved","NotApproved","Completed","Final","Draft","Confidential","ForPublicRelease","NotForPublicRelease",
- "ForComment","Void","PreliminaryResults","InformationOnly","Accepted","Rejected","Witness","InitialHere","SignHere",
- "revised","PrivateMark#1","PrivateMark#2","PrivateMark#3"
- };
- List<int> MaxWidth = new List<int>
- {
- 218,292,234,130,150,280,386,461,282,121,405,366,30,30,133,133,133,173,30,30,30
- };
- List<int> MaxHeight = new List<int>
- {
- 66,66,66,66,66,66,66,66,66,66,66,66,30,30,39,39,39,66,30,30,30
- };
- #endregion
- #region 动态图章相关配置(需要支持多语则修改DynamicStampText的内容)
- List<string> DynamicStampText = new List<string>
- {
- "REVISED","REVIEWED","Completed","RECEIVED","APPROVED","CONFIDENTIAL"
- };
- List<C_TEXTSTAMP_COLOR> DynamicColor = new List<C_TEXTSTAMP_COLOR>
- {
- C_TEXTSTAMP_COLOR.TEXTSTAMP_RED, C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,
- C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,
- C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN,C_TEXTSTAMP_COLOR.TEXTSTAMP_RED,
- };
- #endregion
- private string author = "12312";
- public string Author
- {
- get { return author; }
- set
- {
- SetProperty(ref author, value);
- }
- }
- private bool isSetAuthor = false;
- public bool IsSetAuthor
- {
- get { return isSetAuthor; }
- set
- {
- SetProperty(ref isSetAuthor, value);
- }
- }
- private int tabControlSelectedIndex = 0;
- public int TabControlSelectedIndex
- {
- get { return tabControlSelectedIndex; }
- set
- {
- SetProperty(ref tabControlSelectedIndex, value);
- if (tabControlSelectedIndex == 0)
- {
- UnStandard = false;
- }
- else
- {
- UnStandard = true;
- }
- }
- }
- private bool unStandard;
- /// <summary>
- /// 判断当前是否为非标准图章,控制UI展示
- /// </summary>
- public bool UnStandard
- {
- get { return unStandard; }
- set
- {
- SetProperty(ref unStandard, value);
- }
- }
- /// <summary>
- /// 日期格式
- /// </summary>
- private string dateType = "F";
- private IDialogService dialogs;
- public DelegateCommand ShowDialogCommand { get; set; }
- private CPDFViewer PDFViewer;
- public ObservableCollection<Stamp> StandardStampList { get; set; }
- public ObservableCollection<Stamp> DynamicStampList { get; set; }
- public ObservableCollection<Stamp> CustomStampList { get; set; }
- public StampAnnotPropertyViewModel(IDialogService dialogService)
- {
- dialogs = dialogService;
- StandardStampList = new ObservableCollection<Stamp>();
- DynamicStampList = new ObservableCollection<Stamp>();
- CustomStampList = new ObservableCollection<Stamp>();
- ShowDialogCommand = new DelegateCommand(ShowDialog);
- InitStandardStamp();
- UpDataDynamicStampList();
- }
- private void LoadSettings()
- {
- PDFSettings.CustomStampList stamps = Settings.Default.CustomStampList;
- for (int i = 0; i < stamps.Count; i++)
- {
- Stamp customStamp = new Stamp();
- customStamp.Opacity = 1;
- customStamp.Author = stamps[i].Author;
- customStamp.StampText = stamps[i].StampText;
- customStamp.StampTextDate = stamps[i].StampTextDate;
- customStamp.MaxWidth = stamps[i].ImageWidth;
- customStamp.MaxHeight = stamps[i].ImageHeight;
- customStamp.SourcePath = stamps[i].SourcePath;
- customStamp.Type = stamps[i].Type;
- customStamp.TextColor = stamps[i].TextColor;
- CustomStampList.Add(customStamp);
- }
- }
- /// <summary>
- /// 初始化标准图章相关内容
- /// </summary>
- private void InitStandardStamp()
- {
- for (int i = 0; i < Path.Count; i++)
- {
- Stamp standardStamp = new Stamp();
- standardStamp.Author = "";
- standardStamp.Opacity = 1;
- standardStamp.SourcePath = Path[i];
- standardStamp.StampText = StampText[i];
- standardStamp.MaxWidth = MaxWidth[i];
- standardStamp.MaxHeight = MaxHeight[i];
- standardStamp.Type = StampType.STANDARD_STAMP;
- StandardStampList.Add(standardStamp);
- }
- }
- 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;
- }
- }
- /// <summary>
- /// 在PDFView上创建图章的统一方法
- /// </summary>
- /// <param name="stamp"></param>
- public void SetStamp(Stamp stamp)
- {
- StampAnnotArgs Args = new StampAnnotArgs();
- Args.StampText = stamp.StampText;
- Args.Author = stamp.Author;
- Args.Opacity = stamp.Opacity;
- Args.MaxWidth = stamp.MaxWidth;
- Args.MaxHeight = stamp.MaxHeight;
- Args.StampTextDate = stamp.StampTextDate;
- Args.TextColor = stamp.TextColor;
- if (!string.IsNullOrEmpty(stamp.SourcePath))
- {
- BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
- Args.ImageArray = new byte[image.PixelWidth * image.PixelHeight * 4];
- image.CopyPixels(Args.ImageArray, image.PixelWidth * 4, 0);
- Args.ImageHeight = image.PixelHeight;
- Args.ImageWidth = image.PixelWidth;
- }
- else
- {
- Args.ImageArray = new byte[stamp.ImageSource.PixelWidth * stamp.ImageSource.PixelHeight * 4];
- stamp.ImageSource.CopyPixels(Args.ImageArray, stamp.ImageSource.PixelWidth * 4, 0);
- Args.ImageHeight = stamp.ImageSource.PixelHeight;
- Args.ImageWidth = stamp.ImageSource.PixelWidth;
- }
- Args.Type = stamp.Type;
- PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- PDFViewer.SetToolParam(Args);
- }
- /// <summary>
- /// 更新动态图章对象相关属性
- /// </summary>
- public void UpDataDynamicStampList()
- {
- CPDFDocument doc = CPDFDocument.CreateDocument();
- bool tt = doc.InsertPage(0, 300, 500, null);
- CPDFPage docPage = doc.PageAtIndex(0);
- CPDFStampAnnotation stampCore = docPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
- var x = stampCore.GetTextAttribute();
- DynamicStampList.Clear();
- for (int i = 0; i < DynamicStampText.Count; i++)
- {
- string date = "";
- //区分机密(CONFIDENTIAL)和其他动态图章的区别
- if (i < DynamicStampText.Count - 1)
- {
- date = System.DateTime.Now.ToString(dateType);
- }
- Stamp standardStamp = new Stamp();
- if (IsSetAuthor && !string.IsNullOrEmpty(Author))
- {
- standardStamp.Author = Author;
- if (i < DynamicStampText.Count - 1)
- {
- standardStamp.StampTextDate = "By " + Author + " at " + date;
- }
- else
- {
- standardStamp.StampTextDate = Author;
- }
- }
- else
- {
- standardStamp.StampTextDate = date;
- standardStamp.Author = "";
- }
- var bytes = CPDFStampAnnotation.GetTempTextStampImage(DynamicStampText[i], standardStamp.StampTextDate,
- C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT, DynamicColor[i], out int stampWidth, out int stampHeight, out int width, out int height);
- PixelFormat fmt = PixelFormats.Bgra32;
- BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
- standardStamp.Opacity = 1;
- standardStamp.SourcePath = "";
- standardStamp.StampText = DynamicStampText[i];
- standardStamp.MaxWidth = stampWidth;
- standardStamp.MaxHeight = stampHeight;
- standardStamp.Type = StampType.TEXT_STAMP;
- standardStamp.ImageSource = bps;
- switch (DynamicColor[i])
- {
- case C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE:
- break;
- case C_TEXTSTAMP_COLOR.TEXTSTAMP_RED:
- standardStamp.TextColor = TextStampColor.TEXTSTAMP_RED;
- break;
- case C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN:
- standardStamp.TextColor = TextStampColor.TEXTSTAMP_GREEN;
- break;
- case C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE:
- break;
- default:
- break;
- }
- DynamicStampList.Add(standardStamp);
- }
- }
- public void SaveToPath(string FileType, Stamp Item)
- {
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "保存" + FileType + "文件";
- saveFileDialog.Filter = "(*)|*." + FileType;
- if (saveFileDialog.ShowDialog() == false)
- {
- return;
- }
- BitmapEncoder encoder;
- if (FileType.ToUpper() == "JPG")
- {
- encoder = new JpegBitmapEncoder();
- }
- else
- {
- encoder = new PngBitmapEncoder();
- }
- encoder.Frames.Add(BitmapFrame.Create(new Uri(Item.SourcePath)));
- if (FileType.ToUpper() != "PDF")
- {
- string path = saveFileDialog.FileName;
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- CPDFDocument doc = CPDFDocument.CreateDocument();
- bool tt = doc.InsertPage(0, 300, 500, path);
- doc.WriteToFilePath("C:\\Users\\93131\\Desktop\\test\\cache\\fafdas.pdf");
- doc.Release();
- System.Diagnostics.Process.Start("explorer", "/select,\"" + path + "\"");
- }
- else
- {
- }
- }
- private void ShowDialog()
- {
- switch (TabControlSelectedIndex)
- {
- case 1:
- ShowDynamicPropertyDialog();
- break;
- case 2:
- ShowCustomCreateDialog();
- break;
- default:
- break;
- }
- }
- private void ShowDynamicPropertyDialog()
- {
- bool result = true;
- dialogs.ShowDialog(DialogNames.DynamicPropertyDialog, null, e =>
- {
- if (e.Result != ButtonResult.OK)
- {
- result = false;
- }
- DynamicPropertyDialogViewModel DynamicVM = e.Parameters.GetValue<DynamicPropertyDialogViewModel>(ParameterNames.DataModel);
- if (DynamicVM != null)
- {
- Author = DynamicVM.Author;
- IsSetAuthor = DynamicVM.IsChecked;
- //需要加多语判断
- dateType = DynamicVM.SelectedIndex == 0 ? "F" : DynamicVM.DateFormatList[DynamicVM.SelectedIndex] + " HH:mm:ss";
- UpDataDynamicStampList();
- }
- });
- if (!result)
- {
- return;
- }
- }
- private void ShowCustomCreateDialog()
- {
- bool result = true;
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFViewer, PDFViewer);
- dialogs.ShowDialog(DialogNames.CustomCreateDialog, value, e =>
- {
- if (e.Result != ButtonResult.OK)
- {
- result = false;
- }
- CustomCreateDialogViewModel CustomVM = e.Parameters.GetValue<CustomCreateDialogViewModel>(ParameterNames.DataModel);
- if (CustomVM != null)
- {
- CreateCustomStamp(CustomVM);
- }
- });
- if (!result)
- {
- return;
- }
- }
- private void CreateCustomStamp(CustomCreateDialogViewModel viewModel)
- {
- Stamp stamp = new Stamp();
- stamp.Author = "";
- stamp.Opacity = 1;
- stamp.SourcePath = viewModel.SaveToPath;
- stamp.StampText = viewModel.StampText;
- stamp.MaxWidth = viewModel.ImageSource.PixelWidth;
- stamp.MaxHeight = viewModel.ImageSource.PixelHeight;
- stamp.StampTextDate = viewModel.StampTextDate;
- stamp.Type = viewModel.Type;
- CustomStampList.Add(stamp);
- }
- }
- }
|