BackgroundCreateFileContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 BackgroundInfo TemplateBackgroundInfo;
  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. }
  144. public string PageRangeText { get; set; } = "0";
  145. private int _pageRangeSelectIndex = 0;
  146. public int PageRangeSelectIndex
  147. {
  148. get { return _pageRangeSelectIndex; }
  149. set
  150. {
  151. SetProperty(ref _pageRangeSelectIndex, value);
  152. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  153. }
  154. }
  155. public void InitComponent()
  156. {
  157. InitOpacityList();
  158. InitRotationList();
  159. InitRelativeRatioList();
  160. }
  161. private void InitRotationList()
  162. {
  163. OpacityList.Clear();
  164. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  165. {
  166. RotationList.Add(defaultRotation.ToString());
  167. }
  168. }
  169. private void InitOpacityList()
  170. {
  171. OpacityList.Clear();
  172. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  173. {
  174. OpacityList.Add(defaultOpacity.ToString() + " %");
  175. }
  176. }
  177. private void InitRelativeRatioList()
  178. {
  179. RelativeRatioList.Clear();
  180. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  181. {
  182. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  183. }
  184. }
  185. private void InitLocationButtonMatrix()
  186. {
  187. GetLocationFromNumber.Clear();
  188. for (var temp = 0; temp <= 22; temp++)
  189. {
  190. GetLocationFromNumber.Add(temp.ToString(), true);
  191. if (temp % 10 == 2)
  192. {
  193. temp += 7;
  194. }
  195. }
  196. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  197. GetLocationFromNumber[Num.ToString()] = false;
  198. }
  199. public void InitComponentBySelectedInfo()
  200. {
  201. }
  202. public void ChangeLocation(object e)
  203. {
  204. string args = e as string;
  205. if (args != null)
  206. {
  207. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  208. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  209. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  210. InitLocationButtonMatrix();
  211. }
  212. }
  213. public void OpenFile()
  214. {
  215. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  216. dlg.Multiselect = false;
  217. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  218. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  219. {
  220. FileNameText = dlg.SafeFileName;
  221. FileInfo file = new FileInfo(dlg.FileName);
  222. if (file.Extension == ".pdf")
  223. {
  224. GetBitmapFromDocment(dlg.FileName);
  225. }
  226. else
  227. {
  228. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  229. }
  230. CreateModFileVisible = Visibility.Visible;
  231. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  232. }
  233. }
  234. public async void GetBitmapFromDocment(string filePath)
  235. {
  236. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  237. CPDFPage page = document.PageAtIndex(0);
  238. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  239. await Task.Run(delegate
  240. {
  241. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  242. });
  243. BackgroundInfo.ImageArray = bmp_data;
  244. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  245. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  246. document.ReleasePages();
  247. document.Release();
  248. }
  249. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  250. {
  251. backgroundItem.type = backgroundInfo.BackgroundType;
  252. backgroundItem.imageArray = backgroundInfo.ImageArray;
  253. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  254. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  255. backgroundItem.horizalign = backgroundInfo.Horizalign;
  256. backgroundItem.vertalign = backgroundInfo.Vertalign;
  257. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  258. backgroundItem.horizalign = backgroundInfo.Horizalign;
  259. backgroundItem.opacity = backgroundInfo.Opacity;
  260. backgroundItem.rotation = backgroundInfo.Rotation;
  261. backgroundItem.scale = backgroundInfo.Scale;
  262. backgroundItem.pageRange = backgroundInfo.PageRange;
  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 SaveBackgroundTemplate(EnumColorOrFile enumColorOrFile)
  275. {
  276. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  277. {
  278. SaveCurrentTemplate();
  279. }
  280. }
  281. public bool IsNavigationTarget(NavigationContext navigationContext)
  282. {
  283. return true;
  284. }
  285. public void OnNavigatedFrom(NavigationContext navigationContext)
  286. {
  287. }
  288. public void OnNavigatedTo(NavigationContext navigationContext)
  289. {
  290. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  291. if (navigationContext.Parameters.TryGetValue<BackgroundInfo>("BackgroundInfo", out TemplateBackgroundInfo))
  292. {
  293. InitComponentBySelectedInfo();
  294. }
  295. InitOpacityList();
  296. }
  297. }
  298. }