BackgroundDocumentContentViewModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.EventAggregators;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model;
  8. using PDF_Office.Model.EditTools.Background;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. namespace PDF_Office.ViewModels.EditTools.Background
  22. {
  23. public class BackgroundDocumentContentViewModel : BindableBase, INavigationAware
  24. {
  25. public IEventAggregator eventAggregator;
  26. public IRegionManager regionManager;
  27. public CPDFViewer PDFViewer;
  28. public CPDFDocument Document;
  29. public CPDFBackground background;
  30. private BackgroundInfo BackgroundInfo;
  31. public string ViewerRegionName { get; set; }
  32. private ImageSource _imageSource = null;
  33. public ImageSource ImageSource
  34. {
  35. get { return _imageSource; }
  36. set
  37. {
  38. SetProperty(ref _imageSource, value);
  39. }
  40. }
  41. public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
  42. {
  43. this.eventAggregator = eventAggregator;
  44. this.regionManager = regionManager;
  45. eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground);
  46. ViewerRegionName = RegionNames.BackgroundViewRegionName;
  47. }
  48. public async void AwaitRenderBitmap(CPDFDocument doc)
  49. {
  50. await RenderBitmap(doc);
  51. }
  52. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  53. {
  54. }
  55. private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex)
  56. {
  57. System.Windows.Size pageSize = oldDoc.GetPageSize(pageIndex);
  58. CPDFDocument newDoc = CPDFDocument.CreateDocument();
  59. newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
  60. // CreateBackground(newDoc);
  61. CPDFPage newPage = newDoc.PageAtIndex(0);
  62. double scale = 96.0 / 72.0;
  63. zoom = zoom * scale;
  64. Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale));
  65. byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)];
  66. newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true);
  67. WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null);
  68. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0);
  69. return WirteBitmap;
  70. }
  71. private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e)
  72. {
  73. if (e.DrawPages.Count > 0 && e.DrawContext != null)
  74. {
  75. foreach (DrawPageData drawPageData in e.DrawPages)
  76. {
  77. WriteableBitmap waterMarkBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  78. e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound);
  79. }
  80. }
  81. }
  82. public void SetBackground(BackgroundInfo backgroundInfo)
  83. {
  84. this.BackgroundInfo = backgroundInfo;
  85. CreateBackground(PDFViewer.Document);
  86. AwaitRenderBitmap(Document);
  87. }
  88. public void CreateBackground(CPDFDocument document)
  89. {
  90. byte[] color = { 0, 255, 255 };
  91. background = Document.GetBackground();
  92. BackgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  93. if (BackgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  94. {
  95. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  96. background.SetColor(color);
  97. background.SetScale(1);
  98. }
  99. else
  100. {
  101. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  102. background.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  103. background.SetScale(0.1f);
  104. }
  105. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  106. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  107. background.SetOpacity(50);
  108. background.SetPages("0");
  109. background.SetRotation(0);
  110. background.SetXOffset(0);
  111. background.SetYOffset(0);
  112. background.Update();
  113. background.Release();
  114. }
  115. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
  116. {
  117. IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
  118. BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  119. (
  120. ptr,
  121. IntPtr.Zero,
  122. Int32Rect.Empty,
  123. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  124. );
  125. return bs;
  126. }
  127. public async Task RenderBitmap(CPDFDocument doc)
  128. {
  129. CPDFPage page = doc.PageAtIndex(0, true);
  130. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  131. Bitmap bitmap = await ToolMethod.RenderPageBitmap(Document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true);
  132. //await Task.Run(delegate
  133. //{
  134. // page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  135. //});
  136. //PixelFormat fmt = PixelFormats.Bgra32;
  137. //BitmapSource bps = BitmapSource.Create((int)page.PageSize.Width, (int)page.PageSize.Height, 96.0, 96.0, fmt, null, bmp_data, ((int)page.PageSize.Width * fmt.BitsPerPixel + 7) / 8);
  138. ImageSource = ToBitmapSource(bitmap);
  139. doc.ReleasePages();
  140. }
  141. public bool IsNavigationTarget(NavigationContext navigationContext)
  142. {
  143. return true;
  144. }
  145. public void OnNavigatedFrom(NavigationContext navigationContext)
  146. {
  147. }
  148. public void OnNavigatedTo(NavigationContext navigationContext)
  149. {
  150. CPDFViewer pdfViewer;
  151. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  152. if (pdfViewer != null)
  153. {
  154. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  155. {
  156. PDFViewer = new CPDFViewer();
  157. PDFViewer.InitDocument(pdfViewer.Document.FilePath);
  158. Document = PDFViewer.Document;
  159. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  160. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  161. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  162. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  163. PDFViewer.Load();
  164. PDFViewer.ChangeViewMode(ViewMode.Single);
  165. PDFViewer.SetMouseMode(MouseModes.Default);
  166. }
  167. }
  168. }
  169. }
  170. }