BackgroundCreateFileContentViewModel.cs 26 KB

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