BackgroundCreateFileContentViewModel.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  108. BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  109. InitLocationButtonMatrix();
  110. OpenFileCommand = new DelegateCommand(OpenFile);
  111. eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate);
  112. }
  113. public string PageRangeText { get; set; } = "0";
  114. private int _pageRangeSelectIndex = 0;
  115. public int PageRangeSelectIndex
  116. {
  117. get { return _pageRangeSelectIndex; }
  118. set
  119. {
  120. SetProperty(ref _pageRangeSelectIndex, value);
  121. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  122. }
  123. }
  124. public void InitComponent()
  125. {
  126. InitOpacityList();
  127. InitRotationList();
  128. InitRelativeRatioList();
  129. }
  130. private void InitRotationList()
  131. {
  132. OpacityList.Clear();
  133. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  134. {
  135. RotationList.Add(defaultRotation.ToString());
  136. }
  137. }
  138. private void InitOpacityList()
  139. {
  140. OpacityList.Clear();
  141. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  142. {
  143. OpacityList.Add(defaultOpacity.ToString() + " %");
  144. }
  145. }
  146. private void InitRelativeRatioList()
  147. {
  148. RelativeRatioList.Clear();
  149. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  150. {
  151. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  152. }
  153. }
  154. private void InitLocationButtonMatrix()
  155. {
  156. GetLocationFromNumber.Clear();
  157. for (var temp = 0; temp <= 22; temp++)
  158. {
  159. GetLocationFromNumber.Add(temp.ToString(), true);
  160. if (temp % 10 == 2)
  161. {
  162. temp += 7;
  163. }
  164. }
  165. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  166. GetLocationFromNumber[Num.ToString()] = false;
  167. }
  168. public void ChangeLocation(object e)
  169. {
  170. string args = e as string;
  171. if (args != null)
  172. {
  173. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  174. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  175. InitLocationButtonMatrix();
  176. }
  177. }
  178. public void OpenFile()
  179. {
  180. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  181. dlg.Multiselect = false;
  182. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  183. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  184. {
  185. FileNameText = dlg.SafeFileName;
  186. FileInfo file = new FileInfo(dlg.FileName);
  187. if (file.Extension == ".pdf")
  188. {
  189. GetBitmapFromDocment(dlg.FileName);
  190. }
  191. else
  192. {
  193. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  194. }
  195. CreateModFileVisible = Visibility.Visible;
  196. }
  197. }
  198. public async void GetBitmapFromDocment(string filePath)
  199. {
  200. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  201. CPDFPage page = document.PageAtIndex(0);
  202. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  203. await Task.Run(delegate
  204. {
  205. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  206. });
  207. BackgroundInfo.ImageArray = bmp_data;
  208. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  209. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  210. document.ReleasePages();
  211. document.Release();
  212. }
  213. public void SaveCurrentTemplate()
  214. {
  215. var backgroundItem = new BackgroundItem();
  216. backgroundItem.pageRange = "0";
  217. backgroundItem.type = ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE;
  218. backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString();
  219. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  220. Settings.Default.Save();
  221. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
  222. }
  223. public void SaveBackgroundTemplate(EnumColorOrFile enumColorOrFile)
  224. {
  225. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  226. {
  227. SaveCurrentTemplate();
  228. }
  229. }
  230. public bool IsNavigationTarget(NavigationContext navigationContext)
  231. {
  232. return true;
  233. }
  234. public void OnNavigatedFrom(NavigationContext navigationContext)
  235. {
  236. }
  237. public void OnNavigatedTo(NavigationContext navigationContext)
  238. {
  239. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  240. InitOpacityList();
  241. }
  242. }
  243. }