123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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<SetBackgroundEvent>().Subscribe(SetBackground);
- }
- public async void AwaitRenderBitmap(CPDFDocument doc)
- {
- await RenderBitmap(doc);
- }
- public void SetBackground( BackgroundInfo backgroundInfo)
- {
- CreateBackground();
- document.PdfToImage("0-1", "D:\\");
- document.WriteToFilePath("D:\\sb22222.pdf");
- AwaitRenderBitmap(document);
- }
- public void CreateBackground()
- {
- byte[] color = {0,255, 255};
- background = document.GetBackground();
- background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
- background.SetColor(color);
- 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.SetScale(1);
- 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<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- document = PDFViewer.Document;
- AwaitRenderBitmap(PDFViewer.Document);
- }
- }
- }
|