123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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<SetBackgroundEvent>().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 backgroundBitmap = GetBackground(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
- e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
- }
- }
- }
- public void SetBackground(BackgroundInfo backgroundInfo)
- {
- this.BackgroundInfo = backgroundInfo;
- PDFViewer.InvalidChildVisual(false);
- }
- public void CreateBackground(CPDFDocument document)
- {
- if(BackgroundInfo != null)
- {
- background = Document.GetBackground();
- if (BackgroundInfo.BackgroundType == C_Background_Type.BG_TYPE_COLOR)
- {
- background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
- background.SetColor(BackgroundInfo.Color);
- background.SetScale(100);
- }
- 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(BackgroundInfo.Horizalign);
- background.SetVertalign(BackgroundInfo.Vertalign);
- background.SetOpacity(BackgroundInfo.Opacity);
- background.SetPages(BackgroundInfo.PageRange);
- background.SetRotation(BackgroundInfo.Rotation);
- background.SetXOffset(BackgroundInfo.HorizOffset);
- background.SetYOffset(BackgroundInfo.VertOffset);
- background.SetAllowsView(true);
- 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<CPDFViewer>(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);
- }
- }
- }
- }
- }
|