using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKitViewer; 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 IRegionManager regionManager; public CPDFViewer PDFViewer; public CPDFDocument Document; public CPDFBackground background; private BackgroundInfo BackgroundInfo; public string ViewerRegionName { get; set; } private ImageSource _imageSource = null; public ImageSource ImageSource { get { return _imageSource; } set { SetProperty(ref _imageSource, value); } } public BackgroundDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager) { this.eventAggregator = eventAggregator; this.regionManager = regionManager; eventAggregator.GetEvent().Subscribe(SetBackground); ViewerRegionName = RegionNames.BackgroundViewRegionName; } public async void AwaitRenderBitmap(CPDFDocument doc) { await RenderBitmap(doc); } private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { } private WriteableBitmap GetBackground(CPDFDocument oldDoc, double zoom, int pageIndex) { System.Windows.Size pageSize = oldDoc.GetPageSize(pageIndex); CPDFDocument newDoc = CPDFDocument.CreateDocument(); newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null); // CreateBackground(newDoc); CPDFPage newPage = newDoc.PageAtIndex(0); double scale = 96.0 / 72.0; zoom = zoom * scale; Rect renderRect = new Rect(0, 0, (int)(pageSize.Width * scale), (int)(pageSize.Height * scale)); byte[] imageArray = new byte[(int)(renderRect.Width * renderRect.Height * 4)]; newPage.RenderPageBitmapWithMatrix((float)zoom, renderRect, 0x00FFFFFF, imageArray, 1, true); WriteableBitmap WirteBitmap = new WriteableBitmap((int)renderRect.Width, (int)renderRect.Height, 96, 96, PixelFormats.Bgra32, null); WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)renderRect.Width, (int)renderRect.Height), imageArray, WirteBitmap.BackBufferStride, 0); return WirteBitmap; } private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e) { if (e.DrawPages.Count > 0 && e.DrawContext != null) { foreach (DrawPageData drawPageData in e.DrawPages) { WriteableBitmap waterMarkBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex); e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound); } } } public void SetBackground(BackgroundInfo backgroundInfo) { this.BackgroundInfo = backgroundInfo; CreateBackground(PDFViewer.Document); AwaitRenderBitmap(Document); } public void CreateBackground(CPDFDocument document) { 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(0.1f); } 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) { CPDFViewer pdfViewer; navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out pdfViewer); if (pdfViewer != null) { if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer)) { PDFViewer = new CPDFViewer(); PDFViewer.InitDocument(pdfViewer.Document.FilePath); Document = PDFViewer.Document; PDFViewer.CustomDrawHandler += CurrentViewer_CustomDrawHandler; PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged; regionManager.AddToRegion(ViewerRegionName, PDFViewer); PDFViewer.SetAnnotInteraction(!PDFViewer.GetAnnotInteraction()); PDFViewer.Load(); PDFViewer.ChangeViewMode(ViewMode.Single); PDFViewer.SetMouseMode(MouseModes.Default); } } } } }