BatesCreateContentViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Master.EventAggregators;
  3. using PDF_Master.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_Master.Model.EditTools.Watermark;
  14. using PDF_Master.Helper;
  15. using System.Windows.Media;
  16. using System.Diagnostics;
  17. using ComPDFKit.PDFDocument;
  18. using PDF_Master.Model.EditTools.Background;
  19. using PDF_Master.Model.EditTools.Bates;
  20. using PDF_Master.Model.EditTools.HeaderFooter;
  21. using PDF_Master.Properties;
  22. using System.IO;
  23. namespace PDF_Master.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(int pageCount)
  118. {
  119. StarPagetNumberList.Clear();
  120. for (int temp = 1; temp <= pageCount; 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. BatesInfo.PageRange = PageRangeText;
  143. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  144. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  145. {
  146. Unicode = Unicode,
  147. Status = BatesInfo
  148. });
  149. }
  150. }
  151. private int _pageRangeSelectIndex = 0;
  152. public int PageRangeSelectIndex
  153. {
  154. get { return _pageRangeSelectIndex; }
  155. set
  156. {
  157. SetProperty(ref _pageRangeSelectIndex, value);
  158. BatesInfo.PageRangeIndex = PageRangeSelectIndex;
  159. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  160. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  161. {
  162. Unicode = Unicode,
  163. Status = BatesInfo
  164. });
  165. }
  166. }
  167. private SolidColorBrush _solidColorBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
  168. public SolidColorBrush SolidColorBrush
  169. {
  170. get
  171. {
  172. return _solidColorBrush;
  173. }
  174. set
  175. {
  176. SetProperty(ref _solidColorBrush, value);
  177. }
  178. }
  179. private Color _stringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  180. public Color StringColor
  181. {
  182. get
  183. {
  184. return _stringColor;
  185. }
  186. set
  187. {
  188. SetProperty(ref _stringColor, value);
  189. SolidColorBrush = new SolidColorBrush(StringColor);
  190. for (int i = 0; i <= 5; i++)
  191. {
  192. BatesInfo.TextData[i].Color = EditToolsHelper.ConvertColor(value);
  193. }
  194. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  195. {
  196. Unicode = Unicode,
  197. Status = BatesInfo
  198. });
  199. }
  200. }
  201. private EnumCreateOrEdit _createOrEdit;
  202. public EnumCreateOrEdit CreateOrEdit
  203. {
  204. get { return _createOrEdit; }
  205. set
  206. {
  207. _createOrEdit = value;
  208. if (value == EnumCreateOrEdit.StatusEdit)
  209. {
  210. EditBaseVisible = Visibility.Visible;
  211. CreateBaseVisible = Visibility.Collapsed;
  212. }
  213. else if (value == EnumCreateOrEdit.StatusCreate)
  214. {
  215. CreateBaseVisible = Visibility.Visible;
  216. EditBaseVisible = Visibility.Collapsed;
  217. }
  218. }
  219. }
  220. private Visibility _createBaseVisible;
  221. public Visibility CreateBaseVisible
  222. {
  223. get => _createBaseVisible;
  224. set => SetProperty(ref _createBaseVisible, value);
  225. }
  226. private int maxPageRange = 0;
  227. public int MaxPageRange
  228. {
  229. get { return maxPageRange; }
  230. set
  231. {
  232. SetProperty(ref maxPageRange, value);
  233. }
  234. }
  235. private Visibility _editBaseVisible;
  236. public Visibility EditBaseVisible
  237. {
  238. get => _editBaseVisible;
  239. set => SetProperty(ref _editBaseVisible, value);
  240. }
  241. private string _marginTopValue = "3";
  242. public string MarginTopValue
  243. {
  244. get { return _marginTopValue; }
  245. set
  246. {
  247. SetProperty(ref _marginTopValue, value);
  248. bool ValueEQ = true;
  249. if (BatesInfo.margin[1] == float.Parse(MarginTopValue))
  250. {
  251. ValueEQ = false;
  252. }
  253. BatesInfo.margin[1] = float.Parse(MarginTopValue);
  254. if (ValueEQ)
  255. {
  256. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  257. {
  258. Unicode = Unicode,
  259. Status = BatesInfo
  260. });
  261. }
  262. }
  263. }
  264. private string _marginDownValue = "3";
  265. public string MarginDownValue
  266. {
  267. get { return _marginDownValue; }
  268. set
  269. {
  270. SetProperty(ref _marginDownValue, value);
  271. bool ValueEQ = true;
  272. if (BatesInfo.margin[3] == float.Parse(MarginTopValue))
  273. {
  274. ValueEQ = false;
  275. }
  276. BatesInfo.margin[3] = float.Parse(MarginDownValue);
  277. if (ValueEQ)
  278. {
  279. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  280. {
  281. Unicode = Unicode,
  282. Status = BatesInfo
  283. });
  284. }
  285. }
  286. }
  287. private string _marginLeftValue = "3";
  288. public string MarginLeftValue
  289. {
  290. get { return _marginLeftValue; }
  291. set
  292. {
  293. SetProperty(ref _marginLeftValue, value);
  294. bool ValueEQ = true;
  295. if (BatesInfo.margin[0] == float.Parse(MarginTopValue))
  296. {
  297. ValueEQ = false;
  298. }
  299. BatesInfo.margin[0] = float.Parse(MarginLeftValue);
  300. if (ValueEQ)
  301. {
  302. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  303. {
  304. Unicode = Unicode,
  305. Status = BatesInfo
  306. });
  307. }
  308. }
  309. }
  310. private string _marginRightValue = "3";
  311. public string MarginRightValue
  312. {
  313. get { return _marginRightValue; }
  314. set
  315. {
  316. SetProperty(ref _marginRightValue, value);
  317. bool ValueEQ = true;
  318. if (BatesInfo.margin[2] == float.Parse(MarginTopValue))
  319. {
  320. ValueEQ = false;
  321. }
  322. if (ValueEQ)
  323. {
  324. BatesInfo.margin[2] = float.Parse(MarginRightValue);
  325. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  326. {
  327. Unicode = Unicode,
  328. Status = BatesInfo
  329. });
  330. }
  331. }
  332. }
  333. private string _digitNumberValue = "1";
  334. public string DigitNumberValue
  335. {
  336. get { return _digitNumberValue; }
  337. set
  338. {
  339. SetProperty(ref _digitNumberValue, value);
  340. BatesInfo.DigitNumber = int.Parse(DigitNumberValue);
  341. }
  342. }
  343. private string _textValue = "";
  344. public string TextValue
  345. {
  346. get { return _textValue; }
  347. set
  348. {
  349. SetProperty(ref _textValue, value);
  350. }
  351. }
  352. private string _prefixTextValue = "";
  353. public string PrefixTextValue
  354. {
  355. get { return _prefixTextValue; }
  356. set
  357. {
  358. _prefixTextValue = value;
  359. }
  360. }
  361. private string _surfixTextValue = "";
  362. public string SurfixTextValue
  363. {
  364. get { return _surfixTextValue; }
  365. set
  366. {
  367. _surfixTextValue = value;
  368. }
  369. }
  370. public int GetLocationIndex = 0;
  371. private Dictionary<string, string> _getTextValueFromNumber = new Dictionary<string, string>();
  372. public Dictionary<string, string> GetTextValueFromNumber
  373. {
  374. get { return _getTextValueFromNumber; }
  375. set
  376. {
  377. _getTextValueFromNumber = value;
  378. }
  379. }
  380. public IEventAggregator eventAggregator;
  381. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  382. public DelegateCommand EnterTemplateListCommand { get; set; }
  383. public DelegateCommand SaveToTemplateListCommand { get; set; }
  384. public DelegateCommand SaveToCurrentTemplateListCommand { get; set; }
  385. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  386. public DelegateCommand<object> TextValueChangedCommand { get; set; }
  387. public DelegateCommand ADDBatesCommand { get; set; }
  388. public string Unicode = null;
  389. public BatesCreateContentViewModel(IEventAggregator eventAggregator)
  390. {
  391. this.eventAggregator = eventAggregator;
  392. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  393. for (int i = 0; i <= 5; i++)
  394. {
  395. BatesInfo.TextData[i].Color = EditToolsHelper.ConvertColor(Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
  396. }
  397. InitList();
  398. InitBatesInfo();
  399. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  400. SelectedColorChangedCommand = new DelegateCommand<object>(ChangedColor);
  401. EnterTemplateListCommand = new DelegateCommand(EnterTemplateList);
  402. SaveToTemplateListCommand = new DelegateCommand(SaveToTemplateList);
  403. SaveToCurrentTemplateListCommand = new DelegateCommand(SaveToCurrentTemplateList);
  404. TextValueChangedCommand = new DelegateCommand<object>(TextValueChanged);
  405. ADDBatesCommand = new DelegateCommand(ADDBates);
  406. }
  407. private void InitList()
  408. {
  409. InitFontNameList();
  410. InitFontSizeList();
  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(), "");
  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. PrefixTextValue= textValue.Text;
  508. break;
  509. case "SuffixTextValueTextBox":
  510. if (BatesInfo.Suffix == textValue.Text)
  511. {
  512. TextEQ = false;
  513. }
  514. BatesInfo.Suffix = textValue.Text;
  515. SurfixTextValue = textValue.Text;
  516. break;
  517. default:
  518. break;
  519. }
  520. if (TextEQ)
  521. {
  522. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  523. {
  524. Unicode = Unicode,
  525. Status = BatesInfo
  526. });
  527. }
  528. }
  529. }
  530. }
  531. public void ADDBates()
  532. {
  533. if (PrefixTextValue == "" || SurfixTextValue == "")
  534. {
  535. if (SurfixTextValue == "" && PrefixTextValue == "")
  536. {
  537. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + ">>";
  538. }
  539. else if (SurfixTextValue == "")
  540. {
  541. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + PrefixTextValue + ">>";
  542. }
  543. else
  544. {
  545. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + "#" + SurfixTextValue + ">>";
  546. }
  547. }
  548. else
  549. {
  550. GetTextValueFromNumber[GetLocationIndex.ToString()] = GetTextValueFromNumber[GetLocationIndex.ToString()] + "<<" + "#" + DigitNumberValue + "#" + (StarPagetNumberIndex + 1).ToString() + "#" + PrefixTextValue + "#" + SurfixTextValue + ">>";
  551. }
  552. BatesInfo.TextData[GetLocationIndex].text = GetTextValueFromNumber[GetLocationIndex.ToString()];
  553. TextValue= GetTextValueFromNumber[GetLocationIndex.ToString()];
  554. eventAggregator.GetEvent<SetBatesEvent>().Publish(new BatesInfoUnicode
  555. {
  556. Unicode = Unicode,
  557. Status = BatesInfo
  558. });
  559. }
  560. public void ConvertInfoToItem(ref BatesHeaderFooterItem batesItem, BatesInfo batesInfo)
  561. {
  562. batesItem.TextData = batesInfo.TextData;
  563. batesItem.DigitNumber = batesInfo.DigitNumber;
  564. batesItem.ItemName = batesInfo.ItemName;
  565. batesItem.Prefix = batesInfo.Prefix;
  566. batesItem.Suffix = batesInfo.Suffix;
  567. batesItem.StarPagetNumber = batesInfo.StarPagetNumber;
  568. batesItem.margin = batesInfo.margin;
  569. batesItem.PageRange = PageRangeText;
  570. batesItem.PageRangeIndex = batesInfo.PageRangeIndex;
  571. }
  572. public void ConvertItemToInfo(BatesHeaderFooterItem batesItem, ref BatesInfo batesInfo)
  573. {
  574. batesInfo.TextData = batesItem.TextData;
  575. batesInfo.ItemName = batesItem.ItemName;
  576. batesInfo.DigitNumber = batesItem.DigitNumber;
  577. batesInfo.Prefix = batesItem.Prefix;
  578. batesInfo.Suffix = batesItem.Suffix;
  579. batesInfo.StarPagetNumber = batesItem.StarPagetNumber;
  580. batesInfo.margin = batesItem.margin;
  581. batesInfo.PageRangeIndex = batesItem.PageRangeIndex;
  582. batesInfo.PageRange=batesItem.PageRange;
  583. //EditToolsHelper.GetPageRange(batesInfo.PageRangeIndex, PDFViewer.Document, ref batesInfo.PageRange, batesItem.PageRange);
  584. }
  585. public void SaveCurrentTemplate()
  586. {
  587. var batesItem = new BatesHeaderFooterItem();
  588. ConvertInfoToItem(ref batesItem, BatesInfo);
  589. batesItem.ItemName += Settings.Default.BatesTemplateList.Count().ToString();
  590. try
  591. {
  592. //创建缓存文件夹
  593. string folderPath = Path.Combine(App.CurrentPath, "Bates");
  594. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  595. //保险措施(猜测)
  596. if (File.Exists(folderPath))
  597. {
  598. File.Delete(folderPath);
  599. }
  600. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  601. if (!tempfolder.Exists)
  602. {
  603. tempfolder.Create();
  604. }
  605. //预览图缓存
  606. string saveName = Guid.NewGuid().ToString();
  607. string savePath = Path.Combine(folderPath, saveName);
  608. Settings.Default.BatesTemplateList.Add(batesItem);
  609. Settings.Default.Save();
  610. }
  611. catch
  612. {
  613. }
  614. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  615. {
  616. Unicode = Unicode,
  617. Status = EnumTemplateListOrCreate.StatusTemplate
  618. });
  619. }
  620. public void ConfirmEditBackgroundTemplateItem()
  621. {
  622. var batesItem = new BatesHeaderFooterItem();
  623. ConvertInfoToItem(ref batesItem, BatesInfo);
  624. try
  625. {
  626. //创建缓存文件夹
  627. string folderPath = Path.Combine(App.CurrentPath, "Background");
  628. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  629. //保险措施(猜测)
  630. if (File.Exists(folderPath))
  631. {
  632. File.Delete(folderPath);
  633. }
  634. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  635. if (!tempfolder.Exists)
  636. {
  637. tempfolder.Create();
  638. }
  639. Settings.Default.BatesTemplateList[TemplateBatesItem.listIndex] = batesItem;
  640. Settings.Default.Save();
  641. }
  642. catch
  643. {
  644. }
  645. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  646. {
  647. Unicode = Unicode,
  648. Status = EnumTemplateListOrCreate.StatusTemplate
  649. });
  650. }
  651. public void InitComponentBySelectedInfo()
  652. {
  653. ConvertItemToInfo(TemplateBatesItem, ref BatesInfo);
  654. for (int i = 0; i <= 5; i++)
  655. {
  656. GetTextValueFromNumber[i.ToString()] = BatesInfo.TextData[i].text;
  657. }
  658. MarginTopValue = BatesInfo.margin[0].ToString();
  659. MarginRightValue = BatesInfo.margin[1].ToString();
  660. MarginDownValue = BatesInfo.margin[2].ToString();
  661. MarginLeftValue = BatesInfo.margin[3].ToString();
  662. PrefixTextValue = BatesInfo.Prefix;
  663. SurfixTextValue = BatesInfo.Suffix;
  664. DigitNumberValue = BatesInfo.DigitNumber.ToString();
  665. FontNameSelectedIndex = FontNameList.IndexOf(BatesInfo.TextData[0].fontName);
  666. FontSizeSelectedIndex = FontSizeList.IndexOf(BatesInfo.TextData[0].fontSize.ToString());
  667. StarPagetNumberIndex = StarPagetNumberList.IndexOf(BatesInfo.StarPagetNumber.ToString());
  668. StringColor = EditToolsHelper.ConvertColor(BatesInfo.TextData[0].Color);
  669. SolidColorBrush = new SolidColorBrush(StringColor);
  670. PageRangeSelectIndex = BatesInfo.PageRangeIndex;
  671. if (BatesInfo.PageRangeIndex == 3) { PageRangeSelectIndex = 0; }
  672. PageRangeText =BatesInfo.PageRange;
  673. }
  674. public bool IsNavigationTarget(NavigationContext navigationContext)
  675. {
  676. return true;
  677. }
  678. public void OnNavigatedFrom(NavigationContext navigationContext)
  679. {
  680. }
  681. public void OnNavigatedTo(NavigationContext navigationContext)
  682. {
  683. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  684. MaxPageRange = PDFViewer.Document.PageCount;
  685. InitStarPagetNumberList(MaxPageRange);
  686. if (navigationContext.Parameters.TryGetValue<BatesHeaderFooterItem>("BatesItem", out TemplateBatesItem))
  687. {
  688. CreateOrEdit = EnumCreateOrEdit.StatusEdit;
  689. BatesInfo.PageRange = TemplateBatesItem.PageRange;
  690. InitComponentBySelectedInfo();
  691. }
  692. else
  693. {
  694. CreateOrEdit = EnumCreateOrEdit.StatusCreate;
  695. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref BatesInfo.PageRange, PageRangeText);
  696. }
  697. StarPagetNumberIndex = 0;
  698. }
  699. }
  700. }