BackgroundDocumentContentViewModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
  78. e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
  79. }
  80. }
  81. }
  82. public void SetBackground(BackgroundInfo backgroundInfo)
  83. {
  84. this.BackgroundInfo = backgroundInfo;
  85. PDFViewer.InvalidChildVisual(false);
  86. }
  87. public void CreateBackground(CPDFDocument document)
  88. {
  89. if(BackgroundInfo != null)
  90. {
  91. background = Document.GetBackground();
  92. if (BackgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  93. {
  94. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  95. background.SetColor(BackgroundInfo.Color);
  96. background.SetScale(100);
  97. }
  98. else
  99. {
  100. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  101. background.SetImage(BackgroundInfo.ImageArray, BackgroundInfo.ImageWidth, BackgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  102. background.SetScale(0.1f);
  103. }
  104. background.SetHorizalign(BackgroundInfo.Horizalign);
  105. background.SetVertalign(BackgroundInfo.Vertalign);
  106. background.SetOpacity(BackgroundInfo.Opacity);
  107. background.SetPages(BackgroundInfo.PageRange);
  108. background.SetRotation(BackgroundInfo.Rotation);
  109. background.SetXOffset(BackgroundInfo.HorizOffset);
  110. background.SetYOffset(BackgroundInfo.VertOffset);
  111. background.SetAllowsView(true);
  112. background.Update();
  113. // background.Release();
  114. }
  115. }
  116. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
  117. {
  118. IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
  119. BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  120. (
  121. ptr,
  122. IntPtr.Zero,
  123. Int32Rect.Empty,
  124. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  125. );
  126. return bs;
  127. }
  128. public async Task RenderBitmap(CPDFDocument doc)
  129. {
  130. CPDFPage page = doc.PageAtIndex(0, true);
  131. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  132. Bitmap bitmap = await ToolMethod.RenderPageBitmap(Document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true);
  133. //await Task.Run(delegate
  134. //{
  135. // page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  136. //});
  137. //PixelFormat fmt = PixelFormats.Bgra32;
  138. //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);
  139. ImageSource = ToBitmapSource(bitmap);
  140. doc.ReleasePages();
  141. }
  142. public bool IsNavigationTarget(NavigationContext navigationContext)
  143. {
  144. return true;
  145. }
  146. public void OnNavigatedFrom(NavigationContext navigationContext)
  147. {
  148. }
  149. public void OnNavigatedTo(NavigationContext navigationContext)
  150. {
  151. CPDFViewer pdfViewer;
  152. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  153. if (pdfViewer != null)
  154. {
  155. if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer))
  156. {
  157. PDFViewer = new CPDFViewer();
  158. PDFViewer.InitDocument(pdfViewer.Document.FilePath);
  159. Document = PDFViewer.Document;
  160. PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler;
  161. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  162. regionManager.AddToRegion(ViewerRegionName, PDFViewer);
  163. PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction());
  164. PDFViewer.Load();
  165. PDFViewer.ChangeViewMode(ViewMode.Single);
  166. PDFViewer.SetMouseMode(MouseModes.Default);
  167. }
  168. }
  169. }
  170. }
  171. }