HomePageBackgroundCreateFileContentViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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.EditTools.Background;
  7. using PDF_Office.Model;
  8. using PDF_Office.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_Office.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 string Unicode = null;
  169. public HomePageBackgroundCreateFileContentViewModel(IEventAggregator eventAggregator)
  170. {
  171. this.eventAggregator = eventAggregator;
  172. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  173. BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  174. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  175. InitComponent();
  176. BackgroundInfo.Horizalign = C_Background_Horizalign.BG_HORIZALIGN_CENTER;
  177. BackgroundInfo.Vertalign = C_Background_Vertalign.BG_VERTALIGN_CENTER;
  178. InitLocationButtonMatrix();
  179. OpenFileCommand = new DelegateCommand(OpenFile);
  180. eventAggregator.GetEvent<SaveBackgroundTemplateEvent>().Subscribe(SaveBackgroundTemplate, e => e.Unicode == Unicode);
  181. eventAggregator.GetEvent<ConfirmEditBackgroundTemplateItemEvent>().Subscribe(ConfirmEditBackgroundTemplateItem, e => e.Unicode == Unicode);
  182. }
  183. public string _pageRangeText { get; set; } = "0";
  184. public string PageRangeText
  185. {
  186. get { return _pageRangeText; }
  187. set
  188. {
  189. List<int> PageIndexLists = new List<int>();
  190. _pageRangeText = value;
  191. BackgroundInfo.PageRange = PageRangeText;
  192. if (PageRangeSelectIndex == 3 && !CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, 99999, new char[] { ',' }, new char[] { '-' }))
  193. {
  194. BackgroundInfo.PageRangeIndex = 0;
  195. }
  196. BackgroundInfo.PageRangeIndex = 3;
  197. }
  198. }
  199. private int _pageRangeSelectIndex = 0;
  200. public int PageRangeSelectIndex
  201. {
  202. get { return _pageRangeSelectIndex; }
  203. set
  204. {
  205. SetProperty(ref _pageRangeSelectIndex, value);
  206. BackgroundInfo.PageRangeIndex = PageRangeSelectIndex;
  207. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  208. }
  209. }
  210. public void InitComponent()
  211. {
  212. InitOpacityList();
  213. InitRotationList();
  214. InitRelativeRatioList();
  215. }
  216. private void InitRotationList()
  217. {
  218. RotationList.Clear();
  219. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  220. {
  221. RotationList.Add(defaultRotation.ToString());
  222. }
  223. }
  224. private void InitOpacityList()
  225. {
  226. OpacityList.Clear();
  227. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  228. {
  229. OpacityList.Add(defaultOpacity.ToString() + " %");
  230. }
  231. }
  232. private void InitRelativeRatioList()
  233. {
  234. RelativeRatioList.Clear();
  235. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  236. {
  237. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  238. }
  239. }
  240. private void InitLocationButtonMatrix()
  241. {
  242. GetLocationFromNumber.Clear();
  243. for (var temp = 0; temp <= 22; temp++)
  244. {
  245. GetLocationFromNumber.Add(temp.ToString(), true);
  246. if (temp % 10 == 2)
  247. {
  248. temp += 7;
  249. }
  250. }
  251. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  252. GetLocationFromNumber[Num.ToString()] = false;
  253. }
  254. public void InitComponentBySelectedInfo()
  255. {
  256. ConvertItemToInfo(TemplateBackgroundItem, ref BackgroundInfo);
  257. RotationText = BackgroundInfo.Rotation.ToString();
  258. OpacityText = BackgroundInfo.Opacity.ToString();
  259. InitLocationButtonMatrix();
  260. IsRelativeScaleValue = BackgroundInfo.IsRelativeScale;
  261. RelativeScaleText = BackgroundInfo.Scale.ToString();
  262. HorizontalOffsetValue = (BackgroundInfo.HorizOffset).ToString();
  263. VerticalOffsetValue = (BackgroundInfo.VertOffset).ToString();
  264. PageRangeSelectIndex = BackgroundInfo.PageRangeIndex;
  265. }
  266. public void ChangeLocation(object e)
  267. {
  268. string args = e as string;
  269. if (args != null)
  270. {
  271. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  272. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  273. InitLocationButtonMatrix();
  274. }
  275. }
  276. public void OpenFile()
  277. {
  278. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  279. dlg.Multiselect = false;
  280. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  281. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  282. {
  283. FileNameText = dlg.SafeFileName;
  284. FileInfo file = new FileInfo(dlg.FileName);
  285. if (file.Extension == ".pdf")
  286. {
  287. GetBitmapFromDocment(dlg.FileName);
  288. }
  289. else
  290. {
  291. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  292. }
  293. CreateModFileVisible = Visibility.Visible;
  294. }
  295. }
  296. public async void GetBitmapFromDocment(string filePath)
  297. {
  298. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  299. CPDFPage page = document.PageAtIndex(0);
  300. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  301. await Task.Run(delegate
  302. {
  303. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  304. });
  305. BackgroundInfo.ImageArray = bmp_data;
  306. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  307. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  308. document.ReleasePages();
  309. document.Release();
  310. }
  311. public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
  312. {
  313. if (backgroundItem != null)
  314. {
  315. backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  316. backgroundInfo.Color = backgroundItem.bgColor;
  317. backgroundInfo.Horizalign = backgroundItem.horizalign;
  318. backgroundInfo.Vertalign = backgroundItem.vertalign;
  319. backgroundInfo.VertOffset = backgroundItem.vertOffset;
  320. backgroundInfo.Horizalign = backgroundItem.horizalign;
  321. backgroundInfo.Opacity = backgroundItem.opacity;
  322. backgroundInfo.Rotation = backgroundItem.rotation;
  323. backgroundInfo.IsRelativeScale = backgroundItem.isRelativeScale;
  324. backgroundInfo.Scale = backgroundItem.scale;
  325. backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex;
  326. backgroundInfo.PageRange = backgroundItem.pageRange;
  327. }
  328. }
  329. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  330. {
  331. backgroundItem.type = C_Background_Type.BG_TYPE_IMAGE;
  332. backgroundItem.imageArray = backgroundInfo.ImageArray;
  333. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  334. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  335. backgroundItem.horizalign = backgroundInfo.Horizalign;
  336. backgroundItem.vertalign = backgroundInfo.Vertalign;
  337. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  338. backgroundItem.horizalign = backgroundInfo.Horizalign;
  339. backgroundItem.opacity = backgroundInfo.Opacity;
  340. backgroundItem.rotation = backgroundInfo.Rotation;
  341. backgroundItem.isRelativeScale = backgroundInfo.IsRelativeScale;
  342. backgroundItem.scale = backgroundInfo.Scale;
  343. backgroundItem.PageRangeIndex = backgroundInfo.PageRangeIndex;
  344. backgroundItem.pageRange = backgroundInfo.PageRange;
  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 = PDFViewer.Document.GetPageSize(0);
  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. }