BatesCreateContentViewModel.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Events;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Windows;
  12. using PDFSettings;
  13. using PDF_Office.Model.EditTools.Watermark;
  14. using PDF_Office.Helper;
  15. using System.Windows.Media;
  16. using System.Diagnostics;
  17. using ComPDFKit.PDFDocument;
  18. using PDF_Office.Model.EditTools.Background;
  19. using PDF_Office.Model.EditTools.Bates;
  20. using PDF_Office.Model.EditTools.HeaderFooter;
  21. using PDF_Office.Properties;
  22. using System.IO;
  23. namespace PDF_Office.ViewModels.EditTools.Bates
  24. {
  25. public class BatesCreateContentViewModel : BindableBase, INavigationAware
  26. {
  27. private CPDFViewer PDFViewer;
  28. public BatesInfo BatesInfo = new BatesInfo();
  29. public BatesHeaderFooterItem TemplateBatesItem;
  30. public enum EnumCreateOrEdit
  31. {
  32. None,
  33. StatusCreate,
  34. StatusEdit
  35. }
  36. private List<string> _fontNameList = new List<string>();
  37. public List<string> FontNameList
  38. {
  39. get { return _fontNameList; }
  40. set
  41. {
  42. SetProperty(ref _fontNameList, value);
  43. }
  44. }
  45. private void InitFontNameList()
  46. {
  47. FontNameList.Clear();
  48. FontNameList.Add("Courier");
  49. FontNameList.Add("Courier-Bold");
  50. FontNameList.Add("Courier-Oblique");
  51. FontNameList.Add("Courier-BoldOblique");
  52. FontNameList.Add("Helvetica");
  53. FontNameList.Add("Helvetica-Bold");
  54. FontNameList.Add("Helvetica-Oblique");
  55. FontNameList.Add("Helvetica-BoldOblique");
  56. FontNameList.Add("Times-Roman");
  57. FontNameList.Add("Times-Bold");
  58. FontNameList.Add("Times-Italic");
  59. FontNameList.Add("Times-BoldItalic");
  60. }
  61. private int _fontNameSelectedIndex = 0;
  62. public int FontNameSelectedIndex
  63. {
  64. get { return _fontNameSelectedIndex; }
  65. set
  66. {
  67. SetProperty(ref _fontNameSelectedIndex, value);
  68. SetFontName(FontNameSelectedIndex);
  69. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  70. {
  71. Unicode = Unicode,
  72. Status = BatesInfo
  73. });
  74. }
  75. }
  76. private List<string> _fontSizeList = new List<string>();
  77. public List<string> FontSizeList
  78. {
  79. get { return _fontSizeList; }
  80. set
  81. {
  82. SetProperty(ref _fontSizeList, value);
  83. }
  84. }
  85. private void InitFontSizeList()
  86. {
  87. FontSizeList.Clear();
  88. for (int temp = 10; temp <= 15; temp += 1)
  89. {
  90. FontSizeList.Add(temp.ToString() + "pt");
  91. }
  92. }
  93. private int _fontSizeSelectedIndex = 0;
  94. public int FontSizeSelectedIndex
  95. {
  96. get { return _fontSizeSelectedIndex; }
  97. set
  98. {
  99. SetProperty(ref _fontSizeSelectedIndex, value);
  100. SetFontSize(FontSizeSelectedIndex);
  101. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  102. {
  103. Unicode = Unicode,
  104. Status = BatesInfo
  105. });
  106. }
  107. }
  108. private List<string> _starPagetNumberList = new List<string>();
  109. public List<string> StarPagetNumberList
  110. {
  111. get { return _starPagetNumberList; }
  112. set
  113. {
  114. SetProperty(ref _starPagetNumberList, value);
  115. }
  116. }
  117. private void InitStarPagetNumberList()
  118. {
  119. StarPagetNumberList.Clear();
  120. for (int temp = 10; temp <= 15; temp += 1)
  121. {
  122. StarPagetNumberList.Add(temp.ToString());
  123. }
  124. }
  125. private int _starPagetNumberIndex = 0;
  126. public int StarPagetNumberIndex
  127. {
  128. get { return _starPagetNumberIndex; }
  129. set
  130. {
  131. SetProperty(ref _starPagetNumberIndex, value);
  132. SetStarPagetNumber(StarPagetNumberIndex);
  133. }
  134. }
  135. public string _pageRangeText = "0";
  136. public string PageRangeText
  137. {
  138. get { return _pageRangeText; }
  139. set
  140. {
  141. _pageRangeText = value;
  142. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  143. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  144. {
  145. Unicode = Unicode,
  146. Status = BatesInfo
  147. });
  148. }
  149. }
  150. private int _pageRangeSelectIndex = 0;
  151. public int PageRangeSelectIndex
  152. {
  153. get { return _pageRangeSelectIndex; }
  154. set
  155. {
  156. SetProperty(ref _pageRangeSelectIndex, value);
  157. BatesInfo.PageRangeIndex = PageRangeSelectIndex;
  158. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  159. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  160. {
  161. Unicode = Unicode,
  162. Status = BatesInfo
  163. });
  164. }
  165. }
  166. private SolidColorBrush _solidColorBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
  167. public SolidColorBrush SolidColorBrush
  168. {
  169. get
  170. {
  171. return _solidColorBrush;
  172. }
  173. set
  174. {
  175. SetProperty(ref _solidColorBrush, value);
  176. }
  177. }
  178. private Color _stringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  179. public Color StringColor
  180. {
  181. get
  182. {
  183. return _stringColor;
  184. }
  185. set
  186. {
  187. SetProperty(ref _stringColor, value);
  188. SolidColorBrush = new SolidColorBrush(StringColor);
  189. for (int i = 0; i <= 5; i++)
  190. {
  191. BatesInfo.TextData[i].Color = EditToolsHelper.ConvertColor(value);
  192. }
  193. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  194. {
  195. Unicode = Unicode,
  196. Status = BatesInfo
  197. });
  198. }
  199. }
  200. private EnumCreateOrEdit _createOrEdit;
  201. public EnumCreateOrEdit CreateOrEdit
  202. {
  203. get { return _createOrEdit; }
  204. set
  205. {
  206. _createOrEdit = value;
  207. if (value == EnumCreateOrEdit.StatusEdit)
  208. {
  209. EditBaseVisible = Visibility.Visible;
  210. CreateBaseVisible = Visibility.Collapsed;
  211. }
  212. else if (value == EnumCreateOrEdit.StatusCreate)
  213. {
  214. CreateBaseVisible = Visibility.Visible;
  215. EditBaseVisible = Visibility.Collapsed;
  216. }
  217. }
  218. }
  219. private Visibility _createBaseVisible;
  220. public Visibility CreateBaseVisible
  221. {
  222. get => _createBaseVisible;
  223. set => SetProperty(ref _createBaseVisible, value);
  224. }
  225. private int maxPageRange = 0;
  226. public int MaxPageRange
  227. {
  228. get { return maxPageRange; }
  229. set
  230. {
  231. SetProperty(ref maxPageRange, value);
  232. }
  233. }
  234. private Visibility _editBaseVisible;
  235. public Visibility EditBaseVisible
  236. {
  237. get => _editBaseVisible;
  238. set => SetProperty(ref _editBaseVisible, value);
  239. }
  240. private string _marginTopValue = "3";
  241. public string MarginTopValue
  242. {
  243. get { return _marginTopValue; }
  244. set
  245. {
  246. SetProperty(ref _marginTopValue, value);
  247. bool ValueEQ = true;
  248. if (BatesInfo.margin[1] == float.Parse(MarginTopValue))
  249. {
  250. ValueEQ = false;
  251. }
  252. BatesInfo.margin[1] = float.Parse(MarginTopValue);
  253. if (ValueEQ)
  254. {
  255. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  256. {
  257. Unicode = Unicode,
  258. Status = BatesInfo
  259. });
  260. }
  261. }
  262. }
  263. private string _marginDownValue = "3";
  264. public string MarginDownValue
  265. {
  266. get { return _marginDownValue; }
  267. set
  268. {
  269. SetProperty(ref _marginDownValue, value);
  270. bool ValueEQ = true;
  271. if (BatesInfo.margin[3] == float.Parse(MarginTopValue))
  272. {
  273. ValueEQ = false;
  274. }
  275. BatesInfo.margin[3] = float.Parse(MarginDownValue);
  276. if (ValueEQ)
  277. {
  278. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  279. {
  280. Unicode = Unicode,
  281. Status = BatesInfo
  282. });
  283. }
  284. }
  285. }
  286. private string _marginLeftValue = "3";
  287. public string MarginLeftValue
  288. {
  289. get { return _marginLeftValue; }
  290. set
  291. {
  292. SetProperty(ref _marginLeftValue, value);
  293. bool ValueEQ = true;
  294. if (BatesInfo.margin[0] == float.Parse(MarginTopValue))
  295. {
  296. ValueEQ = false;
  297. }
  298. BatesInfo.margin[0] = float.Parse(MarginLeftValue);
  299. if (ValueEQ)
  300. {
  301. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  302. {
  303. Unicode = Unicode,
  304. Status = BatesInfo
  305. });
  306. }
  307. }
  308. }
  309. private string _marginRightValue = "3";
  310. public string MarginRightValue
  311. {
  312. get { return _marginRightValue; }
  313. set
  314. {
  315. SetProperty(ref _marginRightValue, value);
  316. bool ValueEQ = true;
  317. if (BatesInfo.margin[2] == float.Parse(MarginTopValue))
  318. {
  319. ValueEQ = false;
  320. }
  321. if (ValueEQ)
  322. {
  323. BatesInfo.margin[2] = float.Parse(MarginRightValue);
  324. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  325. {
  326. Unicode = Unicode,
  327. Status = BatesInfo
  328. });
  329. }
  330. }
  331. }
  332. private string _digitNumberValue = "1";
  333. public string DigitNumberValue
  334. {
  335. get { return _digitNumberValue; }
  336. set
  337. {
  338. SetProperty(ref _digitNumberValue, value);
  339. BatesInfo.DigitNumber = int.Parse(DigitNumberValue);
  340. }
  341. }
  342. private string _textValue = "";
  343. public string TextValue
  344. {
  345. get { return _textValue; }
  346. set
  347. {
  348. SetProperty(ref _textValue, value);
  349. }
  350. }
  351. private string _prefixTextValue = "";
  352. public string PrefixTextValue
  353. {
  354. get { return _prefixTextValue; }
  355. set
  356. {
  357. _prefixTextValue = value;
  358. }
  359. }
  360. private string _surfixTextValue = "";
  361. public string SurfixTextValue
  362. {
  363. get { return _surfixTextValue; }
  364. set
  365. {
  366. _surfixTextValue = value;
  367. }
  368. }
  369. public int GetLocationIndex = 0;
  370. private Dictionary<string, string> _getTextValueFromNumber = new Dictionary<string, string>();
  371. public Dictionary<string, string> GetTextValueFromNumber
  372. {
  373. get { return _getTextValueFromNumber; }
  374. set
  375. {
  376. _getTextValueFromNumber = value;
  377. }
  378. }
  379. public IEventAggregator eventAggregator;
  380. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  381. public DelegateCommand EnterTemplateListCommand { get; set; }
  382. public DelegateCommand SaveToTemplateListCommand { get; set; }
  383. public DelegateCommand SaveToCurrentTemplateListCommand { get; set; }
  384. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  385. public DelegateCommand<object> TextValueChangedCommand { get; set; }
  386. public DelegateCommand ADDBatesCommand { get; set; }
  387. public string Unicode = null;
  388. public BatesCreateContentViewModel(IEventAggregator eventAggregator)
  389. {
  390. this.eventAggregator = eventAggregator;
  391. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  392. for (int i = 0; i <= 5; i++)
  393. {
  394. BatesInfo.TextData[i].Color = EditToolsHelper.ConvertColor(Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
  395. }
  396. InitList();
  397. InitBatesInfo();
  398. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  399. SelectedColorChangedCommand = new DelegateCommand<object>(ChangedColor);
  400. EnterTemplateListCommand = new DelegateCommand(EnterTemplateList);
  401. SaveToTemplateListCommand = new DelegateCommand(SaveToTemplateList);
  402. SaveToCurrentTemplateListCommand = new DelegateCommand(SaveToCurrentTemplateList);
  403. TextValueChangedCommand = new DelegateCommand<object>(TextValueChanged);
  404. ADDBatesCommand = new DelegateCommand(ADDBates);
  405. }
  406. private void InitList()
  407. {
  408. InitFontNameList();
  409. InitFontSizeList();
  410. InitStarPagetNumberList();
  411. InitLocationButtonMatrix();
  412. }
  413. private void InitLocationButtonMatrix()
  414. {
  415. GetTextValueFromNumber.Clear();
  416. for (var temp = 0; temp <= 5; temp++)
  417. {
  418. GetTextValueFromNumber.Add(temp.ToString(), temp.ToString());
  419. }
  420. }
  421. private void InitBatesInfo()
  422. {
  423. for (int i = 0; i <= 5; i++)
  424. {
  425. BatesInfo.TextData[i].text = GetTextValueFromNumber[i.ToString()];
  426. }
  427. BatesInfo.margin[0] = float.Parse(MarginTopValue);
  428. BatesInfo.margin[1] = float.Parse(MarginRightValue);
  429. BatesInfo.margin[2] = float.Parse(MarginDownValue);
  430. BatesInfo.margin[3] = float.Parse(MarginLeftValue);
  431. BatesInfo.Prefix = PrefixTextValue;
  432. BatesInfo.Suffix = SurfixTextValue;
  433. BatesInfo.DigitNumber = int.Parse(DigitNumberValue);
  434. SetFontName(FontNameSelectedIndex);
  435. SetFontSize(FontSizeSelectedIndex);
  436. SetStarPagetNumber(StarPagetNumberIndex);
  437. StringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  438. }
  439. private void SetFontName(int Index)
  440. {
  441. for (int i = 0; i <= 5; i++) { BatesInfo.TextData[i].fontName = FontNameList[Index]; }
  442. }
  443. private void SetFontSize(int Index)
  444. {
  445. for (int i = 0; i <= 5; i++) { BatesInfo.TextData[i].fontSize = (Index + 7) * 1.33f; }
  446. }
  447. private void SetStarPagetNumber(int Index)
  448. {
  449. BatesInfo.StarPagetNumber = Index;
  450. }
  451. public void ChangeLocation(object e)
  452. {
  453. string args = e as string;
  454. if (args != null)
  455. {
  456. GetLocationIndex = int.Parse(args);
  457. TextValue = GetTextValueFromNumber[args];
  458. }
  459. }
  460. private void ChangedColor(object obj)
  461. {
  462. if (obj != null)
  463. {
  464. var colorValue = (Color)obj;
  465. if (colorValue != null)
  466. {
  467. StringColor = colorValue;
  468. }
  469. }
  470. }
  471. public void EnterTemplateList()
  472. {
  473. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode { Unicode = Unicode, Status = EnumTemplateListOrCreate.StatusTemplate });
  474. }
  475. public void SaveToTemplateList()
  476. {
  477. SaveCurrentTemplate();
  478. }
  479. public void SaveToCurrentTemplateList()
  480. {
  481. ConfirmEditBackgroundTemplateItem();
  482. }
  483. private void TextValueChanged(object obj)
  484. {
  485. if (obj != null)
  486. {
  487. var textValue = obj as System.Windows.Controls.TextBox;
  488. if (textValue != null)
  489. {
  490. bool TextEQ = true;
  491. switch (textValue.Name)
  492. {
  493. case "TextValueTextBox":
  494. if (GetTextValueFromNumber[GetLocationIndex.ToString()] == textValue.Text)
  495. {
  496. TextEQ = false;
  497. }
  498. GetTextValueFromNumber[GetLocationIndex.ToString()] = textValue.Text;
  499. BatesInfo.TextData[GetLocationIndex].text = GetTextValueFromNumber[GetLocationIndex.ToString()];
  500. break;
  501. case "PrefixTextValueTextBox":
  502. if (BatesInfo.Prefix == textValue.Text)
  503. {
  504. TextEQ = false;
  505. }
  506. BatesInfo.Prefix = textValue.Text;
  507. break;
  508. case "SuffixTextValueTextBox":
  509. if (BatesInfo.Suffix == textValue.Text)
  510. {
  511. TextEQ = false;
  512. }
  513. BatesInfo.Suffix = textValue.Text;
  514. break;
  515. default:
  516. break;
  517. }
  518. if (TextEQ)
  519. {
  520. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  521. {
  522. Unicode = Unicode,
  523. Status = BatesInfo
  524. });
  525. }
  526. }
  527. }
  528. }
  529. public void ADDBates()
  530. {
  531. if (PrefixTextValue == "" || SurfixTextValue == "")
  532. {
  533. if (SurfixTextValue == "" && PrefixTextValue == "")
  534. {
  535. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + ">>";
  536. }
  537. else if (SurfixTextValue == "")
  538. {
  539. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + PrefixTextValue + ">>";
  540. }
  541. else
  542. {
  543. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + "#" + SurfixTextValue + ">>";
  544. }
  545. }
  546. else
  547. {
  548. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + PrefixTextValue + "#" + SurfixTextValue + ">>";
  549. }
  550. BatesInfo.TextData[GetLocationIndex].text = GetTextValueFromNumber[GetLocationIndex.ToString()];
  551. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  552. {
  553. Unicode = Unicode,
  554. Status = BatesInfo
  555. });
  556. }
  557. public void ConvertInfoToItem(ref BatesHeaderFooterItem batesItem, BatesInfo batesInfo)
  558. {
  559. batesItem.TextData = batesInfo.TextData;
  560. batesItem.DigitNumber = batesInfo.DigitNumber;
  561. batesItem.ItemName = batesInfo.ItemName;
  562. batesItem.Prefix = batesInfo.Prefix;
  563. batesItem.Suffix = batesInfo.Suffix;
  564. batesItem.StarPagetNumber = batesInfo.StarPagetNumber;
  565. batesItem.margin = batesInfo.margin;
  566. batesItem.PageRange = batesInfo.PageRange;
  567. batesItem.PageRangeIndex = batesInfo.PageRangeIndex;
  568. }
  569. public void ConvertItemToInfo(BatesHeaderFooterItem batesItem, ref BatesInfo batesInfo)
  570. {
  571. batesInfo.TextData = batesItem.TextData;
  572. batesInfo.ItemName = batesItem.ItemName;
  573. batesInfo.DigitNumber = batesItem.DigitNumber;
  574. batesInfo.Prefix = batesItem.Prefix;
  575. batesInfo.Suffix = batesItem.Suffix;
  576. batesInfo.StarPagetNumber = batesItem.StarPagetNumber;
  577. batesInfo.margin = batesItem.margin;
  578. batesInfo.PageRangeIndex = batesItem.PageRangeIndex;
  579. EditToolsHelper.GetPageRange(batesInfo.PageRangeIndex, PDFViewer.Document, ref batesInfo.PageRange, batesItem.PageRange);
  580. }
  581. public void SaveCurrentTemplate()
  582. {
  583. var batesItem = new BatesHeaderFooterItem();
  584. ConvertInfoToItem(ref batesItem, BatesInfo);
  585. batesItem.ItemName += Settings.Default.BatesTemplateList.Count().ToString();
  586. try
  587. {
  588. //创建缓存文件夹
  589. string folderPath = Path.Combine(App.CurrentPath, "Bates");
  590. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  591. //保险措施(猜测)
  592. if (File.Exists(folderPath))
  593. {
  594. File.Delete(folderPath);
  595. }
  596. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  597. if (!tempfolder.Exists)
  598. {
  599. tempfolder.Create();
  600. }
  601. //预览图缓存
  602. string saveName = Guid.NewGuid().ToString();
  603. string savePath = Path.Combine(folderPath, saveName);
  604. Settings.Default.BatesTemplateList.Add(batesItem);
  605. Settings.Default.Save();
  606. }
  607. catch
  608. {
  609. }
  610. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  611. {
  612. Unicode = Unicode,
  613. Status = EnumTemplateListOrCreate.StatusTemplate
  614. });
  615. }
  616. public void ConfirmEditBackgroundTemplateItem()
  617. {
  618. var batesItem = new BatesHeaderFooterItem();
  619. ConvertInfoToItem(ref batesItem, BatesInfo);
  620. try
  621. {
  622. //创建缓存文件夹
  623. string folderPath = Path.Combine(App.CurrentPath, "Background");
  624. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  625. //保险措施(猜测)
  626. if (File.Exists(folderPath))
  627. {
  628. File.Delete(folderPath);
  629. }
  630. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  631. if (!tempfolder.Exists)
  632. {
  633. tempfolder.Create();
  634. }
  635. Settings.Default.BatesTemplateList[TemplateBatesItem.listIndex] = batesItem;
  636. Settings.Default.Save();
  637. }
  638. catch
  639. {
  640. }
  641. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  642. {
  643. Unicode = Unicode,
  644. Status = EnumTemplateListOrCreate.StatusTemplate
  645. });
  646. }
  647. public void InitComponentBySelectedInfo()
  648. {
  649. ConvertItemToInfo(TemplateBatesItem, ref BatesInfo);
  650. for (int i = 0; i <= 5; i++)
  651. {
  652. GetTextValueFromNumber[i.ToString()] = BatesInfo.TextData[i].text;
  653. }
  654. MarginTopValue = BatesInfo.margin[0].ToString();
  655. MarginRightValue = BatesInfo.margin[1].ToString();
  656. MarginDownValue = BatesInfo.margin[2].ToString();
  657. MarginLeftValue = BatesInfo.margin[3].ToString();
  658. PrefixTextValue = BatesInfo.Prefix;
  659. SurfixTextValue = BatesInfo.Suffix;
  660. DigitNumberValue = BatesInfo.DigitNumber.ToString();
  661. FontNameSelectedIndex = FontNameList.IndexOf(BatesInfo.TextData[0].fontName);
  662. FontSizeSelectedIndex = FontSizeList.IndexOf(BatesInfo.TextData[0].fontSize.ToString());
  663. StarPagetNumberIndex = StarPagetNumberList.IndexOf(BatesInfo.StarPagetNumber.ToString());
  664. StringColor = EditToolsHelper.ConvertColor(BatesInfo.TextData[0].Color);
  665. SolidColorBrush = new SolidColorBrush(StringColor);
  666. PageRangeSelectIndex = BatesInfo.PageRangeIndex;
  667. }
  668. public bool IsNavigationTarget(NavigationContext navigationContext)
  669. {
  670. return true;
  671. }
  672. public void OnNavigatedFrom(NavigationContext navigationContext)
  673. {
  674. }
  675. public void OnNavigatedTo(NavigationContext navigationContext)
  676. {
  677. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  678. MaxPageRange = PDFViewer.Document.PageCount;
  679. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  680. if (navigationContext.Parameters.TryGetValue<BatesHeaderFooterItem>("BatesItem", out TemplateBatesItem))
  681. {
  682. CreateOrEdit = EnumCreateOrEdit.StatusEdit;
  683. InitComponentBySelectedInfo();
  684. }
  685. else
  686. {
  687. CreateOrEdit = EnumCreateOrEdit.StatusCreate;
  688. }
  689. }
  690. }
  691. }