|
@@ -10,14 +10,37 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using PDF_Office.Model.EditTools.Watermark;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
+using ComPDFKit.PDFPage;
|
|
|
+using System.Windows.Media.Imaging;
|
|
|
+using System.Windows.Media;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using ComPDFKit.PDFWatermark;
|
|
|
+using PDF_Office.Helper;
|
|
|
+using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
|
|
|
+using System.Drawing;
|
|
|
+using System.Windows;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
{
|
|
|
- public class WatermarkDocumentContentViewModel : BindableBase
|
|
|
+ public class WatermarkDocumentContentViewModel : BindableBase,INavigationAware
|
|
|
{
|
|
|
|
|
|
public IEventAggregator eventAggregator;
|
|
|
public CPDFViewer PDFViewer;
|
|
|
+ public CPDFDocument document;
|
|
|
+ private CPDFWatermark watermark;
|
|
|
+
|
|
|
+ private ImageSource _imageSource = null;
|
|
|
+ public ImageSource ImageSource
|
|
|
+ {
|
|
|
+ get { return _imageSource; }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ SetProperty(ref _imageSource, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public WatermarkDocumentContentViewModel(IEventAggregator eventAggregator)
|
|
|
{
|
|
|
this.eventAggregator = eventAggregator;
|
|
@@ -26,7 +49,78 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
|
|
|
public void SetWatermark(WatermarkInfo watermarkInfo)
|
|
|
{
|
|
|
+ CreateWatermark(watermarkInfo);
|
|
|
+ document.PdfToImage("0-1", "C:\\Users\\kdanmobile\\Desktop");
|
|
|
+ document.WriteToFilePath("C:\\Users\\kdanmobile\\Desktop\\sb22222.pdf");
|
|
|
+ AwaitRenderBitmap(document);
|
|
|
+ }
|
|
|
|
|
|
+ public void CreateWatermark(WatermarkInfo watermarkInfo)
|
|
|
+ {
|
|
|
+ if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
|
|
|
+ {
|
|
|
+ watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
|
|
|
+ watermark.SetText(watermarkInfo.Text);
|
|
|
+ if (watermarkInfo.TextSize == null)
|
|
|
+ {
|
|
|
+ watermarkInfo.TextSize = "6";
|
|
|
+ }
|
|
|
+ watermark.SetScale(float.Parse(watermarkInfo.TextSize));
|
|
|
+ watermark.SetTextRGBColor(watermarkInfo.TextColor);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
|
|
|
+ watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
|
|
|
+ watermark.SetScale(watermarkInfo.ImageSize % 100);
|
|
|
+ }
|
|
|
+ watermark.SetRotation(watermarkInfo.Rotation);
|
|
|
+ watermark.SetOpacity(watermarkInfo.Opacity);
|
|
|
+ watermark.SetFront(watermarkInfo.IsFront);
|
|
|
+ watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
|
|
|
+ watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
|
|
|
+ watermark.SetFullScreen(true);
|
|
|
+ watermark.SetVertOffset(watermarkInfo.VertOffset);
|
|
|
+ watermark.SetHorizOffset(watermarkInfo.HorizOffset);
|
|
|
+ watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
|
|
|
+ watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
|
|
|
+ watermark.SetPages("0");
|
|
|
+ watermark.CreateWatermark();
|
|
|
+ watermark.UpdateWatermark();
|
|
|
+
|
|
|
+ }
|
|
|
+ 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 * 1.4), (int)(page.PageSize.Height * 1 * 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);
|
|
|
+ }
|
|
|
+ public async void AwaitRenderBitmap(CPDFDocument doc)
|
|
|
+ {
|
|
|
+ await RenderBitmap(doc);
|
|
|
}
|
|
|
|
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
@@ -36,11 +130,14 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
|
|
|
|
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
|
{
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
|
{
|
|
|
navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
|
|
|
+ document = PDFViewer.Document;
|
|
|
+ AwaitRenderBitmap(PDFViewer.Document);
|
|
|
}
|
|
|
}
|
|
|
}
|