HomePageWatermarkCreateTextContentViewModel.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFWatermark;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model.EditTools.Watermark;
  7. using PDF_Master.Model;
  8. using PDF_Master.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.Diagnostics;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Windows.Media;
  20. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageWatermark
  21. {
  22. public class HomePageWatermarkCreateTextContentViewModel : BindableBase, INavigationAware
  23. {
  24. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  25. private WatermarkItem TemplateWatermarkItem;
  26. IEventAggregator eventAggregator;
  27. private CPDFViewer PDFViewer;
  28. private List<string> _opacityList = new List<string>();
  29. public List<string> OpacityList
  30. {
  31. get { return _opacityList; }
  32. set
  33. {
  34. SetProperty(ref _opacityList, value);
  35. }
  36. }
  37. private void InitOpacityList()
  38. {
  39. OpacityList.Clear();
  40. for (int temp = 0; temp <= 100; temp += 10)
  41. {
  42. OpacityList.Add(temp.ToString() + " %");
  43. }
  44. }
  45. private List<string> _rotationList = new List<string>();
  46. public List<string> RotationList
  47. {
  48. get { return _rotationList; }
  49. set
  50. {
  51. SetProperty(ref _rotationList, value);
  52. }
  53. }
  54. private void InitRotationList()
  55. {
  56. RotationList.Clear();
  57. for (int temp = -45; temp <= 45; temp += 15)
  58. {
  59. RotationList.Add(temp.ToString());
  60. }
  61. }
  62. private List<string> _fontNameList = new List<string>();
  63. public List<string> FontNameList
  64. {
  65. get { return _fontNameList; }
  66. set
  67. {
  68. SetProperty(ref _fontNameList, value);
  69. }
  70. }
  71. private void InitFontNameList()
  72. {
  73. FontNameList.Clear();
  74. FontNameList.Add("Courier");
  75. FontNameList.Add("Courier-Bold");
  76. FontNameList.Add("Courier-Oblique");
  77. FontNameList.Add("Courier-BoldOblique");
  78. FontNameList.Add("Helvetica");
  79. FontNameList.Add("Helvetica-Bold");
  80. FontNameList.Add("Helvetica-Oblique");
  81. FontNameList.Add("Helvetica-BoldOblique");
  82. FontNameList.Add("Times-Roman");
  83. FontNameList.Add("Times-Bold");
  84. FontNameList.Add("Times-Italic");
  85. FontNameList.Add("Times-BoldItalic");
  86. }
  87. private List<string> _fontSizeList = new List<string>();
  88. public List<string> FontSizeList
  89. {
  90. get { return _fontSizeList; }
  91. set
  92. {
  93. SetProperty(ref _fontSizeList, value);
  94. }
  95. }
  96. private void InitFontSizeList()
  97. {
  98. FontSizeList.Clear();
  99. FontSizeList.Add("自动");
  100. for (int temp = 8; temp <= 15; temp += 1)
  101. {
  102. FontSizeList.Add(temp.ToString() + "pt");
  103. }
  104. }
  105. private List<string> _scaleList = new List<string>();
  106. public List<string> ScaleList
  107. {
  108. get { return _scaleList; }
  109. set
  110. {
  111. SetProperty(ref _scaleList, value);
  112. }
  113. }
  114. private void InitScaleList()
  115. {
  116. ScaleList.Clear();
  117. for (int temp = 0; temp <= 100; temp += 10)
  118. {
  119. ScaleList.Add(temp.ToString() + " %");
  120. }
  121. }
  122. private List<string> _isFrontList = new List<string>();
  123. public List<string> IsFrontList
  124. {
  125. get { return _isFrontList; }
  126. set
  127. {
  128. SetProperty(ref _isFrontList, value);
  129. }
  130. }
  131. private void InitIsFrontList()
  132. {
  133. IsFrontList.Clear();
  134. IsFrontList.Add("位于页面上方");
  135. IsFrontList.Add("位于页面下方");
  136. }
  137. private string _textValue = "一个水印";
  138. public string TextValue
  139. {
  140. get { return _textValue; }
  141. set
  142. {
  143. SetProperty(ref _textValue, value);
  144. }
  145. }
  146. private int _rotationValue = 0;
  147. public int RotationValue
  148. {
  149. get { return _rotationValue; }
  150. set
  151. {
  152. SetProperty(ref _rotationValue, value);
  153. WatermarkInfo.Rotation = RotationValue;
  154. }
  155. }
  156. private int _opacityValue = 100;
  157. public int OpacityValue
  158. {
  159. get { return _opacityValue; }
  160. set
  161. {
  162. SetProperty(ref _opacityValue, value);
  163. WatermarkInfo.Opacity = (byte)OpacityValue;
  164. }
  165. }
  166. private int _relativeScaleValue = 50;
  167. public int RelativeScaleValue
  168. {
  169. get { return _relativeScaleValue; }
  170. set
  171. {
  172. SetProperty(ref _relativeScaleValue, value);
  173. WatermarkInfo.TextSize = (float)RelativeScaleValue / 100;
  174. }
  175. }
  176. private string _rotationText = "0";
  177. public string RotationText
  178. {
  179. get { return _rotationText; }
  180. set
  181. {
  182. SetProperty(ref _rotationText, value);
  183. }
  184. }
  185. private string _opacityText = "100 %";
  186. public string OpacityText
  187. {
  188. get { return _opacityText; }
  189. set
  190. {
  191. SetProperty(ref _opacityText, value);
  192. }
  193. }
  194. private string _relativeScaleText = "50 %";
  195. public string RelativeScaleText
  196. {
  197. get { return _relativeScaleText; }
  198. set
  199. {
  200. SetProperty(ref _relativeScaleText, value);
  201. }
  202. }
  203. private string _vertOffsetValue = "0";
  204. public string VertOffsetValue
  205. {
  206. get { return _vertOffsetValue; }
  207. set
  208. {
  209. SetProperty(ref _vertOffsetValue, value);
  210. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  211. }
  212. }
  213. private string _horizOffsetValue = "0";
  214. public string HorizOffsetValue
  215. {
  216. get { return _horizOffsetValue; }
  217. set
  218. {
  219. SetProperty(ref _horizOffsetValue, value);
  220. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  221. }
  222. }
  223. private string _verticalSpacingValue = "6";
  224. public string VerticalSpacingValue
  225. {
  226. get { return _verticalSpacingValue; }
  227. set
  228. {
  229. SetProperty(ref _verticalSpacingValue, value);
  230. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  231. }
  232. }
  233. private string _horizontalSpacingValue = "6";
  234. public string HorizontalSpacingValue
  235. {
  236. get { return _horizontalSpacingValue; }
  237. set
  238. {
  239. SetProperty(ref _horizontalSpacingValue, value);
  240. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  241. }
  242. }
  243. private int _fontSizeSelectedIndex = 0;
  244. public int FontSizeSelectedIndex
  245. {
  246. get { return _fontSizeSelectedIndex; }
  247. set
  248. {
  249. SetProperty(ref _fontSizeSelectedIndex, value);
  250. SetFontSize(FontSizeSelectedIndex);
  251. }
  252. }
  253. private int _fontNameSelectedIndex = 0;
  254. public int FontNameSelectedIndex
  255. {
  256. get { return _fontNameSelectedIndex; }
  257. set
  258. {
  259. SetProperty(ref _fontNameSelectedIndex, value);
  260. SetFontName(FontNameSelectedIndex);
  261. }
  262. }
  263. private int _isFrontSelectedIndex = 0;
  264. public int IsFrontSelectedIndex
  265. {
  266. get { return _isFrontSelectedIndex; }
  267. set
  268. {
  269. SetProperty(ref _isFrontSelectedIndex, value);
  270. SetIsFront(IsFrontSelectedIndex);
  271. }
  272. }
  273. private bool _isFullValue = false;
  274. public bool IsFullValue
  275. {
  276. get { return _isFullValue; }
  277. set
  278. {
  279. SetProperty(ref _isFullValue, value);
  280. WatermarkInfo.Isfull = IsFullValue;
  281. if (IsRelativeScaleValue == true) { IsRelativeScaleValue = false; }
  282. }
  283. }
  284. private bool _isRelativeScaleValue = false;
  285. public bool IsRelativeScaleValue
  286. {
  287. get { return _isRelativeScaleValue; }
  288. set
  289. {
  290. SetProperty(ref _isRelativeScaleValue, value);
  291. WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
  292. if (WatermarkInfo.IsRelativeScale)
  293. {
  294. WatermarkInfo.TextSize = (float)RelativeScaleValue / 100;
  295. }
  296. else
  297. {
  298. WatermarkInfo.TextSize = 0.5f;
  299. FontSizeSelectedIndex = 0;
  300. }
  301. }
  302. }
  303. private SolidColorBrush _solidColorBrush = null;
  304. public SolidColorBrush SolidColorBrush
  305. {
  306. get
  307. {
  308. return _solidColorBrush;
  309. }
  310. set
  311. {
  312. SetProperty(ref _solidColorBrush, value);
  313. }
  314. }
  315. private Color _stringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  316. public Color StringColor
  317. {
  318. get
  319. {
  320. return _stringColor;
  321. }
  322. set
  323. {
  324. SetProperty(ref _stringColor, value);
  325. WatermarkInfo.TextColor = EditToolsHelper.ConvertColor(value);
  326. SolidColorBrush = new SolidColorBrush(_stringColor);
  327. }
  328. }
  329. public string _pageRangeText = "0";
  330. public string PageRangeText
  331. {
  332. get { return _pageRangeText; }
  333. set
  334. {
  335. _pageRangeText = value;
  336. WatermarkInfo.PageRange = PageRangeText;
  337. }
  338. }
  339. private int _pageRangeSelectIndex = 0;
  340. public int PageRangeSelectIndex
  341. {
  342. get { return _pageRangeSelectIndex; }
  343. set
  344. {
  345. SetProperty(ref _pageRangeSelectIndex, value);
  346. WatermarkInfo.PageRangeIndex = PageRangeSelectIndex;
  347. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  348. }
  349. }
  350. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  351. public ObservableDictionary<string, bool> GetLocationFromNumber
  352. {
  353. get { return _getLocationFromNumber; }
  354. set { _getLocationFromNumber = value; }
  355. }
  356. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  357. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  358. public DelegateCommand<object> TextValueChangedCommand { get; set; }
  359. public string Unicode = null;
  360. public HomePageWatermarkCreateTextContentViewModel(IEventAggregator eventAggregator)
  361. {
  362. this.eventAggregator = eventAggregator;
  363. WatermarkInfo.WatermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  364. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  365. SelectedColorChangedCommand = new DelegateCommand<object>(ChangedColor);
  366. TextValueChangedCommand = new DelegateCommand<object>(TextValueChanged);
  367. InitList();
  368. InitWatermarkInfo();
  369. InitLocationButtonMatrix();
  370. eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Subscribe(SaveWatermarkTemplate, e => e.Unicode == Unicode);
  371. eventAggregator.GetEvent<ConfirmEditWatermarkTemplateItemEvent>().Subscribe(ConfirmEditWatermarkTemplateItem, e => e.Unicode == Unicode);
  372. }
  373. public void SaveWatermarkTemplate(EnumTextOrFileUnicode enumTextOrFileunicode)
  374. {
  375. EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
  376. if (enumTextOrFile == EnumTextOrFile.StatusText)
  377. {
  378. SaveCurrentTemplate();
  379. }
  380. }
  381. public void ConvertInfoToItem(ref WatermarkItem watermarkItem, WatermarkInfo watermarkInfo)
  382. {
  383. if (watermarkInfo != null)
  384. {
  385. watermarkItem.type = watermarkInfo.WatermarkType;
  386. watermarkItem.text = watermarkInfo.Text;
  387. watermarkItem.fontSize = watermarkInfo.TextSize;
  388. watermarkItem.fontName = watermarkInfo.FontName;
  389. watermarkItem.textcolor = watermarkInfo.TextColor;
  390. watermarkItem.horizalign = watermarkInfo.WatermarkHorizalign;
  391. watermarkItem.vertalign = watermarkInfo.WatermarkVertalign;
  392. watermarkItem.isFullScreen = watermarkInfo.Isfull;
  393. watermarkItem.isRelativeScale = watermarkInfo.IsRelativeScale;
  394. watermarkItem.isfront = watermarkInfo.IsFront;
  395. watermarkItem.opacity = watermarkInfo.Opacity;
  396. watermarkItem.rotation = watermarkInfo.Rotation;
  397. watermarkItem.vertOffset = watermarkInfo.VertOffset;
  398. watermarkItem.horizOffset = watermarkInfo.HorizOffset;
  399. watermarkItem.verticalSpacing = watermarkInfo.VerticalSpacing;
  400. watermarkItem.horizontalSpacing = watermarkInfo.HorizontalSpacing;
  401. watermarkItem.pageRange = watermarkInfo.PageRange;
  402. List<int> PageIndexLists = new List<int>();
  403. if (PageRangeSelectIndex == 3 && !CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, 99999, new char[] { ',' }, new char[] { '-' }))
  404. {
  405. watermarkInfo.PageRangeIndex = 0;
  406. }
  407. watermarkItem.pageRangeIndex = watermarkInfo.PageRangeIndex;
  408. }
  409. }
  410. public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
  411. {
  412. if (watermarkItem != null)
  413. {
  414. watermarkInfo.WatermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  415. watermarkInfo.Text = watermarkItem.text;
  416. watermarkInfo.TextSize = watermarkItem.fontSize;
  417. watermarkInfo.FontName = watermarkItem.fontName;
  418. watermarkInfo.TextColor = watermarkItem.textcolor;
  419. watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
  420. watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
  421. watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
  422. watermarkInfo.Isfull = watermarkItem.isFullScreen;
  423. watermarkInfo.IsFront = watermarkItem.isfront;
  424. watermarkInfo.Opacity = watermarkItem.opacity;
  425. watermarkInfo.Rotation = watermarkItem.rotation;
  426. watermarkInfo.VertOffset = watermarkItem.vertOffset;
  427. watermarkInfo.HorizOffset = watermarkItem.horizOffset;
  428. watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
  429. watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
  430. watermarkInfo.PageRange = watermarkItem.pageRange;
  431. watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
  432. //EditToolsHelper.GetPageRange(watermarkItem.pageRangeIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, watermarkItem.pageRange);
  433. }
  434. }
  435. public void SaveCurrentTemplate()
  436. {
  437. var watermarkItem = new WatermarkItem();
  438. ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
  439. watermarkItem.templateName += Settings.Default.WatermarkIndex.ToString();
  440. try
  441. {
  442. //创建缓存文件夹
  443. string folderPath = Path.Combine(App.CurrentPath, "Watermark");
  444. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  445. //保险措施(猜测)
  446. if (File.Exists(folderPath))
  447. {
  448. File.Delete(folderPath);
  449. }
  450. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  451. if (!tempfolder.Exists)
  452. {
  453. tempfolder.Create();
  454. }
  455. //预览图缓存
  456. string saveName = Guid.NewGuid().ToString();
  457. string savePath = Path.Combine(folderPath, saveName);
  458. System.Windows.Size pageSize = new System.Windows.Size { Width = 128, Height = 160 };
  459. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  460. if (tempDocument != null)
  461. {
  462. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  463. CreateWatermark(tempDocument, WatermarkInfo);
  464. //获取透明背景的图片
  465. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  466. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  467. {
  468. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  469. }
  470. watermarkItem.previewImagePath = savePath;
  471. tempDocument.Release();
  472. Settings.Default.WatermarkTemplateList.Add(watermarkItem);
  473. Settings.Default.Save();
  474. }
  475. }
  476. catch
  477. {
  478. }
  479. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  480. {
  481. Unicode = Unicode,
  482. Status = EnumTemplateListOrCreate.StatusTemplate
  483. });
  484. }
  485. private void InitLocationButtonMatrix()
  486. {
  487. GetLocationFromNumber.Clear();
  488. for (var temp = 0; temp <= 22; temp++)
  489. {
  490. GetLocationFromNumber.Add(temp.ToString(), true);
  491. if (temp % 10 == 2)
  492. {
  493. temp += 7;
  494. }
  495. }
  496. int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
  497. GetLocationFromNumber[Num.ToString()] = false;
  498. }
  499. public void ChangeLocation(object e)
  500. {
  501. string args = e as string;
  502. if (args != null)
  503. {
  504. WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
  505. WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
  506. InitLocationButtonMatrix();
  507. }
  508. }
  509. private void ChangedColor(object obj)
  510. {
  511. if (obj != null)
  512. {
  513. var colorValue = (Color)obj;
  514. if (colorValue != null)
  515. {
  516. StringColor = colorValue;
  517. }
  518. }
  519. }
  520. private void TextValueChanged(object obj)
  521. {
  522. if (obj != null)
  523. {
  524. var textValue = obj as System.Windows.Controls.TextBox;
  525. if (textValue != null)
  526. {
  527. TextValue = textValue.Text;
  528. WatermarkInfo.Text = TextValue;
  529. }
  530. }
  531. }
  532. private void SetFontName(int Index)
  533. {
  534. WatermarkInfo.FontName = FontNameList[Index];
  535. }
  536. private void SetFontSize(int Index)
  537. {
  538. if (Index == 0)
  539. {
  540. WatermarkInfo.IsRelativeScale = true;
  541. WatermarkInfo.TextSize = 0.5f;
  542. }
  543. else
  544. {
  545. WatermarkInfo.IsRelativeScale = false;
  546. WatermarkInfo.TextSize = (Index + 7) * 1.33f;
  547. }
  548. }
  549. private void SetIsFront(int Index)
  550. {
  551. if (Index == 0)
  552. {
  553. WatermarkInfo.IsFront = true;
  554. }
  555. if (Index == 1)
  556. {
  557. WatermarkInfo.IsFront = false;
  558. }
  559. }
  560. public void ConfirmEditToolsWatermark()
  561. {
  562. }
  563. private void InitList()
  564. {
  565. InitOpacityList();
  566. InitFontNameList();
  567. InitFontSizeList();
  568. InitIsFrontList();
  569. InitRotationList();
  570. InitScaleList();
  571. }
  572. private void InitWatermarkInfo()
  573. {
  574. WatermarkInfo.Text = TextValue;
  575. WatermarkInfo.Rotation = RotationValue;
  576. WatermarkInfo.Opacity = (byte)OpacityValue;
  577. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  578. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  579. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  580. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  581. WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  582. WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  583. WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
  584. WatermarkInfo.Isfull = IsFullValue;
  585. SetFontName(FontNameSelectedIndex);
  586. SetFontSize(FontSizeSelectedIndex);
  587. SetIsFront(IsFrontSelectedIndex);
  588. StringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  589. }
  590. /// <summary>
  591. /// 根据已知Info初始化
  592. /// </summary>
  593. public void InitComponentBySelectedInfo()
  594. {
  595. ConvertItemToInfo(TemplateWatermarkItem, ref WatermarkInfo);
  596. TextValue = WatermarkInfo.Text;
  597. StringColor = EditToolsHelper.ConvertColor(WatermarkInfo.TextColor);
  598. VertOffsetValue = WatermarkInfo.VertOffset.ToString();
  599. HorizOffsetValue = WatermarkInfo.HorizOffset.ToString();
  600. VerticalSpacingValue = WatermarkInfo.VerticalSpacing.ToString();
  601. HorizontalSpacingValue = WatermarkInfo.HorizontalSpacing.ToString();
  602. PageRangeSelectIndex = WatermarkInfo.PageRangeIndex;
  603. FontNameSelectedIndex = FontNameList.IndexOf(WatermarkInfo.FontName);
  604. FontSizeSelectedIndex = FontSizeList.IndexOf(((int)(WatermarkInfo.TextSize / 1.33f)).ToString() + "pt");
  605. IsFrontSelectedIndex = Convert.ToInt32(!WatermarkInfo.IsFront);
  606. RotationText = WatermarkInfo.Rotation.ToString();
  607. OpacityText = WatermarkInfo.Opacity.ToString() + " %";
  608. RelativeScaleText = WatermarkInfo.TextSize.ToString() + " %";
  609. IsFullValue = WatermarkInfo.Isfull;
  610. IsRelativeScaleValue = WatermarkInfo.IsRelativeScale;
  611. InitLocationButtonMatrix();
  612. }
  613. public void CreateWatermark(CPDFDocument document, WatermarkInfo watermarkInfo)
  614. {
  615. if (watermarkInfo != null)
  616. {
  617. document.DeleteWatermarks();
  618. CPDFWatermark watermark;
  619. if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  620. {
  621. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  622. watermark.SetText(watermarkInfo.Text);
  623. watermark.SetFontName(watermarkInfo.FontName);
  624. if (WatermarkInfo.IsRelativeScale)
  625. {
  626. watermark.SetFontSizeWithPercentage(WatermarkInfo.TextSize);
  627. }
  628. else
  629. {
  630. watermark.SetFontSize(WatermarkInfo.TextSize);
  631. }
  632. watermark.SetTextRGBColor(watermarkInfo.TextColor);
  633. }
  634. else
  635. {
  636. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  637. if (watermarkInfo.ImageArray != null)
  638. {
  639. watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
  640. }
  641. watermark.SetScale(watermarkInfo.ImageSize / 100);
  642. }
  643. watermark.SetRotation((float)((watermarkInfo.Rotation / 180) * Math.PI));
  644. watermark.SetOpacity((byte)(((float)watermarkInfo.Opacity / 100) * 255));
  645. watermark.SetFront(watermarkInfo.IsFront);
  646. watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
  647. watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
  648. watermark.SetFullScreen(watermarkInfo.Isfull);
  649. watermark.SetVertOffset(watermarkInfo.VertOffset);
  650. watermark.SetHorizOffset(watermarkInfo.HorizOffset);
  651. watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
  652. watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
  653. watermark.SetPages("0");
  654. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  655. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  656. }
  657. }
  658. public void ConfirmEditWatermarkTemplateItem(EnumTextOrFileUnicode enumTextOrFileunicode)
  659. {
  660. EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
  661. if (enumTextOrFile == EnumTextOrFile.StatusText)
  662. {
  663. var watermarkItem = new WatermarkItem();
  664. ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
  665. try
  666. {
  667. //创建缓存文件夹
  668. string folderPath = Path.Combine(App.CurrentPath, "Watermark");
  669. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  670. //保险措施(猜测)
  671. if (File.Exists(folderPath))
  672. {
  673. File.Delete(folderPath);
  674. }
  675. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  676. if (!tempfolder.Exists)
  677. {
  678. tempfolder.Create();
  679. }
  680. //预览图缓存
  681. string saveName = Guid.NewGuid().ToString();
  682. string savePath = Path.Combine(folderPath, saveName);
  683. System.Windows.Size pageSize = new System.Windows.Size { Width = 128, Height = 160 };
  684. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  685. if (tempDocument != null)
  686. {
  687. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  688. CreateWatermark(tempDocument, WatermarkInfo);
  689. //获取透明背景的图片
  690. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  691. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  692. {
  693. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  694. }
  695. watermarkItem.previewImagePath = savePath;
  696. tempDocument.Release();
  697. Settings.Default.WatermarkTemplateList[TemplateWatermarkItem.listIndex] = watermarkItem;
  698. Settings.Default.Save();
  699. }
  700. }
  701. catch
  702. {
  703. }
  704. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  705. {
  706. Unicode = Unicode,
  707. Status = EnumTemplateListOrCreate.StatusTemplate
  708. });
  709. }
  710. }
  711. public bool IsNavigationTarget(NavigationContext navigationContext)
  712. {
  713. return true;
  714. }
  715. public void OnNavigatedFrom(NavigationContext navigationContext)
  716. {
  717. }
  718. public void OnNavigatedTo(NavigationContext navigationContext)
  719. {
  720. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  721. navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
  722. //EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  723. if (navigationContext.Parameters.TryGetValue<WatermarkItem>("WatermarkItem", out TemplateWatermarkItem))
  724. {
  725. InitComponentBySelectedInfo();
  726. }
  727. }
  728. }
  729. }