using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKitViewer.PdfViewer; using PDF_Office.EventAggregators; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.EditTools.Background; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace PDF_Office.ViewModels.EditTools.Background { public class BackgroundDocumentContentViewModel : BindableBase,INavigationAware { public IEventAggregator eventAggregator; public CPDFViewer PDFViewer; public CPDFDocument document; public CPDFBackground background; private ImageSource _imageSource = null; public ImageSource ImageSource { get { return _imageSource; } set { SetProperty(ref _imageSource, value); } } public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; eventAggregator.GetEvent().Subscribe(SetBackground); } public async void AwaitRenderBitmap(CPDFDocument doc) { await RenderBitmap(doc); } public void SetBackground( BackgroundInfo backgroundInfo) { CreateBackground(backgroundInfo); document.PdfToImage("0-1", "D:\\"); document.WriteToFilePath("D:\\sb22222.pdf"); AwaitRenderBitmap(document); } public void CreateBackground(BackgroundInfo backgroundInfo) { byte[] color = {0,255, 255}; background = document.GetBackground(); backgroundInfo.BackgroundType = C_Background_Type.BG_TYPE_IMAGE; if (backgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR) { background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR); background.SetColor(color); background.SetScale(1); } else { background.SetBackgroundType(C_Background_Type.BG_TYPE_IMAGE); background.SetImage(backgroundInfo.ImageArray, backgroundInfo.ImageWidth, backgroundInfo.ImageHeight, ComPDFKit.Import.C_Scale_Type.fitCenter); background.SetScale(1); } background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER); background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER); background.SetOpacity(50); background.SetPages("0"); background.SetRotation(0); background.SetXOffset(0); background.SetYOffset(0); background.Update(); background.Release(); } public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image) { IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap ( ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions() ); return bs; } public async Task RenderBitmap(CPDFDocument doc) { CPDFPage page = doc.PageAtIndex(0, true); byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4]; Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true); //await Task.Run(delegate //{ // page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1); //}); //PixelFormat fmt = PixelFormats.Bgra32; //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); ImageSource = ToBitmapSource(bitmap); doc.ReleasePages(); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); document = PDFViewer.Document; AwaitRenderBitmap(PDFViewer.Document); } } }