HomePageBackgroundCreateFileContentViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. _pageRangeText = value;
  190. BackgroundInfo.PageRange = PageRangeText;
  191. }
  192. }
  193. private int _pageRangeSelectIndex = 0;
  194. public int PageRangeSelectIndex
  195. {
  196. get { return _pageRangeSelectIndex; }
  197. set
  198. {
  199. SetProperty(ref _pageRangeSelectIndex, value);
  200. BackgroundInfo.PageRangeIndex = PageRangeSelectIndex;
  201. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BackgroundInfo.PageRange, PageRangeText);
  202. }
  203. }
  204. public void InitComponent()
  205. {
  206. InitOpacityList();
  207. InitRotationList();
  208. InitRelativeRatioList();
  209. }
  210. private void InitRotationList()
  211. {
  212. RotationList.Clear();
  213. for (int defaultRotation = -45; defaultRotation <= 45; defaultRotation += 15)
  214. {
  215. RotationList.Add(defaultRotation.ToString());
  216. }
  217. }
  218. private void InitOpacityList()
  219. {
  220. OpacityList.Clear();
  221. for (int defaultOpacity = 10; defaultOpacity <= 100; defaultOpacity += 10)
  222. {
  223. OpacityList.Add(defaultOpacity.ToString() + " %");
  224. }
  225. }
  226. private void InitRelativeRatioList()
  227. {
  228. RelativeRatioList.Clear();
  229. for (int defaultRelativeRatioList = 10; defaultRelativeRatioList <= 100; defaultRelativeRatioList += 10)
  230. {
  231. RelativeRatioList.Add(defaultRelativeRatioList.ToString() + " %");
  232. }
  233. }
  234. private void InitLocationButtonMatrix()
  235. {
  236. GetLocationFromNumber.Clear();
  237. for (var temp = 0; temp <= 22; temp++)
  238. {
  239. GetLocationFromNumber.Add(temp.ToString(), true);
  240. if (temp % 10 == 2)
  241. {
  242. temp += 7;
  243. }
  244. }
  245. int Num = (int)BackgroundInfo.Vertalign * 10 + (int)BackgroundInfo.Horizalign;
  246. GetLocationFromNumber[Num.ToString()] = false;
  247. }
  248. public void InitComponentBySelectedInfo()
  249. {
  250. ConvertItemToInfo(TemplateBackgroundItem, ref BackgroundInfo);
  251. RotationText = BackgroundInfo.Rotation.ToString();
  252. OpacityText = BackgroundInfo.Opacity.ToString();
  253. InitLocationButtonMatrix();
  254. IsRelativeScaleValue = BackgroundInfo.IsRelativeScale;
  255. RelativeScaleText = BackgroundInfo.Scale.ToString();
  256. HorizontalOffsetValue = (BackgroundInfo.HorizOffset).ToString();
  257. VerticalOffsetValue = (BackgroundInfo.VertOffset).ToString();
  258. PageRangeSelectIndex = BackgroundInfo.PageRangeIndex;
  259. }
  260. public void ChangeLocation(object e)
  261. {
  262. string args = e as string;
  263. if (args != null)
  264. {
  265. BackgroundInfo.Vertalign = (C_Background_Vertalign)(int.Parse(args) / 10);
  266. BackgroundInfo.Horizalign = (C_Background_Horizalign)(int.Parse(args) % 10);
  267. InitLocationButtonMatrix();
  268. }
  269. }
  270. public void OpenFile()
  271. {
  272. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  273. dlg.Multiselect = false;
  274. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  275. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  276. {
  277. FileNameText = dlg.SafeFileName;
  278. FileInfo file = new FileInfo(dlg.FileName);
  279. if (file.Extension == ".pdf")
  280. {
  281. GetBitmapFromDocment(dlg.FileName);
  282. }
  283. else
  284. {
  285. EditToolsHelper.ChooseFile(dlg.FileName, ref BackgroundInfo);
  286. }
  287. CreateModFileVisible = Visibility.Visible;
  288. }
  289. }
  290. public async void GetBitmapFromDocment(string filePath)
  291. {
  292. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  293. CPDFPage page = document.PageAtIndex(0);
  294. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  295. await Task.Run(delegate
  296. {
  297. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  298. });
  299. BackgroundInfo.ImageArray = bmp_data;
  300. BackgroundInfo.ImageWidth = (int)page.PageSize.Width;
  301. BackgroundInfo.ImageHeight = (int)page.PageSize.Height;
  302. document.ReleasePages();
  303. document.Release();
  304. }
  305. public void ConvertItemToInfo(BackgroundItem backgroundItem, ref BackgroundInfo backgroundInfo)
  306. {
  307. if (backgroundItem != null)
  308. {
  309. backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  310. backgroundInfo.Color = backgroundItem.bgColor;
  311. backgroundInfo.Horizalign = backgroundItem.horizalign;
  312. backgroundInfo.Vertalign = backgroundItem.vertalign;
  313. backgroundInfo.VertOffset = backgroundItem.vertOffset;
  314. backgroundInfo.Horizalign = backgroundItem.horizalign;
  315. backgroundInfo.Opacity = backgroundItem.opacity;
  316. backgroundInfo.Rotation = backgroundItem.rotation;
  317. backgroundInfo.IsRelativeScale = backgroundItem.isRelativeScale;
  318. backgroundInfo.Scale = backgroundItem.scale;
  319. backgroundInfo.PageRangeIndex = backgroundItem.PageRangeIndex;
  320. backgroundInfo.PageRange = backgroundItem.pageRange;
  321. }
  322. }
  323. public void ConvertInfoToItem(ref BackgroundItem backgroundItem, BackgroundInfo backgroundInfo)
  324. {
  325. backgroundItem.type = C_Background_Type.BG_TYPE_IMAGE;
  326. backgroundItem.imageArray = backgroundInfo.ImageArray;
  327. backgroundItem.imageWidth = backgroundInfo.ImageWidth;
  328. backgroundItem.imageHeight = backgroundInfo.ImageHeight;
  329. backgroundItem.horizalign = backgroundInfo.Horizalign;
  330. backgroundItem.vertalign = backgroundInfo.Vertalign;
  331. backgroundItem.vertOffset = backgroundInfo.VertOffset;
  332. backgroundItem.horizalign = backgroundInfo.Horizalign;
  333. backgroundItem.opacity = backgroundInfo.Opacity;
  334. backgroundItem.rotation = backgroundInfo.Rotation;
  335. backgroundItem.isRelativeScale = backgroundInfo.IsRelativeScale;
  336. backgroundItem.scale = backgroundInfo.Scale;
  337. backgroundItem.pageRange = backgroundInfo.PageRange;
  338. List<int> PageIndexLists = new List<int>();
  339. if (PageRangeSelectIndex == 3 && !CommonHelper.GetPagesInRange(ref PageIndexLists, backgroundInfo.PageRange, 99999, new char[] { ',' }, new char[] { '-' }))
  340. {
  341. backgroundInfo.PageRangeIndex = 0;
  342. }
  343. backgroundItem.PageRangeIndex = backgroundInfo.PageRangeIndex;
  344. //backgroundItem.pagRangeMode = backgroundInfo.PageRange;
  345. }
  346. public void SaveCurrentTemplate()
  347. {
  348. var backgroundItem = new BackgroundItem();
  349. ConvertInfoToItem(ref backgroundItem, BackgroundInfo);
  350. backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString();
  351. try
  352. {
  353. //创建缓存文件夹
  354. string folderPath = Path.Combine(App.CurrentPath, "Background");
  355. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  356. //保险措施(猜测)
  357. if (File.Exists(folderPath))
  358. {
  359. File.Delete(folderPath);
  360. }
  361. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  362. if (!tempfolder.Exists)
  363. {
  364. tempfolder.Create();
  365. }
  366. //预览图缓存
  367. string saveName = Guid.NewGuid().ToString();
  368. string savePath = Path.Combine(folderPath, saveName);
  369. System.Windows.Size pageSize =new System.Windows.Size() ;
  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 = PDFViewer.Document.GetPageSize(0);
  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. }