BackgroundDocumentContentViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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();
  49. document.PdfToImage("0-1", "D:\\");
  50. document.WriteToFilePath("D:\\sb22222.pdf");
  51. AwaitRenderBitmap(document);
  52. }
  53. public void CreateBackground()
  54. {
  55. byte[] color = {0,255, 255};
  56. background = document.GetBackground();
  57. background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
  58. background.SetColor(color);
  59. background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
  60. background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
  61. background.SetOpacity(50);
  62. background.SetPages("0");
  63. background.SetRotation(0);
  64. background.SetScale(1);
  65. background.SetXOffset(0);
  66. background.SetYOffset(0);
  67. background.Update();
  68. background.Release();
  69. }
  70. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
  71. {
  72. IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
  73. BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  74. (
  75. ptr,
  76. IntPtr.Zero,
  77. Int32Rect.Empty,
  78. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  79. );
  80. return bs;
  81. }
  82. public async Task RenderBitmap(CPDFDocument doc)
  83. {
  84. CPDFPage page = doc.PageAtIndex(0, true);
  85. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  86. Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true);
  87. //await Task.Run(delegate
  88. //{
  89. // page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  90. //});
  91. //PixelFormat fmt = PixelFormats.Bgra32;
  92. //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);
  93. ImageSource = ToBitmapSource(bitmap);
  94. doc.ReleasePages();
  95. }
  96. public bool IsNavigationTarget(NavigationContext navigationContext)
  97. {
  98. return true;
  99. }
  100. public void OnNavigatedFrom(NavigationContext navigationContext)
  101. {
  102. }
  103. public void OnNavigatedTo(NavigationContext navigationContext)
  104. {
  105. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  106. document = PDFViewer.Document;
  107. AwaitRenderBitmap(PDFViewer.Document);
  108. }
  109. }
  110. }