WatermarkDocumentContentViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Model.EditTools.Background;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using PDF_Office.Model.EditTools.Watermark;
  13. using ComPDFKit.PDFDocument;
  14. using ComPDFKit.PDFPage;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Media;
  17. using System.Threading.Tasks;
  18. using ComPDFKit.PDFWatermark;
  19. using PDF_Office.Helper;
  20. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  21. using System.Drawing;
  22. using System.Windows;
  23. using ComPDFKitViewer;
  24. using Size = System.Windows.Size;
  25. using System.Diagnostics;
  26. using System.Windows.Controls;
  27. using ComPDFKit.Import;
  28. namespace PDF_Office.ViewModels.EditTools.Watermark
  29. {
  30. public class WatermarkDocumentContentViewModel : BindableBase, INavigationAware
  31. {
  32. public IEventAggregator eventAggregator;
  33. public IRegionManager regionManager;
  34. public CPDFViewer PDFViewer;
  35. public CPDFDocument Document;
  36. private CPDFWatermark watermark;
  37. private CPDFBackground background;
  38. private WatermarkInfo watermarkInfo;
  39. public bool firstin = true;
  40. public string ViewerRegionName { get; set; }
  41. private ImageSource _imageSource = null;
  42. public ImageSource ImageSource
  43. {
  44. get { return _imageSource; }
  45. set
  46. {
  47. SetProperty(ref _imageSource, value);
  48. }
  49. }
  50. public WatermarkDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  51. {
  52. this.regionManager = regionManager;
  53. this.eventAggregator = eventAggregator;
  54. eventAggregator.GetEvent<SetWatermarkEvent>().Subscribe(SetWatermark);
  55. ViewerRegionName = RegionNames.WatermarkViwerRegionName;
  56. }
  57. public void SetWatermark(WatermarkInfo watermarkInfo)
  58. {
  59. this.watermarkInfo = watermarkInfo;
  60. //CreateWatermark(PDFViewer.Document);
  61. //PDFViewer.Document.PdfToImage("0-1", "C:\\Users\\kdanmobile\\Desktop\\888\\");
  62. //PDFViewer.Document.WriteToFilePath("C:\\Users\\kdanmobile\\Desktop\\888\\sb22222.pdf");
  63. PDFViewer.InvalidChildVisual(false);
  64. }
  65. public void CreateWatermark(CPDFDocument document, bool IsNewDoc = false)
  66. {
  67. if (watermarkInfo != null) {
  68. background = document.GetBackground();
  69. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  70. background.SetScale(1);
  71. background.SetColor(watermarkInfo.TextColor);
  72. background.SetRotation(0);
  73. background.SetOpacity(255);
  74. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  75. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  76. background.SetYOffset(0);
  77. background.SetXOffset(0);
  78. background.SetAllowsPrint(true);
  79. background.SetAllowsView(true);
  80. background.SetPages("0");
  81. background.Update();
  82. }
  83. //if (watermarkInfo != null)
  84. //{
  85. // document.DeleteWatermarks();
  86. // if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  87. // {
  88. // watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
  89. // watermark.SetText(watermarkInfo.Text);
  90. // if (watermarkInfo.TextSize == null)
  91. // {
  92. // watermarkInfo.TextSize = "6";
  93. // }
  94. // watermark.SetFontName(watermarkInfo.FontName);
  95. // watermark.SetFontSize(float.Parse(watermarkInfo.TextSize));
  96. // watermark.SetTextRGBColor(watermarkInfo.TextColor);
  97. // }
  98. // else
  99. // {
  100. // watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  101. // if (watermarkInfo.ImageArray != null)
  102. // {
  103. // watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
  104. // }
  105. // watermark.SetScale(0.1f);
  106. // }
  107. // watermark.SetRotation(watermarkInfo.Rotation);
  108. // watermark.SetOpacity(watermarkInfo.Opacity);
  109. // watermark.SetFront(watermarkInfo.IsFront);
  110. // watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
  111. // watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
  112. // watermark.SetFullScreen(watermarkInfo.Isfull);
  113. // watermark.SetVertOffset(watermarkInfo.VertOffset);
  114. // watermark.SetHorizOffset(watermarkInfo.HorizOffset);
  115. // watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
  116. // watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
  117. // if (IsNewDoc) { watermark.SetPages("0"); }
  118. // else { watermark.SetPages(watermarkInfo.PageRange); }
  119. // watermark.CreateWatermark();
  120. // watermark.UpdateWatermark();
  121. //}
  122. }
  123. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  124. {
  125. }
  126. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  127. {
  128. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  129. {
  130. List<int> PageIndexLists = new List<int>();
  131. foreach (DrawPageData drawPageData in e.DrawPages)
  132. {
  133. if(watermarkInfo != null) {
  134. char[] enumerationSeparator = new char[] { ',' };
  135. char[] rangeSeparator = new char[] { '-' };
  136. if (CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
  137. { //TODO
  138. Trace.WriteLine(CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true));
  139. Trace.WriteLine(drawPageData.PageIndex);
  140. foreach (var abbb in PageIndexLists ) {
  141. Trace.WriteLine("smgui _______"+abbb);
  142. }
  143. if (PageIndexLists.Contains(drawPageData.PageIndex-1)) {
  144. WriteableBitmap waterMarkBitmap = GetWaterMark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  145. e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. private WriteableBitmap GetWaterMark(CPDFDocument oldDoc, double zoom, int pageIndex)
  153. {
  154. Size pageSize = oldDoc.GetPageSize(pageIndex);
  155. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  156. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  157. CreateWatermark(newDoc,true);
  158. CPDFPage newPage = newDoc.PageAtIndex(0);
  159. double scale = 96.0 / 72.0;
  160. zoom = zoom * scale;
  161. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  162. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  163. newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  164. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  165. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  166. return WirteBitmap;
  167. }
  168. public bool IsNavigationTarget(NavigationContext navigationContext)
  169. {
  170. return true;
  171. }
  172. public void OnNavigatedFrom(NavigationContext navigationContext)
  173. {
  174. }
  175. public void OnNavigatedTo(NavigationContext navigationContext)
  176. {
  177. CPDFViewer pdfViewer;
  178. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  179. if (pdfViewer != null)
  180. {
  181. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  182. {
  183. PDFViewer = new CPDFViewer();
  184. PDFViewer.InitDocument(pdfViewer.Document.FilePath);
  185. Document = PDFViewer.Document;
  186. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  187. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  188. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  189. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  190. PDFViewer.Load();
  191. PDFViewer.ChangeViewMode(ViewMode.Single);
  192. PDFViewer.SetMouseMode(MouseModes.Default);
  193. }
  194. }
  195. }
  196. }
  197. }