BackgroundCreateFileContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.Drawing.Printing;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
  24. namespace PDF_Office.ViewModels.EditTools.Background
  25. {
  26. public class BackgroundCreateFileContentViewModel : BindableBase, INavigationAware
  27. {
  28. IEventAggregator eventAggregator;
  29. private BackgroundItem TemplateBackgroundItem;
  30. public BackgroundInfo BackgroundInfo = new BackgroundInfo();
  31. private CPDFViewer PDFViewer;
  32. private List<string> _rotationList = new List<string>();
  33. public List<string> RotationList
  34. {
  35. get { return _rotationList; }
  36. set { SetProperty(ref _rotationList, value); }
  37. }
  38. private List<string> _opacityList = new List<string>();
  39. public List<string> OpacityList
  40. {
  41. get { return _opacityList; }
  42. set
  43. {
  44. SetProperty(ref _opacityList, value);
  45. }
  46. }
  47. private List<string> _relativeRatioList = new List<string>();
  48. public List<string> RelativeRatioList
  49. {
  50. get { return _relativeRatioList; }
  51. set
  52. {
  53. SetProperty(ref _relativeRatioList, value);
  54. }
  55. }
  56. private int _rotationValue = 0;
  57. public int RotationValue
  58. {
  59. get { return _rotationValue; }
  60. set
  61. {
  62. SetProperty(ref _rotationValue, value);
  63. BackgroundInfo.Rotation = ((float)RotationValue / 180) * 3.1415926f;
  64. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  65. }
  66. }
  67. private int _opacityValue = 100;
  68. public int OpacityValue
  69. {
  70. get { return _opacityValue; }
  71. set
  72. {
  73. SetProperty(ref _opacityValue, value);
  74. BackgroundInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225);
  75. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  76. }
  77. }
  78. private int _relativeRatioValue = 0;
  79. public int RelativeRatioValue
  80. {
  81. get { return _relativeRatioValue; }
  82. set { SetProperty(ref _relativeRatioValue, value); }
  83. }
  84. private string _verticalOffsetValue = "0";
  85. public string VerticalOffsetValue
  86. {
  87. get { return _verticalOffsetValue; }
  88. set
  89. {
  90. SetProperty(ref _verticalOffsetValue, value);
  91. BackgroundInfo.VertOffset = float.Parse(VerticalOffsetValue);
  92. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  93. }
  94. }
  95. private string _horizOffsetValue = "0";
  96. public string HorizontalOffsetValue
  97. {
  98. get { return _horizOffsetValue; }
  99. set
  100. {
  101. SetProperty(ref _horizOffsetValue, value);
  102. BackgroundInfo.HorizOffset = float.Parse(HorizontalOffsetValue);
  103. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  104. }
  105. }
  106. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  107. public ObservableDictionary<string, bool> GetLocationFromNumber
  108. {
  109. get { return _getLocationFromNumber; }
  110. }
  111. private string _fileNameText = "";
  112. public string FileNameText
  113. {
  114. get { return _fileNameText; }
  115. set
  116. {
  117. SetProperty(ref _fileNameText, value);
  118. }
  119. }
  120. private bool isFirstEnter = true;
  121. public bool IsFirstEnter
  122. {
  123. get => isFirstEnter;
  124. set => isFirstEnter = value;
  125. }
  126. private Visibility _createModFileVisible = Visibility.Collapsed;
  127. public Visibility CreateModFileVisible
  128. {
  129. get => _createModFileVisible;
  130. set => SetProperty(ref _createModFileVisible, value);
  131. }
  132. public DelegateCommand OpenFileCommand { get; set; }
  133. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  134. public BackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
  135. {
  136. this.eventAggregator = eventAggregator;
  137. BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  138. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  139. InitComponent();
  140. BackgroundInfo.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  141. BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  142. InitLocationButtonMatrix();
  143. OpenFileCommand = new DelegateCommand(OpenFile);
  144. eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate);
  145. eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem);
  146. }
  147. public string _pageRangeText { get; set; } = "0";
  148. public string PageRangeText
  149. {
  150. get { return _pageRangeText; }
  151. set
  152. {
  153. _pageRangeText = value;
  154. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  155. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  156. }
  157. }
  158. private int _pageRangeSelectIndex = 0;
  159. public int PageRangeSelectIndex
  160. {
  161. get { return _pageRangeSelectIndex; }
  162. set
  163. {
  164. SetProperty(ref _pageRangeSelectIndex, value);
  165. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  166. }
  167. }
  168. public void InitComponent()
  169. {
  170. InitOpacityList();
  171. InitRotationList();
  172. InitRelativeRatioList();
  173. }
  174. private void InitRotationList()
  175. {
  176. OpacityList.Clear();
  177. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  178. {
  179. RotationList.Add(defaultRotation.ToString());
  180. }
  181. }
  182. private void InitOpacityList()
  183. {
  184. OpacityList.Clear();
  185. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  186. {
  187. OpacityList.Add(defaultOpacity.ToString() + " %");
  188. }
  189. }
  190. private void InitRelativeRatioList()
  191. {
  192. RelativeRatioList.Clear();
  193. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  194. {
  195. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  196. }
  197. }
  198. private void InitLocationButtonMatrix()
  199. {
  200. GetLocationFromNumber.Clear();
  201. for (var temp = 0; temp <= 22; temp++)
  202. {
  203. GetLocationFromNumber.Add(temp.ToString(), true);
  204. if (temp % 10 == 2)
  205. {
  206. temp += 7;
  207. }
  208. }
  209. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  210. GetLocationFromNumber[Num.ToString()] = false;
  211. }
  212. public void InitComponentBySelectedInfo()
  213. {
  214. }
  215. public void ChangeLocation(object e)
  216. {
  217. string args = e as string;
  218. if (args != null)
  219. {
  220. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  221. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  222. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  223. InitLocationButtonMatrix();
  224. }
  225. }
  226. public void OpenFile()
  227. {
  228. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  229. dlg.Multiselect = false;
  230. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  231. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  232. {
  233. FileNameText = dlg.SafeFileName;
  234. FileInfo file = new FileInfo(dlg.FileName);
  235. if (file.Extension == ".pdf")
  236. {
  237. GetBitmapFromDocment(dlg.FileName);
  238. }
  239. else
  240. {
  241. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  242. }
  243. CreateModFileVisible = Visibility.Visible;
  244. eventAggregator.GetEvent<SetBackgroundEvent>().Publish(BackgroundInfo);
  245. }
  246. }
  247. public async void GetBitmapFromDocment(string filePath)
  248. {
  249. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  250. CPDFPage page = document.PageAtIndex(0);
  251. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  252. await Task.Run(delegate
  253. {
  254. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  255. });
  256. BackgroundInfo.ImageArray = bmp_data;
  257. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  258. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  259. document.ReleasePages();
  260. document.Release();
  261. }
  262. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  263. {
  264. backgroundItem.type = backgroundInfo.BackgroundType;
  265. backgroundItem.imageArray = backgroundInfo.ImageArray;
  266. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  267. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  268. backgroundItem.horizalign = backgroundInfo.Horizalign;
  269. backgroundItem.vertalign = backgroundInfo.Vertalign;
  270. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  271. backgroundItem.horizalign = backgroundInfo.Horizalign;
  272. backgroundItem.opacity = backgroundInfo.Opacity;
  273. backgroundItem.rotation = backgroundInfo.Rotation;
  274. backgroundItem.scale = backgroundInfo.Scale;
  275. // backgroundItem.pagRangeMode = backgroundInfo.PageRange;
  276. }
  277. public void SaveCurrentTemplate()
  278. {
  279. var backgroundItem = new BackgroundItem();
  280. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  281. backgroundItem.templateName += Settings.Default.WatermarkIndex.ToString();
  282. try
  283. {
  284. //创建缓存文件夹
  285. string folderPath = Path.Combine(App.CurrentPath, "Background");
  286. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  287. //保险措施(猜测)
  288. if (File.Exists(folderPath))
  289. {
  290. File.Delete(folderPath);
  291. }
  292. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  293. if (!tempfolder.Exists)
  294. {
  295. tempfolder.Create();
  296. }
  297. //预览图缓存
  298. string saveName = Guid.NewGuid().ToString();
  299. string savePath = Path.Combine(folderPath, saveName);
  300. System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
  301. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  302. if (tempDocument != null)
  303. {
  304. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  305. var tempBackground = tempDocument.GetBackground();
  306. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  307. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  308. tempBackground.SetScale(1);
  309. tempBackground.SetRotation(BackgroundInfo.Rotation);
  310. tempBackground.SetOpacity(BackgroundInfo.Opacity);
  311. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  312. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  313. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  314. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  315. tempBackground.SetPages(BackgroundInfo.PageRange);
  316. tempBackground.SetAllowsPrint(true);
  317. tempBackground.SetAllowsView(true);
  318. tempBackground.Update();
  319. //获取透明背景的图片
  320. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  321. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  322. {
  323. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  324. }
  325. backgroundItem.previewImagePath = savePath;
  326. tempDocument.Release();
  327. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  328. Settings.Default.Save();
  329. }
  330. }
  331. catch
  332. {
  333. }
  334. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
  335. }
  336. public void ConfirmEditBackgroundTemplateItem(EnumColorOrFile enumColorOrFile)
  337. {
  338. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  339. {
  340. var backgroundItem = new BackgroundItem();
  341. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  342. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(EnumTemplateListOrCreate.StatusTemplate);
  343. try
  344. {
  345. //创建缓存文件夹
  346. string folderPath = Path.Combine(App.CurrentPath, "Background");
  347. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  348. //保险措施(猜测)
  349. if (File.Exists(folderPath))
  350. {
  351. File.Delete(folderPath);
  352. }
  353. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  354. if (!tempfolder.Exists)
  355. {
  356. tempfolder.Create();
  357. }
  358. //预览图缓存
  359. string saveName = Guid.NewGuid().ToString();
  360. string savePath = Path.Combine(folderPath, saveName);
  361. System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
  362. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  363. if (tempDocument != null)
  364. {
  365. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  366. var tempBackground = tempDocument.GetBackground();
  367. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  368. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  369. tempBackground.SetScale(1);
  370. tempBackground.SetRotation(BackgroundInfo.Rotation);
  371. tempBackground.SetOpacity(BackgroundInfo.Opacity);
  372. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  373. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  374. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  375. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  376. tempBackground.SetPages(BackgroundInfo.PageRange);
  377. tempBackground.SetAllowsPrint(true);
  378. tempBackground.SetAllowsView(true);
  379. tempBackground.Update();
  380. //获取透明背景的图片
  381. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  382. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  383. {
  384. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  385. }
  386. backgroundItem.previewImagePath = savePath;
  387. tempDocument.Release();
  388. Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
  389. Settings.Default.Save();
  390. }
  391. }
  392. catch
  393. {
  394. }
  395. }
  396. }
  397. public void SaveBackgroundTemplate(EnumColorOrFile enumColorOrFile)
  398. {
  399. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  400. {
  401. SaveCurrentTemplate();
  402. }
  403. }
  404. public bool IsNavigationTarget(NavigationContext navigationContext)
  405. {
  406. return true;
  407. }
  408. public void OnNavigatedFrom(NavigationContext navigationContext)
  409. {
  410. }
  411. public void OnNavigatedTo(NavigationContext navigationContext)
  412. {
  413. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  414. if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
  415. {
  416. InitComponentBySelectedInfo();
  417. }
  418. InitOpacityList();
  419. }
  420. }
  421. }