BackgroundDocumentContentViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.EventAggregators;
  5. using PDF_Office.Helper;
  6. using PDF_Office.Model;
  7. using PDF_Office.Model.EditTools.Background;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Drawing;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. namespace PDF_Office.ViewModels.EditTools.Background
  21. {
  22. public class BackgroundDocumentContentViewModel : BindableBase,INavigationAware
  23. {
  24. public IEventAggregator eventAggregator;
  25. public CPDFViewer PDFViewer;
  26. public CPDFDocument document;
  27. public CPDFBackground background;
  28. private ImageSource _imageSource = null;
  29. public ImageSource ImageSource
  30. {
  31. get { return _imageSource; }
  32. set
  33. {
  34. SetProperty(ref _imageSource, value);
  35. }
  36. }
  37. public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator)
  38. {
  39. this.eventAggregator = eventAggregator;
  40. eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground);
  41. }
  42. public async void AwaitRenderBitmap(CPDFDocument doc)
  43. {
  44. await RenderBitmap(doc);
  45. }
  46. public void SetBackground( BackgroundInfo backgroundInfo)
  47. {
  48. CreateBackground(backgroundInfo);
  49. document.PdfToImage("0-1", "D:\\");
  50. document.WriteToFilePath("D:\\sb22222.pdf");
  51. AwaitRenderBitmap(document);
  52. }
  53. public void CreateBackground(BackgroundInfo backgroundInfo)
  54. {
  55. byte[] color = {0,255, 255};
  56. background = document.GetBackground();
  57. backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE;
  58. if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
  59. {
  60. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  61. background.SetColor(color);
  62. background.SetScale(1);
  63. }
  64. else
  65. {
  66. background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE);
  67. background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter);
  68. background.SetScale(1);
  69. }
  70. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  71. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  72. background.SetOpacity(50);
  73. background.SetPages("0");
  74. background.SetRotation(0);
  75. background.SetXOffset(0);
  76. background.SetYOffset(0);
  77. background.Update();
  78. background.Release();
  79. }
  80. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
  81. {
  82. IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
  83. BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  84. (
  85. ptr,
  86. IntPtr.Zero,
  87. Int32Rect.Empty,
  88. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  89. );
  90. return bs;
  91. }
  92. public async Task RenderBitmap(CPDFDocument doc)
  93. {
  94. CPDFPage page = doc.PageAtIndex(0, true);
  95. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  96. Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true);
  97. //await Task.Run(delegate
  98. //{
  99. // page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  100. //});
  101. //PixelFormat fmt = PixelFormats.Bgra32;
  102. //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);
  103. ImageSource = ToBitmapSource(bitmap);
  104. doc.ReleasePages();
  105. }
  106. public bool IsNavigationTarget(NavigationContext navigationContext)
  107. {
  108. return true;
  109. }
  110. public void OnNavigatedFrom(NavigationContext navigationContext)
  111. {
  112. }
  113. public void OnNavigatedTo(NavigationContext navigationContext)
  114. {
  115. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  116. document = PDFViewer.Document;
  117. AwaitRenderBitmap(PDFViewer.Document);
  118. }
  119. }
  120. }