HomePageBackgroundCreateFileContentViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. System.Windows.Size pageSize =new System.Windows.Size() ;
  371. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  372. if (tempDocument != null)
  373. {
  374. tempDocument.InsertPage(0, 595, 842, null);
  375. var tempBackground = tempDocument.GetBackground();
  376. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  377. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  378. if (!BackgroundInfo.IsRelativeScale)
  379. {
  380. tempBackground.SetScale(1);
  381. }
  382. else
  383. {
  384. tempBackground.SetScale(BackgroundInfo.Scale / 100);
  385. }
  386. tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
  387. tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
  388. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  389. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  390. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  391. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  392. tempBackground.SetPages("0");
  393. tempBackground.SetAllowsPrint(true);
  394. tempBackground.SetAllowsView(true);
  395. tempBackground.Update();
  396. //获取透明背景的图片
  397. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, 595, 842, 0, false, false, 0x00FFFFFF);
  398. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  399. {
  400. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  401. }
  402. backgroundItem.previewImagePath = savePath;
  403. tempDocument.Release();
  404. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  405. Settings.Default.Save();
  406. }
  407. }
  408. catch
  409. {
  410. }
  411. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  412. {
  413. Unicode = Unicode,
  414. Status = EnumTemplateListOrCreate.StatusTemplate
  415. });
  416. }
  417. public void ConfirmEditBackgroundTemplateItem(EnumColorOrFileUnicode enumColorOrFileunicode)
  418. {
  419. EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
  420. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  421. {
  422. var backgroundItem = new BackgroundItem();
  423. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  424. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusTemplate });
  425. try
  426. {
  427. //创建缓存文件夹
  428. string folderPath = Path.Combine(App.CurrentPath, "Background");
  429. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  430. //保险措施(猜测)
  431. if (File.Exists(folderPath))
  432. {
  433. File.Delete(folderPath);
  434. }
  435. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  436. if (!tempfolder.Exists)
  437. {
  438. tempfolder.Create();
  439. }
  440. //预览图缓存
  441. string saveName = Guid.NewGuid().ToString();
  442. string savePath = Path.Combine(folderPath, saveName);
  443. System.Windows.Size pageSize = new Size { Width = 128, Height = 160 };
  444. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  445. if (tempDocument != null)
  446. {
  447. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  448. var tempBackground = tempDocument.GetBackground();
  449. tempBackground.SetBackgroundType(BackgroundInfo.BackgroundType);
  450. tempBackground.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  451. if (!BackgroundInfo.IsRelativeScale)
  452. {
  453. tempBackground.SetScale(1);
  454. }
  455. else
  456. {
  457. tempBackground.SetScale(BackgroundInfo.Scale / 100);
  458. }
  459. tempBackground.SetRotation((float)((BackgroundInfo.Rotation / 180) * Math.PI));
  460. tempBackground.SetOpacity((byte)((float)(BackgroundInfo.Opacity / 100) * 255));
  461. tempBackground.SetVertalign(BackgroundInfo.Vertalign);
  462. tempBackground.SetHorizalign(BackgroundInfo.Horizalign);
  463. tempBackground.SetXOffset(BackgroundInfo.HorizOffset);
  464. tempBackground.SetYOffset(BackgroundInfo.VertOffset);
  465. tempBackground.SetPages("0");
  466. tempBackground.SetAllowsPrint(true);
  467. tempBackground.SetAllowsView(true);
  468. tempBackground.Update();
  469. //获取透明背景的图片
  470. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  471. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  472. {
  473. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  474. }
  475. backgroundItem.previewImagePath = savePath;
  476. tempDocument.Release();
  477. Settings.Default.BackgroundTemplateList[TemplateBackgroundItem.listIndex] = backgroundItem;
  478. Settings.Default.Save();
  479. }
  480. }
  481. catch
  482. {
  483. }
  484. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  485. {
  486. Unicode = Unicode,
  487. Status = EnumTemplateListOrCreate.StatusTemplate
  488. });
  489. }
  490. }
  491. public void SaveBackgroundTemplate(EnumColorOrFileUnicode enumColorOrFileunicode)
  492. {
  493. EnumColorOrFile enumColorOrFile = enumColorOrFileunicode.Status;
  494. if (enumColorOrFile == EnumColorOrFile.StatusFile)
  495. {
  496. SaveCurrentTemplate();
  497. }
  498. }
  499. public bool IsNavigationTarget(NavigationContext navigationContext)
  500. {
  501. return true;
  502. }
  503. public void OnNavigatedFrom(NavigationContext navigationContext)
  504. {
  505. }
  506. public void OnNavigatedTo(NavigationContext navigationContext)
  507. {
  508. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  509. navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
  510. if (navigationContext.Parameters.TryGetValue<BackgroundItem>("BackgroundItem", out TemplateBackgroundItem))
  511. {
  512. InitComponentBySelectedInfo();
  513. }
  514. }
  515. }
  516. }