HomePageWatermarkCreateFileContentViewModel.cs 29 KB

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