BackgroundCreateFileContentViewModel.cs 9.4 KB

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