WatermarkCreateTextContentViewModel.cs 34 KB

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