HomePageWatermarkCreateFileContentViewModel.cs 28 KB

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