BackgroundCreateFileContentViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.EditTools.Background;
  8. using PDF_Office.Model.EditTools.Watermark;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. namespace PDF_Office.ViewModels.EditTools.Background
  21. {
  22. public class BackgroundCreateFileContentViewModel : BindableBase, INavigationAware
  23. {
  24. IEventAggregator eventAggregator;
  25. public BackgroundInfo BackgroundInfo = new BackgroundInfo();
  26. private CPDFViewer PDFViewer;
  27. private List<string> _rotationList = new List<string>();
  28. public List<string> RotationList
  29. {
  30. get { return _rotationList; }
  31. set { SetProperty(ref _rotationList, value); }
  32. }
  33. private List<string> _opacityList = new List<string>();
  34. public List<string> OpacityList
  35. {
  36. get { return _opacityList; }
  37. set
  38. {
  39. SetProperty(ref _opacityList, value);
  40. }
  41. }
  42. private List<string> _relativeRatioList = new List<string>();
  43. public List<string> RelativeRatioList
  44. {
  45. get { return _relativeRatioList; }
  46. set
  47. {
  48. SetProperty(ref _relativeRatioList, value);
  49. }
  50. }
  51. private int _rotationNumber = 0;
  52. public int RotationNumber
  53. {
  54. get { return _rotationNumber; }
  55. set
  56. {
  57. SetProperty(ref _rotationNumber, value);
  58. }
  59. }
  60. private int _opacityNumber = 100;
  61. public int OpacityNumber
  62. {
  63. get { return _opacityNumber; }
  64. set { SetProperty(ref _opacityNumber, value); }
  65. }
  66. private int _relativeRatioNumber = 0;
  67. public int RelativeRatioNumber
  68. {
  69. get { return _relativeRatioNumber; }
  70. set { SetProperty(ref _rotationNumber, value); }
  71. }
  72. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  73. public ObservableDictionary<string, bool> GetLocationFromNumber
  74. {
  75. get { return _getLocationFromNumber; }
  76. }
  77. private string _fileNameText = "";
  78. public string FileNameText
  79. {
  80. get { return _fileNameText; }
  81. set
  82. {
  83. SetProperty(ref _fileNameText, value);
  84. }
  85. }
  86. private bool isFirstEnter = true;
  87. public bool IsFirstEnter
  88. {
  89. get => isFirstEnter;
  90. set => isFirstEnter = value;
  91. }
  92. private Visibility _createModFileVisible = Visibility.Collapsed;
  93. public Visibility CreateModFileVisible
  94. {
  95. get => _createModFileVisible;
  96. set=>SetProperty(ref _createModFileVisible, value);
  97. }
  98. public DelegateCommand OpenFileCommand { get; set; }
  99. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  100. public BackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
  101. {
  102. this.eventAggregator = eventAggregator;
  103. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  104. InitComponent();
  105. BackgroundInfo.BackgroundHorizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  106. BackgroundInfo.BackgroundVertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  107. InitLocationButtonMatrix();
  108. OpenFileCommand = new DelegateCommand(OpenFile);
  109. eventAggregator.GetEvent<ConfirmEditToolsBackgroundEvent>().Subscribe(ConfirmEditTools);
  110. }
  111. public string PageRangeText { get; set; } = "0";
  112. private int _pageRangeSelectIndex = 0;
  113. public int PageRangeSelectIndex
  114. {
  115. get { return _pageRangeSelectIndex; }
  116. set
  117. {
  118. SetProperty(ref _pageRangeSelectIndex, value);
  119. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  120. }
  121. }
  122. public void InitComponent()
  123. {
  124. InitOpacityList();
  125. InitRotationList();
  126. InitRelativeRatioList();
  127. }
  128. private void InitRotationList()
  129. {
  130. OpacityList.Clear();
  131. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  132. {
  133. RotationList.Add(defaultRotation.ToString());
  134. }
  135. }
  136. private void InitOpacityList()
  137. {
  138. OpacityList.Clear();
  139. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  140. {
  141. OpacityList.Add(defaultOpacity.ToString() + " %");
  142. }
  143. }
  144. private void InitRelativeRatioList()
  145. {
  146. RelativeRatioList.Clear();
  147. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  148. {
  149. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  150. }
  151. }
  152. private void InitLocationButtonMatrix()
  153. {
  154. GetLocationFromNumber.Clear();
  155. for (var temp = 0; temp <= 22; temp++)
  156. {
  157. GetLocationFromNumber.Add(temp.ToString(), true);
  158. if (temp % 10 == 2)
  159. {
  160. temp += 7;
  161. }
  162. }
  163. int Num = (int)BackgroundInfo.BackgroundVertalign * 10 + (int)BackgroundInfo.BackgroundHorizalign;
  164. GetLocationFromNumber[Num.ToString()] = false;
  165. }
  166. public void ConfirmEditTools(EnumColorOrFile enumColorOrFile)
  167. {
  168. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  169. {
  170. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  171. }
  172. }
  173. public void ChangeLocation(object e)
  174. {
  175. string args = e as string;
  176. if (args != null)
  177. {
  178. BackgroundInfo.BackgroundVertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  179. BackgroundInfo.BackgroundHorizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  180. InitLocationButtonMatrix();
  181. }
  182. }
  183. public void OpenFile()
  184. {
  185. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  186. dlg.Multiselect = false;
  187. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  188. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  189. {
  190. FileNameText = dlg.SafeFileName;
  191. FileInfo file = new FileInfo(dlg.FileName);
  192. if (file.Extension == ".pdf")
  193. {
  194. GetBitmapFromDocment(dlg.FileName);
  195. }
  196. else
  197. {
  198. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  199. }
  200. CreateModFileVisible = Visibility.Visible;
  201. }
  202. }
  203. public async void GetBitmapFromDocment(string filePath)
  204. {
  205. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  206. CPDFPage page = document.PageAtIndex(0);
  207. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  208. await Task.Run(delegate
  209. {
  210. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  211. });
  212. BackgroundInfo.ImageArray = bmp_data;
  213. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  214. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  215. document.ReleasePages();
  216. document.Release();
  217. }
  218. public bool IsNavigationTarget(NavigationContext navigationContext)
  219. {
  220. return true;
  221. }
  222. public void OnNavigatedFrom(NavigationContext navigationContext)
  223. {
  224. }
  225. public void OnNavigatedTo(NavigationContext navigationContext)
  226. {
  227. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  228. InitOpacityList();
  229. }
  230. }
  231. }