123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.EventAggregators;
- using PDF_Master.Helper;
- using PDF_Master.Model.EditTools.Background;
- using PDF_Master.Model;
- using PDF_Master.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.Windows;
- using System.Threading.Tasks;
- namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageBackground
- {
- public class HomePageBackgroundCreateFileContentViewModel : 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 = value;
-
- }
- }
- private int _opacityValue = 100;
- public int OpacityValue
- {
- get { return _opacityValue; }
- set
- {
- SetProperty(ref _opacityValue, value);
- BackgroundInfo.Opacity = (byte)(value);
-
- }
- }
- private int _relativeRatioValue = 50;
- public int RelativeRatioValue
- {
- get { return _relativeRatioValue; }
- set
- {
- SetProperty(ref _rotationValue, value);
- BackgroundInfo.Scale = value;
-
- }
- }
- private string _verticalOffsetValue = "0";
- public string VerticalOffsetValue
- {
- get { return _verticalOffsetValue; }
- set
- {
- SetProperty(ref _verticalOffsetValue, value);
- BackgroundInfo.VertOffset = float.Parse(VerticalOffsetValue);
-
- }
- }
- private string _relativeScaleText = "50 %";
- public string RelativeScaleText
- {
- get { return _relativeScaleText; }
- set
- {
- SetProperty(ref _relativeScaleText, value);
-
- }
- }
- private string _horizOffsetValue = "0";
- public string HorizontalOffsetValue
- {
- get { return _horizOffsetValue; }
- set
- {
- SetProperty(ref _horizOffsetValue, value);
- BackgroundInfo.HorizOffset = float.Parse(HorizontalOffsetValue);
-
- }
- }
- private string _rotationText = "0";
- public string RotationText
- {
- get { return _rotationText; }
- set
- {
- SetProperty(ref _rotationText, value);
- }
- }
- private string _opacityText = "100 %";
- public string OpacityText
- {
- get { return _opacityText; }
- set
- {
- SetProperty(ref _opacityText, value);
- }
- }
- 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);
- }
- private bool _isRelativeScaleValue = false;
- public bool IsRelativeScaleValue
- {
- get { return _isRelativeScaleValue; }
- set
- {
- SetProperty(ref _isRelativeScaleValue, value);
- BackgroundInfo.IsRelativeScale = IsRelativeScaleValue;
-
- }
- }
- public DelegateCommand OpenFileCommand { get; set; }
- public DelegateCommand<object> ChangeLocationCommand { get; set; }
- public string Unicode = null;
- public HomePageBackgroundCreateFileContentViewModel(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, e => e.Unicode == Unicode);
- eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem, e => e.Unicode == Unicode);
- }
- public string _pageRangeText { get; set; } = "0";
- public string PageRangeText
- {
- get { return _pageRangeText; }
- set
- {
- _pageRangeText = value;
- BackgroundInfo.PageRange = PageRangeText;
-
- }
- }
- private int _pageRangeSelectIndex = 0;
- public int PageRangeSelectIndex
- {
- get { return _pageRangeSelectIndex; }
- set
- {
- SetProperty(ref _pageRangeSelectIndex, value);
- BackgroundInfo.PageRangeIndex = PageRangeSelectIndex;
- //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
- }
- }
- public void InitComponent()
- {
- InitOpacityList();
- InitRotationList();
- InitRelativeRatioList();
- }
- private void InitRotationList()
- {
- RotationList.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()
- {
- ConvertItemToInfo(TemplateBackgroundItem, ref BackgroundInfo);
- RotationText = BackgroundInfo.Rotation.ToString();
- OpacityText = BackgroundInfo.Opacity.ToString();
- InitLocationButtonMatrix();
- IsRelativeScaleValue = BackgroundInfo.IsRelativeScale;
- RelativeScaleText = BackgroundInfo.Scale.ToString();
- HorizontalOffsetValue = (BackgroundInfo.HorizOffset).ToString();
- VerticalOffsetValue = (BackgroundInfo.VertOffset).ToString();
- PageRangeSelectIndex = BackgroundInfo.PageRangeIndex;
- }
- 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);
-
- 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(null,dlg.FileName);
- }
- else
- {
- EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
- }
- CreateModFileVisible = Visibility.Visible;
-
- }
- }
- public async void GetBitmapFromDocment(CPDFDocument document = null, string filePath = "")
- {
- if (document == null)
- {
- 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 ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
- {
- if (backgroundItem != null)
- {
- backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
- backgroundInfo.Color = backgroundItem.bgColor;
- backgroundInfo.Horizalign = backgroundItem.horizalign;
- backgroundInfo.Vertalign = backgroundItem.vertalign;
- backgroundInfo.VertOffset = backgroundItem.vertOffset;
- backgroundInfo.Horizalign = backgroundItem.horizalign;
- backgroundInfo.Opacity = backgroundItem.opacity;
- backgroundInfo.Rotation = backgroundItem.rotation;
- backgroundInfo.IsRelativeScale = backgroundItem.isRelativeScale;
- backgroundInfo.Scale = backgroundItem.scale;
- backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex;
- backgroundInfo.PageRange = backgroundItem.pageRange;
- }
- }
- public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
- {
- backgroundItem.type = C_Background_Type.BG_TYPE_IMAGE;
- 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.isRelativeScale = backgroundInfo.IsRelativeScale;
- backgroundItem.scale = backgroundInfo.Scale;
- backgroundItem.pageRange = backgroundInfo.PageRange;
- List<int> PageIndexLists = new List<int>();
- if (PageRangeSelectIndex == 3 && !CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, 99999, new char[] { ',' }, new char[] { '-' }))
- {
- backgroundInfo.PageRangeIndex = 0;
- }
- backgroundItem.PageRangeIndex = backgroundInfo.PageRangeIndex;
- //backgroundItem.pagRangeMode = backgroundInfo.PageRange;
- }
- public void SaveCurrentTemplate()
- {
- var backgroundItem = new BackgroundItem();
- ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
- backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString();
- try
- {
- //创建缓存文件夹
- string folderPath = Path.Combine(App.CurrentPath, "Background");
- //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
- //保险措施(猜测)
- if (File.Exists(folderPath))
- {
- File.Delete(folderPath);
- }
- DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
- if (!tempfolder.Exists)
- {
- tempfolder.Create();
- }
- //预览图缓存
- string saveName = Guid.NewGuid().ToString();
- string savePath = Path.Combine(folderPath, saveName);
- System.Windows.Size pageSize =new System.Windows.Size() ;
- CPDFDocument tempDocument = CPDFDocument.CreateDocument();
- if (tempDocument != null)
- {
- tempDocument.InsertPage(0, 595, 842, null);
- var tempBackground = tempDocument.GetBackground();
- tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
- tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
- if (!BackgroundInfo.IsRelativeScale)
- {
- tempBackground.SetScale(1);
- }
- else
- {
- tempBackground.SetScale(BackgroundInfo.Scale / 100);
- }
- tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
- tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
- tempBackground.SetVertalign(BackgroundInfo.Vertalign);
- tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
- tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
- tempBackground.SetYOffset(BackgroundInfo.VertOffset);
- tempBackground.SetPages("0");
- tempBackground.SetAllowsPrint(true);
- tempBackground.SetAllowsView(true);
- tempBackground.Update();
- //获取透明背景的图片
- var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, 595, 842, 0, false, false, 0x00FFFFFF);
- using (FileStream stream = new FileStream(savePath, FileMode.Create))
- {
- bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- }
- backgroundItem.previewImagePath = savePath;
- tempDocument.Release();
- Settings.Default.BackgroundTemplateList.Add(backgroundItem);
- Settings.Default.Save();
- }
- }
- catch
- {
- }
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
- {
- Unicode = Unicode,
- Status = EnumTemplateListOrCreate.StatusTemplate
- });
- }
- public void ConfirmEditBackgroundTemplateItem(EnumColorOrFileUnicode enumColorOrFileunicode)
- {
- EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
- if (enumColorOrFile == EnumColorOrFile.StatusFile)
- {
- var backgroundItem = new BackgroundItem();
- ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusTemplate });
- try
- {
- //创建缓存文件夹
- string folderPath = Path.Combine(App.CurrentPath, "Background");
- //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
- //保险措施(猜测)
- if (File.Exists(folderPath))
- {
- File.Delete(folderPath);
- }
- DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
- if (!tempfolder.Exists)
- {
- tempfolder.Create();
- }
- //预览图缓存
- string saveName = Guid.NewGuid().ToString();
- string savePath = Path.Combine(folderPath, saveName);
- System.Windows.Size pageSize = new Size { Width = 128, Height = 160 };
- CPDFDocument tempDocument = CPDFDocument.CreateDocument();
- if (tempDocument != null)
- {
- tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
- var tempBackground = tempDocument.GetBackground();
- tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
- tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
- if (!BackgroundInfo.IsRelativeScale)
- {
- tempBackground.SetScale(1);
- }
- else
- {
- tempBackground.SetScale(BackgroundInfo.Scale / 100);
- }
- tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
- tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
- tempBackground.SetVertalign(BackgroundInfo.Vertalign);
- tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
- tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
- tempBackground.SetYOffset(BackgroundInfo.VertOffset);
- tempBackground.SetPages("0");
- tempBackground.SetAllowsPrint(true);
- tempBackground.SetAllowsView(true);
- tempBackground.Update();
- //获取透明背景的图片
- var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
- using (FileStream stream = new FileStream(savePath, FileMode.Create))
- {
- bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- }
- backgroundItem.previewImagePath = savePath;
- tempDocument.Release();
- Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
- Settings.Default.Save();
- }
- }
- catch
- {
- }
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
- {
- Unicode = Unicode,
- Status = EnumTemplateListOrCreate.StatusTemplate
- });
- }
- }
- public void SaveBackgroundTemplate(EnumColorOrFileUnicode enumColorOrFileunicode)
- {
- EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
- 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);
- navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
- if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
- {
- InitComponentBySelectedInfo();
- }
- }
- }
- }
|