WatermarkCreateFileContentViewModel.cs 30 KB

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