123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.EditTools.Background;
- using PDF_Office.Model.EditTools.Watermark;
- 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.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDF_Office.ViewModels.EditTools.Background
- {
- public class BackgroundCreateFileContentViewModel : BindableBase, INavigationAware
- {
- IEventAggregator eventAggregator;
- private BackgroundItem TemplateBackgroundItem;
- public BackgroundInfo BackgroundInfo = new BackgroundInfo();
- private CPDFViewer PDFViewer;
- private List<string> _rotationList = new List<string>();
- public List<string> RotationList
- {
- get { return _rotationList; }
- set { SetProperty(ref _rotationList, value); }
- }
- private List<string> _opacityList = new List<string>();
- public List<string> OpacityList
- {
- get { return _opacityList; }
- set
- {
- SetProperty(ref _opacityList, value);
- }
- }
- private List<string> _relativeRatioList = new List<string>();
- public List<string> RelativeRatioList
- {
- get { return _relativeRatioList; }
- set
- {
- SetProperty(ref _relativeRatioList, value);
- }
- }
- private int _rotationValue = 0;
- public int RotationValue
- {
- get { return _rotationValue; }
- set
- {
- SetProperty(ref _rotationValue, value);
- BackgroundInfo.Rotation = ((float)RotationValue / 180) * 3.1415926f;
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- }
- }
- private int _opacityValue = 100;
- public int OpacityValue
- {
- get { return _opacityValue; }
- set
- {
- SetProperty(ref _opacityValue, value);
- BackgroundInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225);
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- }
- }
- private int _relativeRatioValue = 0;
- public int RelativeRatioValue
- {
- get { return _relativeRatioValue; }
- set { SetProperty(ref _relativeRatioValue, value); }
- }
- private string _verticalOffsetValue = "0";
- public string VerticalOffsetValue
- {
- get { return _verticalOffsetValue; }
- set
- {
- SetProperty(ref _verticalOffsetValue, value);
- BackgroundInfo.VertOffset = float.Parse(VerticalOffsetValue);
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- }
- }
- private string _horizOffsetValue = "0";
- public string HorizontalOffsetValue
- {
- get { return _horizOffsetValue; }
- set
- {
- SetProperty(ref _horizOffsetValue, value);
- BackgroundInfo.HorizOffset = float.Parse(HorizontalOffsetValue);
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- }
- }
- private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
- public ObservableDictionary<string, bool> GetLocationFromNumber
- {
- get { return _getLocationFromNumber; }
- }
- private string _fileNameText = "";
- public string FileNameText
- {
- get { return _fileNameText; }
- set
- {
- SetProperty(ref _fileNameText, value);
- }
- }
- private bool isFirstEnter = true;
- public bool IsFirstEnter
- {
- get => isFirstEnter;
- set => isFirstEnter = value;
- }
- private Visibility _createModFileVisible = Visibility.Collapsed;
- public Visibility CreateModFileVisible
- {
- get => _createModFileVisible;
- set => SetProperty(ref _createModFileVisible, value);
- }
- public DelegateCommand OpenFileCommand { get; set; }
- public DelegateCommand<object> ChangeLocationCommand { get; set; }
- public BackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
- {
- this.eventAggregator = eventAggregator;
- BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
- ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
- InitComponent();
- BackgroundInfo.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
- BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
- InitLocationButtonMatrix();
- OpenFileCommand = new DelegateCommand(OpenFile);
- eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate);
- eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem);
- }
- public string PageRangeText { get; set; } = "0";
- private int _pageRangeSelectIndex = 0;
- public int PageRangeSelectIndex
- {
- get { return _pageRangeSelectIndex; }
- set
- {
- SetProperty(ref _pageRangeSelectIndex, value);
- EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
- }
- }
- public void InitComponent()
- {
- InitOpacityList();
- InitRotationList();
- InitRelativeRatioList();
- }
- private void InitRotationList()
- {
- OpacityList.Clear();
- for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
- {
- RotationList.Add(defaultRotation.ToString());
- }
- }
- private void InitOpacityList()
- {
- OpacityList.Clear();
- for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
- {
- OpacityList.Add(defaultOpacity.ToString() + " %");
- }
- }
- private void InitRelativeRatioList()
- {
- RelativeRatioList.Clear();
- for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
- {
- RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
- }
- }
- 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)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
- GetLocationFromNumber[Num.ToString()] = false;
- }
- public void InitComponentBySelectedInfo()
- {
- }
- public void ChangeLocation(object e)
- {
- string args = e as string;
- if (args != null)
- {
- BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
- BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- InitLocationButtonMatrix();
- }
- }
- 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 BackgroundInfo);
- }
- CreateModFileVisible = Visibility.Visible;
- eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
- }
- }
- 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);
- });
- BackgroundInfo.ImageArray = bmp_data;
- BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
- BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
- document.ReleasePages();
- document.Release();
- }
- public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
- {
- backgroundItem.type = backgroundInfo.BackgroundType;
- backgroundItem.imageArray = backgroundInfo.ImageArray;
- backgroundItem.imageWidth = backgroundInfo.ImageWidth;
- backgroundItem.imageHeight = backgroundInfo.ImageHeight;
- backgroundItem.horizalign = backgroundInfo.Horizalign;
- backgroundItem.vertalign = backgroundInfo.Vertalign;
- backgroundItem.vertOffset = backgroundInfo.VertOffset;
- backgroundItem.horizalign = backgroundInfo.Horizalign;
- backgroundItem.opacity = backgroundInfo.Opacity;
- backgroundItem.rotation = backgroundInfo.Rotation;
- backgroundItem.scale = backgroundInfo.Scale;
- // backgroundItem.pagRangeMode = backgroundInfo.PageRange;
- }
- public void SaveCurrentTemplate()
- {
- var backgroundItem = new BackgroundItem();
- ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
- backgroundItem.templateName += Settings.Default.WatermarkIndex.ToString();
- Settings.Default.BackgroundTemplateList.Add(backgroundItem);
- Settings.Default.Save();
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
- }
- public void ConfirmEditBackgroundTemplateItem(EnumColorOrFile enumColorOrFile)
- {
- if (enumColorOrFile == EnumColorOrFile.StatusFile)
- {
- var backgroundItem = new BackgroundItem();
- ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
- Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
- Settings.Default.Save();
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
- }
- }
- public void SaveBackgroundTemplate(EnumColorOrFile enumColorOrFile)
- {
- if (enumColorOrFile == EnumColorOrFile.StatusFile)
- {
- SaveCurrentTemplate();
- }
- }
- 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);
- if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
- {
- InitComponentBySelectedInfo();
- }
- InitOpacityList();
- }
- }
- }
|