HomePageWatermarkCreateTextContentViewModel.cs 29 KB

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