123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using PDFSettings;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using System.Windows;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using PDF_Office.Properties;
- using Prism.Events;
- using PDF_Office.EventAggregators;
- using PDF_Office.Model.EditTools.Watermark;
- using System.Windows.Controls;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.CustomControl;
- using PDF_Office.Model;
- using PDF_Office.Helper;
- using PDF_Office.Model.EditTools.Background;
- using PDF_Office.ViewModels.Tools;
- using Microsoft.Office.Interop.Word;
- namespace PDF_Office.ViewModels.EditTools.Watermark
- {
- public class WatermarkTemplateListTextContentViewModel : BindableBase, INavigationAware
- {
- private readonly IEventAggregator eventAggregator;
- private CPDFViewer CurrentPDFViewer;
- public WatermarkInfo WatermarkInfo = new WatermarkInfo();
- public WatermarkItem WatermarkItem = new WatermarkItem();
- public int TemplateIndex = 0;
- public ObservableCollection<WatermarkItem> watermarkModTextCollection = new ObservableCollection<WatermarkItem>();
- public ObservableCollection<WatermarkItem> WatermarkModTextCollection
- {
- get { return watermarkModTextCollection; }
- set
- {
- watermarkModTextCollection = value;
- RaisePropertyChanged();
- }
- }
- private Visibility _createTemplateVisible;
- public Visibility CreateTemplateVisible
- {
- get { return _createTemplateVisible; }
- set { SetProperty(ref _createTemplateVisible, value); }
- }
- public DelegateCommand AddTemplateCommand { get; set; }
- public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
- public DelegateCommand<object> EditTemplateItemCommand { get; set; }
- public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
- public DelegateCommand<object> SelectTemplateItemCommand { get; set; }
- public string Unicode = null;
- public WatermarkTemplateListTextContentViewModel(IEventAggregator eventAggregator)
- {
- this.eventAggregator = eventAggregator;
- Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- AddTemplateCommand = new DelegateCommand(AddTemplate);
- DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
- DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
- EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
- SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
- eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
- eventAggregator.GetEvent<SaveEditedWatermarkTemplateItemEvent>().Subscribe(SaveEditedWatermarkTemplateItem, e => e.Unicode == Unicode);
- }
- public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicode)
- {
- CurrentPDFViewer = cPDFViewerunicode.Status;
- }
- private void CheckTemplateListIsEmpty(List<WatermarkItem> watermarkTemplateList)
- {
- if (watermarkTemplateList.Count() == 0)
- {
- CreateTemplateVisible = Visibility.Visible;
- }
- else
- {
- CreateTemplateVisible = Visibility.Collapsed;
- }
- }
- private void GetWatermarkSource()
- {
- List<WatermarkItem> watermarkModTextTemplateList = new List<WatermarkItem>();
- for (int temp = 0; temp < Settings.Default.WatermarkTemplateList.Count; temp++)
- {
- if (Settings.Default.WatermarkTemplateList[temp].type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
- {
- watermarkModTextTemplateList.Add(Settings.Default.WatermarkTemplateList[temp]);
- }
- }
- WatermarkModTextCollection = new ObservableCollection<WatermarkItem>(watermarkModTextTemplateList);
- CheckTemplateListIsEmpty(watermarkModTextTemplateList);
- }
- private void InitWatermarkTemplateList()
- {
- if (Settings.Default.WatermarkTemplateList == null)
- {
- Settings.Default.WatermarkTemplateList = new WatermarkTemplateList();
- }
- GetWatermarkSource();
- }
- public void AddTemplate()
- {
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
- {
- Unicode = Unicode,
- Status = EnumTemplateListOrCreate.StatusCreate
- });
- }
- public void DeleteTemplateItem(object e)
- {
- var btn = e as System.Windows.Controls.Control;
- if (btn == null)
- {
- return;
- }
- var template = btn.DataContext as WatermarkItem;
- if (template == null)
- {
- return;
- }
- Settings.Default.WatermarkTemplateList.Remove(template);
- Settings.Default.Save();
- WatermarkModTextCollection.Remove(template);
- GetWatermarkSource();
- }
- public void DeleteAllTemplateItem()
- {
- List<WatermarkItem> removewatermarkItems=new List<WatermarkItem>();
- for (int i=0; i<Settings.Default.WatermarkTemplateList.Count;i++)
- {
-
- var template = Settings.Default.WatermarkTemplateList[i];
- if (template.type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
- {
- removewatermarkItems.Add(template);
- }
- }
- foreach (var removetemplate in removewatermarkItems)
- {
- Settings.Default.WatermarkTemplateList.Remove(removetemplate);
- Settings.Default.Save();
- WatermarkModTextCollection.Remove(removetemplate);
- }
- GetWatermarkSource();
- }
- public void RequestEditTemplateItem(object e)
- {
- var control = e as Control;
- if (control == null)
- {
- return;
- }
- var template = control.DataContext as WatermarkItem;
- if (template == null)
- {
- return;
- }
- TemplateIndex = Settings.Default.WatermarkTemplateList.IndexOf(template);
- template.listIndex = TemplateIndex;
- this.eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Publish(new WatermarkItemUnicode
- {
- Unicode = Unicode,
- Status = template
- });
- }
- public void SelectTemplateItem(object e)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- if (CurrentPDFViewer.Document.GetWatermarkCount() > 0)
- {
- alertsMessage.ShowDialog("文件中已添加水印,您要替换现有的水印吗?", "", "取消", "确定");
- if (alertsMessage.result == ContentResult.Ok)
- {
- this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
- {
- Unicode = Unicode,
- Status = EnumDelete.StatusDeleteInitial
- });
- }
- else
- {
- //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
- //{
- // Unicode = Unicode,
- // Status = EnumDelete.StatusCreate
- //});
- return;
- }
- }
- var listBox = e as ListBox;
- WatermarkItem WatermarkItem = listBox.SelectedItem as WatermarkItem;
- SendTemplateItemToDocument(WatermarkItem);
- }
- public void SaveEditedWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
- {
- WatermarkItem watermarkItem = watermarkItemunicode.Status;
- Settings.Default.WatermarkTemplateList[TemplateIndex] = watermarkItem;
- Settings.Default.Save();
- GetWatermarkSource();
- }
- public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
- {
- if (watermarkItem != null)
- {
- watermarkInfo.WatermarkType = watermarkItem.type;
- watermarkInfo.Text = watermarkItem.text;
- watermarkInfo.TextSize = watermarkItem.fontSize;
- watermarkInfo.FontName = watermarkItem.fontName;
- watermarkInfo.TextColor = watermarkItem.textcolor;
- watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
- watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
- watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
- watermarkInfo.Isfull = watermarkItem.isFullScreen;
- watermarkInfo.IsFront = watermarkItem.isfront;
- watermarkInfo.Opacity = watermarkItem.opacity;
- watermarkInfo.Rotation = watermarkItem.rotation;
- watermarkInfo.VertOffset = watermarkItem.vertOffset;
- watermarkInfo.HorizOffset = watermarkItem.horizOffset;
- watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
- watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
- watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
- watermarkInfo.PageRange = watermarkItem.pageRange;
- }
- }
- public void SendTemplateItemToDocument(WatermarkItem WatermarkItem)
- {
- ConvertItemToInfo(WatermarkItem, ref WatermarkInfo);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- InitWatermarkTemplateList();
- }
- }
- }
|