WatermarkCreateTextContentViewModel.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. }
  190. }
  191. private string _rotationText = "0";
  192. public string RotationText
  193. {
  194. get { return _rotationText; }
  195. set
  196. {
  197. SetProperty(ref _rotationText, value);
  198. }
  199. }
  200. private string _opacityText = "100 %";
  201. public string OpacityText
  202. {
  203. get { return _opacityText; }
  204. set
  205. {
  206. SetProperty(ref _opacityText, value);
  207. }
  208. }
  209. private string _relativeScaleText = "50 %";
  210. public string RelativeScaleText
  211. {
  212. get { return _relativeScaleText; }
  213. set
  214. {
  215. SetProperty(ref _relativeScaleText, value);
  216. }
  217. }
  218. private string _vertOffsetValue = "0";
  219. public string VertOffsetValue
  220. {
  221. get { return _vertOffsetValue; }
  222. set
  223. {
  224. SetProperty(ref _vertOffsetValue, value);
  225. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  226. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  227. {
  228. Unicode = Unicode,
  229. Status = WatermarkInfo
  230. });
  231. }
  232. }
  233. private string _horizOffsetValue = "0";
  234. public string HorizOffsetValue
  235. {
  236. get { return _horizOffsetValue; }
  237. set
  238. {
  239. SetProperty(ref _horizOffsetValue, value);
  240. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  241. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  242. {
  243. Unicode = Unicode,
  244. Status = WatermarkInfo
  245. });
  246. }
  247. }
  248. private string _verticalSpacingValue = "6";
  249. public string VerticalSpacingValue
  250. {
  251. get { return _verticalSpacingValue; }
  252. set
  253. {
  254. SetProperty(ref _verticalSpacingValue, value);
  255. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  256. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  257. {
  258. Unicode = Unicode,
  259. Status = WatermarkInfo
  260. });
  261. }
  262. }
  263. private string _horizontalSpacingValue = "6";
  264. public string HorizontalSpacingValue
  265. {
  266. get { return _horizontalSpacingValue; }
  267. set
  268. {
  269. SetProperty(ref _horizontalSpacingValue, value);
  270. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  271. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  272. {
  273. Unicode = Unicode,
  274. Status = WatermarkInfo
  275. });
  276. }
  277. }
  278. private int _fontSizeSelectedIndex = 0;
  279. public int FontSizeSelectedIndex
  280. {
  281. get { return _fontSizeSelectedIndex; }
  282. set
  283. {
  284. SetProperty(ref _fontSizeSelectedIndex, value);
  285. SetFontSize(FontSizeSelectedIndex);
  286. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  287. {
  288. Unicode = Unicode,
  289. Status = WatermarkInfo
  290. });
  291. }
  292. }
  293. private int _fontNameSelectedIndex = 0;
  294. public int FontNameSelectedIndex
  295. {
  296. get { return _fontNameSelectedIndex; }
  297. set
  298. {
  299. SetProperty(ref _fontNameSelectedIndex, value);
  300. SetFontName(FontNameSelectedIndex);
  301. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  302. {
  303. Unicode = Unicode,
  304. Status = WatermarkInfo
  305. });
  306. }
  307. }
  308. private int _isFrontSelectedIndex = 0;
  309. public int IsFrontSelectedIndex
  310. {
  311. get { return _isFrontSelectedIndex; }
  312. set
  313. {
  314. SetProperty(ref _isFrontSelectedIndex, value);
  315. SetIsFront(IsFrontSelectedIndex);
  316. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  317. {
  318. Unicode = Unicode,
  319. Status = WatermarkInfo
  320. });
  321. }
  322. }
  323. private bool _isFullValue = false;
  324. public bool IsFullValue
  325. {
  326. get { return _isFullValue; }
  327. set
  328. {
  329. SetProperty(ref _isFullValue, value);
  330. WatermarkInfo.Isfull = IsFullValue;
  331. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  332. {
  333. Unicode = Unicode,
  334. Status = WatermarkInfo
  335. });
  336. }
  337. }
  338. private bool _isRelativeScaleValue = false;
  339. public bool IsRelativeScaleValue
  340. {
  341. get { return _isRelativeScaleValue; }
  342. set
  343. {
  344. SetProperty(ref _isRelativeScaleValue, value);
  345. WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
  346. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  347. {
  348. Unicode = Unicode,
  349. Status = WatermarkInfo
  350. });
  351. }
  352. }
  353. private SolidColorBrush _solidColorBrush = null;
  354. public SolidColorBrush SolidColorBrush
  355. {
  356. get
  357. {
  358. return _solidColorBrush;
  359. }
  360. set
  361. {
  362. SetProperty(ref _solidColorBrush, value);
  363. }
  364. }
  365. private Color _stringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  366. public Color StringColor
  367. {
  368. get
  369. {
  370. return _stringColor;
  371. }
  372. set
  373. {
  374. SetProperty(ref _stringColor, value);
  375. WatermarkInfo.TextColor = EditToolsHelper.ConvertColor(value);
  376. SolidColorBrush = new SolidColorBrush(_stringColor);
  377. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  378. {
  379. Unicode = Unicode,
  380. Status = WatermarkInfo
  381. });
  382. }
  383. }
  384. public string _pageRangeText = "0";
  385. public string PageRangeText
  386. {
  387. get { return _pageRangeText; }
  388. set
  389. {
  390. _pageRangeText = value;
  391. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  392. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  393. {
  394. Unicode = Unicode,
  395. Status = WatermarkInfo
  396. });
  397. }
  398. }
  399. private int _pageRangeSelectIndex = 0;
  400. public int PageRangeSelectIndex
  401. {
  402. get { return _pageRangeSelectIndex; }
  403. set
  404. {
  405. SetProperty(ref _pageRangeSelectIndex, value);
  406. WatermarkInfo.PageRangeIndex = PageRangeSelectIndex;
  407. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  408. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  409. {
  410. Unicode = Unicode,
  411. Status = WatermarkInfo
  412. });
  413. }
  414. }
  415. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  416. public ObservableDictionary<string, bool> GetLocationFromNumber
  417. {
  418. get { return _getLocationFromNumber; }
  419. set { _getLocationFromNumber = value; }
  420. }
  421. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  422. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  423. public DelegateCommand<object> TextValueChangedCommand { get; set; }
  424. public string Unicode = null;
  425. public WatermarkCreateTextContentViewModel(IEventAggregator eventAggregator)
  426. {
  427. this.eventAggregator = eventAggregator;
  428. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  429. WatermarkInfo.WatermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  430. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  431. SelectedColorChangedCommand = new DelegateCommand<object>(ChangedColor);
  432. TextValueChangedCommand = new DelegateCommand<object>(TextValueChanged);
  433. InitList();
  434. InitWatermarkInfo();
  435. InitLocationButtonMatrix();
  436. eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Subscribe(SaveWatermarkTemplate, e => e.Unicode == Unicode);
  437. eventAggregator.GetEvent<ConfirmEditWatermarkTemplateItemEvent>().Subscribe(ConfirmEditWatermarkTemplateItem, e => e.Unicode == Unicode);
  438. }
  439. public void SaveWatermarkTemplate(EnumTextOrFileUnicode enumTextOrFileunicode)
  440. {
  441. EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
  442. if (enumTextOrFile == EnumTextOrFile.StatusText)
  443. {
  444. SaveCurrentTemplate();
  445. }
  446. }
  447. public void ConvertInfoToItem(ref WatermarkItem watermarkItem, WatermarkInfo watermarkInfo)
  448. {
  449. if (watermarkInfo != null)
  450. {
  451. watermarkItem.type = watermarkInfo.WatermarkType;
  452. watermarkItem.text = watermarkInfo.Text;
  453. watermarkItem.fontSize = watermarkInfo.TextSize;
  454. watermarkItem.fontName = watermarkInfo.FontName;
  455. watermarkItem.textcolor = watermarkInfo.TextColor;
  456. watermarkItem.horizalign = watermarkInfo.WatermarkHorizalign;
  457. watermarkItem.vertalign = watermarkInfo.WatermarkVertalign;
  458. watermarkItem.isFullScreen = watermarkInfo.Isfull;
  459. watermarkItem.isRelativeScale = watermarkInfo.IsRelativeScale;
  460. watermarkItem.isfront = watermarkInfo.IsFront;
  461. watermarkItem.opacity = watermarkInfo.Opacity;
  462. watermarkItem.rotation = watermarkInfo.Rotation;
  463. watermarkItem.vertOffset = watermarkInfo.VertOffset;
  464. watermarkItem.horizOffset = watermarkInfo.HorizOffset;
  465. watermarkItem.verticalSpacing = watermarkInfo.VerticalSpacing;
  466. watermarkItem.horizontalSpacing = watermarkInfo.HorizontalSpacing;
  467. watermarkItem.pageRangeIndex = watermarkInfo.PageRangeIndex;
  468. watermarkItem.pageRange = watermarkInfo.PageRange;
  469. }
  470. }
  471. public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
  472. {
  473. if (watermarkItem != null)
  474. {
  475. watermarkInfo.WatermarkType = C_Watermark_Type.WATERMARK_TYPE_TEXT;
  476. watermarkInfo.Text = watermarkItem.text;
  477. watermarkInfo.TextSize = watermarkItem.fontSize;
  478. watermarkInfo.FontName = watermarkItem.fontName;
  479. watermarkInfo.TextColor = watermarkItem.textcolor;
  480. watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
  481. watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
  482. watermarkInfo.IsRelativeScale=watermarkItem.isRelativeScale;
  483. watermarkInfo.Isfull = watermarkItem.isFullScreen;
  484. watermarkInfo.IsFront = watermarkItem.isfront;
  485. watermarkInfo.Opacity = watermarkItem.opacity;
  486. watermarkInfo.Rotation = watermarkItem.rotation;
  487. watermarkInfo.VertOffset = watermarkItem.vertOffset;
  488. watermarkInfo.HorizOffset = watermarkItem.horizOffset;
  489. watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
  490. watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
  491. watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
  492. EditToolsHelper.GetPageRange(watermarkItem.pageRangeIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, watermarkItem.pageRange);
  493. }
  494. }
  495. public void SaveCurrentTemplate()
  496. {
  497. var watermarkItem = new WatermarkItem();
  498. ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
  499. watermarkItem.templateName += Settings.Default.WatermarkIndex.ToString();
  500. try
  501. {
  502. //创建缓存文件夹
  503. string folderPath = Path.Combine(App.CurrentPath, "Watermark");
  504. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  505. //保险措施(猜测)
  506. if (File.Exists(folderPath))
  507. {
  508. File.Delete(folderPath);
  509. }
  510. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  511. if (!tempfolder.Exists)
  512. {
  513. tempfolder.Create();
  514. }
  515. //预览图缓存
  516. string saveName = Guid.NewGuid().ToString();
  517. string savePath = Path.Combine(folderPath, saveName);
  518. System.Windows.Size pageSize = new System.Windows.Size { Width = 128, Height = 160 };
  519. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  520. if (tempDocument != null)
  521. {
  522. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  523. CreateWatermark(tempDocument, WatermarkInfo);
  524. //获取透明背景的图片
  525. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  526. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  527. {
  528. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  529. }
  530. watermarkItem.previewImagePath = savePath;
  531. tempDocument.Release();
  532. Settings.Default.WatermarkTemplateList.Add(watermarkItem);
  533. Settings.Default.Save();
  534. }
  535. }
  536. catch
  537. {
  538. }
  539. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  540. {
  541. Unicode = Unicode,
  542. Status = EnumTemplateListOrCreate.StatusTemplate
  543. });
  544. }
  545. private void InitLocationButtonMatrix()
  546. {
  547. GetLocationFromNumber.Clear();
  548. for (var temp = 0; temp <= 22; temp++)
  549. {
  550. GetLocationFromNumber.Add(temp.ToString(), true);
  551. if (temp % 10 == 2)
  552. {
  553. temp += 7;
  554. }
  555. }
  556. int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
  557. GetLocationFromNumber[Num.ToString()] = false;
  558. }
  559. public void ChangeLocation(object e)
  560. {
  561. string args = e as string;
  562. if (args != null)
  563. {
  564. WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
  565. WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
  566. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  567. {
  568. Unicode = Unicode,
  569. Status = WatermarkInfo
  570. });
  571. InitLocationButtonMatrix();
  572. }
  573. }
  574. private void ChangedColor(object obj)
  575. {
  576. if (obj != null)
  577. {
  578. var colorValue = (Color)obj;
  579. if (colorValue != null)
  580. {
  581. StringColor = colorValue;
  582. }
  583. }
  584. }
  585. private void TextValueChanged(object obj)
  586. {
  587. if (obj != null)
  588. {
  589. var textValue = obj as System.Windows.Controls.TextBox;
  590. if (textValue != null)
  591. {
  592. TextValue = textValue.Text;
  593. WatermarkInfo.Text = TextValue;
  594. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  595. {
  596. Unicode = Unicode,
  597. Status = WatermarkInfo
  598. });
  599. }
  600. }
  601. }
  602. private void SetFontName(int Index)
  603. {
  604. WatermarkInfo.FontName = FontNameList[Index];
  605. }
  606. private void SetFontSize(int Index)
  607. {
  608. if (Index == 0)
  609. {
  610. WatermarkInfo.TextSize = 10;
  611. }
  612. else
  613. {
  614. WatermarkInfo.TextSize = (Index + 7) * 1.33f;
  615. }
  616. }
  617. private void SetIsFront(int Index)
  618. {
  619. if (Index == 0)
  620. {
  621. WatermarkInfo.IsFront = true;
  622. }
  623. if (Index == 1)
  624. {
  625. WatermarkInfo.IsFront = false;
  626. }
  627. }
  628. public void ConfirmEditToolsWatermark()
  629. {
  630. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  631. {
  632. Unicode = Unicode,
  633. Status = WatermarkInfo
  634. });
  635. }
  636. private void InitList()
  637. {
  638. InitOpacityList();
  639. InitFontNameList();
  640. InitFontSizeList();
  641. InitIsFrontList();
  642. InitRotationList();
  643. InitScaleList();
  644. }
  645. private void InitWatermarkInfo()
  646. {
  647. WatermarkInfo.Text = TextValue;
  648. WatermarkInfo.Rotation = RotationValue;
  649. WatermarkInfo.Opacity = (byte)OpacityValue;
  650. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  651. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  652. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  653. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  654. WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  655. WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  656. WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
  657. WatermarkInfo.Isfull = IsFullValue;
  658. SetFontName(FontNameSelectedIndex);
  659. SetFontSize(FontSizeSelectedIndex);
  660. SetIsFront(IsFrontSelectedIndex);
  661. StringColor = Color.FromArgb(0xFF, 0xFF, 0x00, 0x00);
  662. }
  663. /// <summary>
  664. /// 根据已知Info初始化
  665. /// </summary>
  666. public void InitComponentBySelectedInfo()
  667. {
  668. ConvertItemToInfo(TemplateWatermarkItem, ref WatermarkInfo);
  669. TextValue = WatermarkInfo.Text;
  670. StringColor = EditToolsHelper.ConvertColor(WatermarkInfo.TextColor);
  671. VertOffsetValue = WatermarkInfo.VertOffset.ToString();
  672. HorizOffsetValue = WatermarkInfo.HorizOffset.ToString();
  673. VerticalSpacingValue = WatermarkInfo.VerticalSpacing.ToString();
  674. HorizontalSpacingValue = WatermarkInfo.HorizontalSpacing.ToString();
  675. PageRangeSelectIndex = WatermarkInfo.PageRangeIndex;
  676. FontNameSelectedIndex = FontNameList.IndexOf(WatermarkInfo.FontName);
  677. FontSizeSelectedIndex = FontSizeList.IndexOf(((int)(WatermarkInfo.TextSize / 1.33f)).ToString() + "pt");
  678. IsFrontSelectedIndex = Convert.ToInt32(!WatermarkInfo.IsFront);
  679. RotationText = WatermarkInfo.Rotation.ToString();
  680. OpacityText = WatermarkInfo.Opacity.ToString() + " %";
  681. RelativeScaleText = WatermarkInfo.TextSize.ToString() + " %";
  682. IsFullValue = WatermarkInfo.Isfull;
  683. IsRelativeScaleValue = WatermarkInfo.IsRelativeScale;
  684. InitLocationButtonMatrix();
  685. }
  686. public void CreateWatermark(CPDFDocument document, WatermarkInfo watermarkInfo)
  687. {
  688. if (watermarkInfo != null)
  689. {
  690. document.DeleteWatermarks();
  691. CPDFWatermark watermark;
  692. if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  693. {
  694. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  695. watermark.SetText(watermarkInfo.Text);
  696. watermark.SetFontName(watermarkInfo.FontName);
  697. watermark.SetFontSize(watermarkInfo.TextSize);
  698. watermark.SetTextRGBColor(watermarkInfo.TextColor);
  699. }
  700. else
  701. {
  702. watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  703. if (watermarkInfo.ImageArray != null)
  704. {
  705. watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
  706. }
  707. watermark.SetScale(watermarkInfo.ImageSize / 100);
  708. }
  709. watermark.SetRotation((float)((watermarkInfo.Rotation / 180) * Math.PI));
  710. watermark.SetOpacity((byte)(((float)watermarkInfo.Opacity / 100) * 255));
  711. watermark.SetFront(watermarkInfo.IsFront);
  712. watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
  713. watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
  714. watermark.SetFullScreen(watermarkInfo.Isfull);
  715. watermark.SetVertOffset(watermarkInfo.VertOffset);
  716. watermark.SetHorizOffset(watermarkInfo.HorizOffset);
  717. watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
  718. watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
  719. watermark.SetPages("0");
  720. Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
  721. Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
  722. }
  723. }
  724. public void ConfirmEditWatermarkTemplateItem(EnumTextOrFileUnicode enumTextOrFileunicode)
  725. {
  726. EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
  727. if (enumTextOrFile == EnumTextOrFile.StatusText)
  728. {
  729. var watermarkItem = new WatermarkItem();
  730. ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
  731. try
  732. {
  733. //创建缓存文件夹
  734. string folderPath = Path.Combine(App.CurrentPath, "Watermark");
  735. //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
  736. //保险措施(猜测)
  737. if (File.Exists(folderPath))
  738. {
  739. File.Delete(folderPath);
  740. }
  741. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  742. if (!tempfolder.Exists)
  743. {
  744. tempfolder.Create();
  745. }
  746. //预览图缓存
  747. string saveName = Guid.NewGuid().ToString();
  748. string savePath = Path.Combine(folderPath, saveName);
  749. System.Windows.Size pageSize = new System.Windows.Size { Width = 128, Height = 160 };
  750. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  751. if (tempDocument != null)
  752. {
  753. tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
  754. CreateWatermark(tempDocument, WatermarkInfo);
  755. //获取透明背景的图片
  756. var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
  757. using (FileStream stream = new FileStream(savePath, FileMode.Create))
  758. {
  759. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  760. }
  761. watermarkItem.previewImagePath = savePath;
  762. tempDocument.Release();
  763. Settings.Default.WatermarkTemplateList[TemplateWatermarkItem.listIndex] = watermarkItem;
  764. Settings.Default.Save();
  765. }
  766. }
  767. catch
  768. {
  769. }
  770. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  771. {
  772. Unicode = Unicode,
  773. Status = EnumTemplateListOrCreate.StatusTemplate
  774. });
  775. }
  776. }
  777. public bool IsNavigationTarget(NavigationContext navigationContext)
  778. {
  779. return true;
  780. }
  781. public void OnNavigatedFrom(NavigationContext navigationContext)
  782. {
  783. }
  784. public void OnNavigatedTo(NavigationContext navigationContext)
  785. {
  786. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  787. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  788. if (navigationContext.Parameters.TryGetValue<WatermarkItem>("WatermarkItem", out TemplateWatermarkItem))
  789. {
  790. InitComponentBySelectedInfo();
  791. }
  792. }
  793. }
  794. }