BackgroundCreateFileContentViewModel.cs 22 KB

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