BackgroundCreateFileContentViewModel.cs 20 KB

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