HomePageBackgroundCreateFileContentViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model.EditTools.Background;
  7. using PDF_Master.Model;
  8. using PDF_Master.Properties;
  9. using PDFSettings;
  10. using Prism.Commands;
  11. using Prism.Events;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Windows;
  19. using System.Threading.Tasks;
  20. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageBackground
  21. {
  22. public class HomePageBackgroundCreateFileContentViewModel : BindableBase, INavigationAware
  23. {
  24. IEventAggregator eventAggregator;
  25. private BackgroundItem TemplateBackgroundItem;
  26. public BackgroundInfo BackgroundInfo = new BackgroundInfo();
  27. private CPDFViewer PDFViewer;
  28. private List<string> _rotationList = new List<string>();
  29. public List<string> RotationList
  30. {
  31. get { return _rotationList; }
  32. set { SetProperty(ref _rotationList, value); }
  33. }
  34. private List<string> _opacityList = new List<string>();
  35. public List<string> OpacityList
  36. {
  37. get { return _opacityList; }
  38. set
  39. {
  40. SetProperty(ref _opacityList, value);
  41. }
  42. }
  43. private List<string> _relativeRatioList = new List<string>();
  44. public List<string> RelativeRatioList
  45. {
  46. get { return _relativeRatioList; }
  47. set
  48. {
  49. SetProperty(ref _relativeRatioList, value);
  50. }
  51. }
  52. private int _rotationValue = 0;
  53. public int RotationValue
  54. {
  55. get { return _rotationValue; }
  56. set
  57. {
  58. SetProperty(ref _rotationValue, value);
  59. BackgroundInfo.Rotation = value;
  60. }
  61. }
  62. private int _opacityValue = 100;
  63. public int OpacityValue
  64. {
  65. get { return _opacityValue; }
  66. set
  67. {
  68. SetProperty(ref _opacityValue, value);
  69. BackgroundInfo.Opacity = (byte)(value);
  70. }
  71. }
  72. private int _relativeRatioValue = 50;
  73. public int RelativeRatioValue
  74. {
  75. get { return _relativeRatioValue; }
  76. set
  77. {
  78. SetProperty(ref _rotationValue, value);
  79. BackgroundInfo.Scale = value;
  80. }
  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. }
  91. }
  92. private string _relativeScaleText = "50 %";
  93. public string RelativeScaleText
  94. {
  95. get { return _relativeScaleText; }
  96. set
  97. {
  98. SetProperty(ref _relativeScaleText, value);
  99. }
  100. }
  101. private string _horizOffsetValue = "0";
  102. public string HorizontalOffsetValue
  103. {
  104. get { return _horizOffsetValue; }
  105. set
  106. {
  107. SetProperty(ref _horizOffsetValue, value);
  108. BackgroundInfo.HorizOffset = float.Parse(HorizontalOffsetValue);
  109. }
  110. }
  111. private string _rotationText = "0";
  112. public string RotationText
  113. {
  114. get { return _rotationText; }
  115. set
  116. {
  117. SetProperty(ref _rotationText, value);
  118. }
  119. }
  120. private string _opacityText = "100 %";
  121. public string OpacityText
  122. {
  123. get { return _opacityText; }
  124. set
  125. {
  126. SetProperty(ref _opacityText, value);
  127. }
  128. }
  129. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  130. public ObservableDictionary<string, bool> GetLocationFromNumber
  131. {
  132. get { return _getLocationFromNumber; }
  133. }
  134. private string _fileNameText = "";
  135. public string FileNameText
  136. {
  137. get { return _fileNameText; }
  138. set
  139. {
  140. SetProperty(ref _fileNameText, value);
  141. }
  142. }
  143. private bool isFirstEnter = true;
  144. public bool IsFirstEnter
  145. {
  146. get => isFirstEnter;
  147. set => isFirstEnter = value;
  148. }
  149. private Visibility _createModFileVisible = Visibility.Collapsed;
  150. public Visibility CreateModFileVisible
  151. {
  152. get => _createModFileVisible;
  153. set => SetProperty(ref _createModFileVisible, value);
  154. }
  155. private bool _isRelativeScaleValue = false;
  156. public bool IsRelativeScaleValue
  157. {
  158. get { return _isRelativeScaleValue; }
  159. set
  160. {
  161. SetProperty(ref _isRelativeScaleValue, value);
  162. BackgroundInfo.IsRelativeScale = IsRelativeScaleValue;
  163. }
  164. }
  165. public DelegateCommand OpenFileCommand { get; set; }
  166. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  167. public string Unicode = null;
  168. public HomePageBackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
  169. {
  170. this.eventAggregator = eventAggregator;
  171. BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  172. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  173. InitComponent();
  174. BackgroundInfo.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  175. BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  176. InitLocationButtonMatrix();
  177. OpenFileCommand = new DelegateCommand(OpenFile);
  178. eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate, e => e.Unicode == Unicode);
  179. eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem, e => e.Unicode == Unicode);
  180. }
  181. public string _pageRangeText { get; set; } = "0";
  182. public string PageRangeText
  183. {
  184. get { return _pageRangeText; }
  185. set
  186. {
  187. _pageRangeText = value;
  188. BackgroundInfo.PageRange = PageRangeText;
  189. }
  190. }
  191. private int _pageRangeSelectIndex = 0;
  192. public int PageRangeSelectIndex
  193. {
  194. get { return _pageRangeSelectIndex; }
  195. set
  196. {
  197. SetProperty(ref _pageRangeSelectIndex, value);
  198. BackgroundInfo.PageRangeIndex = PageRangeSelectIndex;
  199. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  200. }
  201. }
  202. public void InitComponent()
  203. {
  204. InitOpacityList();
  205. InitRotationList();
  206. InitRelativeRatioList();
  207. }
  208. private void InitRotationList()
  209. {
  210. RotationList.Clear();
  211. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  212. {
  213. RotationList.Add(defaultRotation.ToString());
  214. }
  215. }
  216. private void InitOpacityList()
  217. {
  218. OpacityList.Clear();
  219. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  220. {
  221. OpacityList.Add(defaultOpacity.ToString() + " %");
  222. }
  223. }
  224. private void InitRelativeRatioList()
  225. {
  226. RelativeRatioList.Clear();
  227. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  228. {
  229. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  230. }
  231. }
  232. private void InitLocationButtonMatrix()
  233. {
  234. GetLocationFromNumber.Clear();
  235. for (var temp = 0; temp <= 22; temp++)
  236. {
  237. GetLocationFromNumber.Add(temp.ToString(), true);
  238. if (temp % 10 == 2)
  239. {
  240. temp += 7;
  241. }
  242. }
  243. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  244. GetLocationFromNumber[Num.ToString()] = false;
  245. }
  246. public void InitComponentBySelectedInfo()
  247. {
  248. ConvertItemToInfo(TemplateBackgroundItem, ref BackgroundInfo);
  249. RotationText = BackgroundInfo.Rotation.ToString();
  250. OpacityText = BackgroundInfo.Opacity.ToString();
  251. InitLocationButtonMatrix();
  252. IsRelativeScaleValue = BackgroundInfo.IsRelativeScale;
  253. RelativeScaleText = BackgroundInfo.Scale.ToString();
  254. HorizontalOffsetValue = (BackgroundInfo.HorizOffset).ToString();
  255. VerticalOffsetValue = (BackgroundInfo.VertOffset).ToString();
  256. PageRangeSelectIndex = BackgroundInfo.PageRangeIndex;
  257. }
  258. public void ChangeLocation(object e)
  259. {
  260. string args = e as string;
  261. if (args != null)
  262. {
  263. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  264. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  265. InitLocationButtonMatrix();
  266. }
  267. }
  268. public void OpenFile()
  269. {
  270. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  271. dlg.Multiselect = false;
  272. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  273. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  274. {
  275. FileNameText = dlg.SafeFileName;
  276. FileInfo file = new FileInfo(dlg.FileName);
  277. if (file.Extension == ".pdf")
  278. {
  279. GetBitmapFromDocment(null,dlg.FileName);
  280. }
  281. else
  282. {
  283. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  284. }
  285. CreateModFileVisible = Visibility.Visible;
  286. }
  287. }
  288. public async void GetBitmapFromDocment(CPDFDocument document = null, string filePath = "")
  289. {
  290. if (document == null)
  291. {
  292. document = CPDFDocument.InitWithFilePath(filePath);
  293. }
  294. CPDFPage page = document.PageAtIndex(0);
  295. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  296. await Task.Run(delegate
  297. {
  298. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  299. });
  300. BackgroundInfo.ImageArray = bmp_data;
  301. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  302. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  303. document.ReleasePages();
  304. document.Release();
  305. }
  306. public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
  307. {
  308. if (backgroundItem != null)
  309. {
  310. backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  311. backgroundInfo.Color = backgroundItem.bgColor;
  312. backgroundInfo.Horizalign = backgroundItem.horizalign;
  313. backgroundInfo.Vertalign = backgroundItem.vertalign;
  314. backgroundInfo.VertOffset = backgroundItem.vertOffset;
  315. backgroundInfo.Horizalign = backgroundItem.horizalign;
  316. backgroundInfo.Opacity = backgroundItem.opacity;
  317. backgroundInfo.Rotation = backgroundItem.rotation;
  318. backgroundInfo.IsRelativeScale = backgroundItem.isRelativeScale;
  319. backgroundInfo.Scale = backgroundItem.scale;
  320. backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex;
  321. backgroundInfo.PageRange = backgroundItem.pageRange;
  322. }
  323. }
  324. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  325. {
  326. backgroundItem.type = C_Background_Type.BG_TYPE_IMAGE;
  327. backgroundItem.imageArray = backgroundInfo.ImageArray;
  328. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  329. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  330. backgroundItem.horizalign = backgroundInfo.Horizalign;
  331. backgroundItem.vertalign = backgroundInfo.Vertalign;
  332. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  333. backgroundItem.horizalign = backgroundInfo.Horizalign;
  334. backgroundItem.opacity = backgroundInfo.Opacity;
  335. backgroundItem.rotation = backgroundInfo.Rotation;
  336. backgroundItem.isRelativeScale = backgroundInfo.IsRelativeScale;
  337. backgroundItem.scale = backgroundInfo.Scale;
  338. backgroundItem.pageRange = backgroundInfo.PageRange;
  339. List<int> PageIndexLists = new List<int>();
  340. if (PageRangeSelectIndex == 3 && !CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, 99999, new char[] { ',' }, new char[] { '-' }))
  341. {
  342. backgroundInfo.PageRangeIndex = 0;
  343. }
  344. backgroundItem.PageRangeIndex = backgroundInfo.PageRangeIndex;
  345. //backgroundItem.pagRangeMode = backgroundInfo.PageRange;
  346. }
  347. public void SaveCurrentTemplate()
  348. {
  349. var backgroundItem = new BackgroundItem();
  350. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  351. backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString();
  352. try
  353. {
  354. //创建缓存文件夹
  355. string folderPath = Path.Combine(App.CurrentPath, "Background");
  356. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  357. //保险措施(猜测)
  358. if (File.Exists(folderPath))
  359. {
  360. File.Delete(folderPath);
  361. }
  362. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  363. if (!tempfolder.Exists)
  364. {
  365. tempfolder.Create();
  366. }
  367. //预览图缓存
  368. string saveName = Guid.NewGuid().ToString();
  369. string savePath = Path.Combine(folderPath, saveName);
  370. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  371. if (tempDocument != null)
  372. {
  373. tempDocument.InsertPage(0, 595, 842, null);
  374. var tempBackground = tempDocument.GetBackground();
  375. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  376. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  377. if (!BackgroundInfo.IsRelativeScale)
  378. {
  379. tempBackground.SetScale(1);
  380. }
  381. else
  382. {
  383. tempBackground.SetScale(BackgroundInfo.Scale / 100);
  384. }
  385. tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
  386. tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
  387. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  388. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  389. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  390. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  391. tempBackground.SetPages("0");
  392. tempBackground.SetAllowsPrint(true);
  393. tempBackground.SetAllowsView(true);
  394. tempBackground.Update();
  395. //获取透明背景的图片
  396. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, 595, 842, 0, false, false, 0x00FFFFFF);
  397. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  398. {
  399. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  400. }
  401. backgroundItem.previewImagePath = savePath;
  402. tempDocument.Release();
  403. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  404. Settings.Default.Save();
  405. }
  406. }
  407. catch
  408. {
  409. }
  410. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  411. {
  412. Unicode = Unicode,
  413. Status = EnumTemplateListOrCreate.StatusTemplate
  414. });
  415. }
  416. public void ConfirmEditBackgroundTemplateItem(EnumColorOrFileUnicode enumColorOrFileunicode)
  417. {
  418. EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
  419. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  420. {
  421. var backgroundItem = new BackgroundItem();
  422. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  423. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusTemplate });
  424. try
  425. {
  426. //创建缓存文件夹
  427. string folderPath = Path.Combine(App.CurrentPath, "Background");
  428. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  429. //保险措施(猜测)
  430. if (File.Exists(folderPath))
  431. {
  432. File.Delete(folderPath);
  433. }
  434. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  435. if (!tempfolder.Exists)
  436. {
  437. tempfolder.Create();
  438. }
  439. //预览图缓存
  440. string saveName = Guid.NewGuid().ToString();
  441. string savePath = Path.Combine(folderPath, saveName);
  442. System.Windows.Size pageSize = new Size { Width = 128, Height = 160 };
  443. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  444. if (tempDocument != null)
  445. {
  446. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  447. var tempBackground = tempDocument.GetBackground();
  448. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  449. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  450. if (!BackgroundInfo.IsRelativeScale)
  451. {
  452. tempBackground.SetScale(1);
  453. }
  454. else
  455. {
  456. tempBackground.SetScale(BackgroundInfo.Scale / 100);
  457. }
  458. tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
  459. tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
  460. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  461. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  462. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  463. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  464. tempBackground.SetPages("0");
  465. tempBackground.SetAllowsPrint(true);
  466. tempBackground.SetAllowsView(true);
  467. tempBackground.Update();
  468. //获取透明背景的图片
  469. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  470. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  471. {
  472. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  473. }
  474. backgroundItem.previewImagePath = savePath;
  475. tempDocument.Release();
  476. Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
  477. Settings.Default.Save();
  478. }
  479. }
  480. catch
  481. {
  482. }
  483. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  484. {
  485. Unicode = Unicode,
  486. Status = EnumTemplateListOrCreate.StatusTemplate
  487. });
  488. }
  489. }
  490. public void SaveBackgroundTemplate(EnumColorOrFileUnicode enumColorOrFileunicode)
  491. {
  492. EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
  493. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  494. {
  495. SaveCurrentTemplate();
  496. }
  497. }
  498. public bool IsNavigationTarget(NavigationContext navigationContext)
  499. {
  500. return true;
  501. }
  502. public void OnNavigatedFrom(NavigationContext navigationContext)
  503. {
  504. }
  505. public void OnNavigatedTo(NavigationContext navigationContext)
  506. {
  507. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  508. navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
  509. if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
  510. {
  511. InitComponentBySelectedInfo();
  512. }
  513. }
  514. }
  515. }