|
@@ -1,5 +1,8 @@
|
|
|
-using ComPDFKitViewer.PdfViewer;
|
|
|
+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;
|
|
@@ -8,8 +11,12 @@ 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
|
|
|
{
|
|
@@ -17,15 +24,88 @@ namespace PDF_Office.ViewModels.EditTools.Background
|
|
|
{
|
|
|
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)
|
|
@@ -40,7 +120,8 @@ namespace PDF_Office.ViewModels.EditTools.Background
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
|
{
|
|
|
navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
|
|
|
-
|
|
|
+ document = PDFViewer.Document;
|
|
|
+ AwaitRenderBitmap(PDFViewer.Document);
|
|
|
}
|
|
|
}
|
|
|
}
|