using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKit.PDFWatermark; using ComPDFKitViewer.PdfViewer; using PDF_Office.CustomControl; using PDF_Office.EventAggregators; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.EditTools.Watermark; using PDF_Office.Model.PageEdit; using PDF_Office.Properties; using PDFSettings; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; namespace PDF_Office.ViewModels.EditTools.Watermark { public class WatermarkCreateFileContentViewModel : BindableBase, INavigationAware { public WatermarkInfo WatermarkInfo = new WatermarkInfo(); private CPDFViewer PDFViewer; IEventAggregator eventAggregator; private List<string> _opacityList = new List<string>(); public List<string> OpacityList { get { return _opacityList; } set { SetProperty(ref _opacityList, value); } } private void InitOpacityList() { OpacityList.Clear(); for (int temp = 0; temp <= 100; temp += 10) { OpacityList.Add(temp.ToString() + " %"); } } private List<string> _rotationList = new List<string>(); public List<string> RotationList { get { return _rotationList; } set { SetProperty(ref _rotationList, value); } } private void InitRotationList() { RotationList.Clear(); for (int temp = -45; temp <= 45; temp += 15) { RotationList.Add(temp.ToString()); } } private List<string> _scaleList = new List<string>(); public List<string> ScaleList { get { return _scaleList; } set { SetProperty(ref _scaleList, value); } } private void InitScaleList() { ScaleList.Clear(); for (int temp = 0; temp <= 100; temp += 10) { ScaleList.Add(temp.ToString() + " %"); } } private List<string> _isFrontList = new List<string>(); public List<string> IsFrontList { get { return _isFrontList; } set { SetProperty(ref _isFrontList, value); } } private void InitIsFrontList() { IsFrontList.Clear(); IsFrontList.Add("位于页面上方"); IsFrontList.Add("位于页面下方"); } private int _rotationValue = 0; public int RotationValue { get { return _rotationValue; } set { SetProperty(ref _rotationValue, value); WatermarkInfo.Rotation = ((float)RotationValue / 180) * 3.1415926f; eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private int _opacityValue = 100; public int OpacityValue { get { return _opacityValue; } set { SetProperty(ref _opacityValue, value); WatermarkInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private int _relativeScaleValue = 50; public int RelativeScaleValue { get { return _relativeScaleValue; } set { SetProperty(ref _relativeScaleValue, value); } } private string _vertOffsetValue = "0"; public string VertOffsetValue { get { return _vertOffsetValue; } set { SetProperty(ref _vertOffsetValue, value); WatermarkInfo.VertOffset = float.Parse(VertOffsetValue); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private string _horizOffsetValue = "0"; public string HorizOffsetValue { get { return _horizOffsetValue; } set { SetProperty(ref _horizOffsetValue, value); WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private string _verticalSpacingValue = "6"; public string VerticalSpacingValue { get { return _verticalSpacingValue; } set { SetProperty(ref _verticalSpacingValue, value); WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private string _horizontalSpacingValue = "6"; public string HorizontalSpacingValue { get { return _horizontalSpacingValue; } set { SetProperty(ref _horizontalSpacingValue, value); WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } public string _pageRangeText = "0"; public string PageRangeText { get { return _pageRangeText; } set { _pageRangeText = value; EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private bool _isFullValue = false; public bool IsFullValue { get { return _isFullValue; } set { SetProperty(ref _isFullValue, value); WatermarkInfo.Isfull = IsFullValue; eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private int _pageRangeSelectIndex = 0; public int PageRangeSelectIndex { get { return _pageRangeSelectIndex; } set { SetProperty(ref _pageRangeSelectIndex, value); EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private int _isFrontSelectedIndex = 0; public int IsFrontSelectedIndex { get { return _isFrontSelectedIndex; } set { SetProperty(ref _isFrontSelectedIndex, value); SetIsFront(IsFrontSelectedIndex); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } private string _fileNameText = ""; public string FileNameText { get { return _fileNameText; } set { SetProperty(ref _fileNameText, value); } } private Visibility _createFileVisible = Visibility.Collapsed; public Visibility CreateFileVisible { get { return _createFileVisible; } set { SetProperty(ref _createFileVisible, value); } } private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>(); public ObservableDictionary<string, bool> GetLocationFromNumber { get { return _getLocationFromNumber; } set { _getLocationFromNumber = value; } } public DelegateCommand<object> ChangeLocationCommand { get; set; } public DelegateCommand OpenFileCommand { get; set; } public WatermarkCreateFileContentViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; WatermarkInfo.WatermarkType=C_Watermark_Type.WATERMARK_TYPE_IMG; ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation); OpenFileCommand = new DelegateCommand(OpenFile); InitList(); InitWatermarkInfo(); InitLocationButtonMatrix(); eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Subscribe(ConfirmEditToolsWatermark); eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Subscribe(SaveWatermarkTemplate); } public void SaveWatermarkTemplate(EnumTextOrFile enumTextOrFile) { if (enumTextOrFile == EnumTextOrFile.StatusFile && FileNameText!="") { SaveCurrentTemplate(); } } public void SaveCurrentTemplate() { var watermarkItem = new WatermarkItem(); watermarkItem.pageRange = "0"; watermarkItem.type = ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG; watermarkItem.templateName += Settings.Default.BackgroundIndex.ToString(); Settings.Default.WatermarkTemplateList.Add(watermarkItem); Settings.Default.Save(); this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate); } private void SetIsFront(int Index) { if (Index == 0) { WatermarkInfo.IsFront = true; } if (Index == 1) { WatermarkInfo.IsFront = false; } } private void InitList() { InitOpacityList(); InitIsFrontList(); InitRotationList(); InitScaleList(); } private void InitWatermarkInfo() { WatermarkInfo.Rotation = ((float)RotationValue / 180) * 3.1415926f; WatermarkInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225); WatermarkInfo.VertOffset = float.Parse(VertOffsetValue); WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue); WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue); WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue); WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER; WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER; WatermarkInfo.Isfull = IsFullValue; SetIsFront(IsFrontSelectedIndex); } private void InitLocationButtonMatrix() { GetLocationFromNumber.Clear(); for (var temp = 0; temp <= 22; temp++) { GetLocationFromNumber.Add(temp.ToString(), true); if (temp % 10 == 2) { temp += 7; } } int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign; GetLocationFromNumber[Num.ToString()] = false; } public void ChangeLocation(object e) { string args = e as string; if (args != null) { WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10); WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10); eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); InitLocationButtonMatrix(); } } public void ConfirmEditToolsWatermark(EnumTextOrFile enumTextOrFile) { if (enumTextOrFile == EnumTextOrFile.StatusFile) { eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } public void OpenFile() { System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = "PDF|*.png;*.jpg;*.pdf"; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FileNameText = dlg.SafeFileName; FileInfo file = new FileInfo(dlg.FileName); if (file.Extension == ".pdf") { GetBitmapFromDocment(dlg.FileName); } else { EditToolsHelper.ChooseFile(dlg.FileName, ref WatermarkInfo); } CreateFileVisible=Visibility.Visible; eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo); } } public async void GetBitmapFromDocment(string filePath) { CPDFDocument document = CPDFDocument.InitWithFilePath(filePath); CPDFPage page = document.PageAtIndex(0); byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4]; await Task.Run(delegate { page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1); }); WatermarkInfo.ImageArray= bmp_data; WatermarkInfo.ImageWidth = (int)page.PageSize.Width; WatermarkInfo.ImageHeight = (int)page.PageSize.Height; document.ReleasePages(); document.Release(); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer); EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText); } } }