BackgroundCreateFileContentViewModel.cs 25 KB

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