BackgroundCreateFileContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. private BackgroundItem TemplateBackgroundItem;
  28. public BackgroundInfo BackgroundInfo = new BackgroundInfo();
  29. private CPDFViewer PDFViewer;
  30. private List<string> _rotationList = new List<string>();
  31. public List<string> RotationList
  32. {
  33. get { return _rotationList; }
  34. set { SetProperty(ref _rotationList, value); }
  35. }
  36. private List<string> _opacityList = new List<string>();
  37. public List<string> OpacityList
  38. {
  39. get { return _opacityList; }
  40. set
  41. {
  42. SetProperty(ref _opacityList, value);
  43. }
  44. }
  45. private List<string> _relativeRatioList = new List<string>();
  46. public List<string> RelativeRatioList
  47. {
  48. get { return _relativeRatioList; }
  49. set
  50. {
  51. SetProperty(ref _relativeRatioList, value);
  52. }
  53. }
  54. private int _rotationValue = 0;
  55. public int RotationValue
  56. {
  57. get { return _rotationValue; }
  58. set
  59. {
  60. SetProperty(ref _rotationValue, value);
  61. BackgroundInfo.Rotation = ((float)RotationValue / 180) * 3.1415926f;
  62. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  63. }
  64. }
  65. private int _opacityValue = 100;
  66. public int OpacityValue
  67. {
  68. get { return _opacityValue; }
  69. set
  70. {
  71. SetProperty(ref _opacityValue, value);
  72. BackgroundInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225);
  73. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  74. }
  75. }
  76. private int _relativeRatioValue = 0;
  77. public int RelativeRatioValue
  78. {
  79. get { return _relativeRatioValue; }
  80. set { SetProperty(ref _relativeRatioValue, value); }
  81. }
  82. private string _verticalOffsetValue = "0";
  83. public string VerticalOffsetValue
  84. {
  85. get { return _verticalOffsetValue; }
  86. set
  87. {
  88. SetProperty(ref _verticalOffsetValue, value);
  89. BackgroundInfo.VertOffset = float.Parse(VerticalOffsetValue);
  90. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  91. }
  92. }
  93. private string _horizOffsetValue = "0";
  94. public string HorizontalOffsetValue
  95. {
  96. get { return _horizOffsetValue; }
  97. set
  98. {
  99. SetProperty(ref _horizOffsetValue, value);
  100. BackgroundInfo.HorizOffset = float.Parse(HorizontalOffsetValue);
  101. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  102. }
  103. }
  104. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  105. public ObservableDictionary<string, bool> GetLocationFromNumber
  106. {
  107. get { return _getLocationFromNumber; }
  108. }
  109. private string _fileNameText = "";
  110. public string FileNameText
  111. {
  112. get { return _fileNameText; }
  113. set
  114. {
  115. SetProperty(ref _fileNameText, value);
  116. }
  117. }
  118. private bool isFirstEnter = true;
  119. public bool IsFirstEnter
  120. {
  121. get => isFirstEnter;
  122. set => isFirstEnter = value;
  123. }
  124. private Visibility _createModFileVisible = Visibility.Collapsed;
  125. public Visibility CreateModFileVisible
  126. {
  127. get => _createModFileVisible;
  128. set => SetProperty(ref _createModFileVisible, value);
  129. }
  130. public DelegateCommand OpenFileCommand { get; set; }
  131. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  132. public BackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
  133. {
  134. this.eventAggregator = eventAggregator;
  135. BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  136. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  137. InitComponent();
  138. BackgroundInfo.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  139. BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  140. InitLocationButtonMatrix();
  141. OpenFileCommand = new DelegateCommand(OpenFile);
  142. eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate);
  143. eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem);
  144. }
  145. public string PageRangeText { get; set; } = "0";
  146. private int _pageRangeSelectIndex = 0;
  147. public int PageRangeSelectIndex
  148. {
  149. get { return _pageRangeSelectIndex; }
  150. set
  151. {
  152. SetProperty(ref _pageRangeSelectIndex, value);
  153. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  154. }
  155. }
  156. public void InitComponent()
  157. {
  158. InitOpacityList();
  159. InitRotationList();
  160. InitRelativeRatioList();
  161. }
  162. private void InitRotationList()
  163. {
  164. OpacityList.Clear();
  165. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  166. {
  167. RotationList.Add(defaultRotation.ToString());
  168. }
  169. }
  170. private void InitOpacityList()
  171. {
  172. OpacityList.Clear();
  173. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  174. {
  175. OpacityList.Add(defaultOpacity.ToString() + " %");
  176. }
  177. }
  178. private void InitRelativeRatioList()
  179. {
  180. RelativeRatioList.Clear();
  181. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  182. {
  183. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  184. }
  185. }
  186. private void InitLocationButtonMatrix()
  187. {
  188. GetLocationFromNumber.Clear();
  189. for (var temp = 0; temp <= 22; temp++)
  190. {
  191. GetLocationFromNumber.Add(temp.ToString(), true);
  192. if (temp % 10 == 2)
  193. {
  194. temp += 7;
  195. }
  196. }
  197. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  198. GetLocationFromNumber[Num.ToString()] = false;
  199. }
  200. public void InitComponentBySelectedInfo()
  201. {
  202. }
  203. public void ChangeLocation(object e)
  204. {
  205. string args = e as string;
  206. if (args != null)
  207. {
  208. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  209. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  210. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  211. InitLocationButtonMatrix();
  212. }
  213. }
  214. public void OpenFile()
  215. {
  216. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  217. dlg.Multiselect = false;
  218. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  219. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  220. {
  221. FileNameText = dlg.SafeFileName;
  222. FileInfo file = new FileInfo(dlg.FileName);
  223. if (file.Extension == ".pdf")
  224. {
  225. GetBitmapFromDocment(dlg.FileName);
  226. }
  227. else
  228. {
  229. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  230. }
  231. CreateModFileVisible = Visibility.Visible;
  232. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  233. }
  234. }
  235. public async void GetBitmapFromDocment(string filePath)
  236. {
  237. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  238. CPDFPage page = document.PageAtIndex(0);
  239. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  240. await Task.Run(delegate
  241. {
  242. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  243. });
  244. BackgroundInfo.ImageArray = bmp_data;
  245. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  246. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  247. document.ReleasePages();
  248. document.Release();
  249. }
  250. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  251. {
  252. backgroundItem.type = backgroundInfo.BackgroundType;
  253. backgroundItem.imageArray = backgroundInfo.ImageArray;
  254. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  255. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  256. backgroundItem.horizalign = backgroundInfo.Horizalign;
  257. backgroundItem.vertalign = backgroundInfo.Vertalign;
  258. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  259. backgroundItem.horizalign = backgroundInfo.Horizalign;
  260. backgroundItem.opacity = backgroundInfo.Opacity;
  261. backgroundItem.rotation = backgroundInfo.Rotation;
  262. backgroundItem.scale = backgroundInfo.Scale;
  263. // backgroundItem.pagRangeMode = backgroundInfo.PageRange;
  264. }
  265. public void SaveCurrentTemplate()
  266. {
  267. var backgroundItem = new BackgroundItem();
  268. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  269. backgroundItem.templateName += Settings.Default.WatermarkIndex.ToString();
  270. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  271. Settings.Default.Save();
  272. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
  273. }
  274. public void ConfirmEditBackgroundTemplateItem(EnumColorOrFile enumColorOrFile)
  275. {
  276. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  277. {
  278. var backgroundItem = new BackgroundItem();
  279. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  280. Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
  281. Settings.Default.Save();
  282. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
  283. }
  284. }
  285. public void SaveBackgroundTemplate(EnumColorOrFile enumColorOrFile)
  286. {
  287. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  288. {
  289. SaveCurrentTemplate();
  290. }
  291. }
  292. public bool IsNavigationTarget(NavigationContext navigationContext)
  293. {
  294. return true;
  295. }
  296. public void OnNavigatedFrom(NavigationContext navigationContext)
  297. {
  298. }
  299. public void OnNavigatedTo(NavigationContext navigationContext)
  300. {
  301. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  302. if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
  303. {
  304. InitComponentBySelectedInfo();
  305. }
  306. InitOpacityList();
  307. }
  308. }
  309. }