using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using PDF_Office.Model.AnnotPanel; 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.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 Path = new List { "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 StampText = new List { "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 MaxWidth = new List { 218,292,234,130,150,280,386,461,282,121,405,366,30,30,133,133,133,173,30,30,30 }; List MaxHeight = new List { 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 DynamicStampText = new List { "REVISED","REVIEWED","Completed","RECEIVED","APPROVED","CONFIDENTIAL" }; List DynamicColor = new List { 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; /// /// 判断当前是否为非标准图章,控制UI展示 /// public bool UnStandard { get { return unStandard; } set { SetProperty(ref unStandard, value); } } /// /// 日期格式 /// private string dateType = "F"; private IDialogService dialogs; public DelegateCommand ShowDialogCommand { get; set; } private CPDFViewer PDFViewer; public ObservableCollection StandardStampList { get; set; } public ObservableCollection DynamicStampList { get; set; } public ObservableCollection CustomStampList { get; set; } public StampAnnotPropertyViewModel(IDialogService dialogService) { dialogs = dialogService; StandardStampList = new ObservableCollection(); DynamicStampList = new ObservableCollection(); CustomStampList = new ObservableCollection(); ShowDialogCommand = new DelegateCommand(ShowDialog); InitStandardStamp(); UpDataDynamicStampList(); //CPDFDocument doc = CPDFDocument.CreateDocument(); //bool tt= doc.InsertPage(0, 300, 500, Path[0]); //doc.WriteToFilePath("D:\\fafdas.pdf"); //doc.Release(); } /// /// 初始化标准图章相关内容 /// 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(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer == null) { return; } } /// /// 在PDFView上创建图章的统一方法 /// /// 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); } /// /// 更新动态图章对象相关属性 /// 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); } } 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(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(ParameterNames.DataModel); if (CustomVM != null) { } }); if (!result) { return; } } } }