WatermarkCreateFileContentViewModel.cs 32 KB

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