using ComPDFKitViewer.PdfViewer; using PDF_Master.EventAggregators; using PDF_Master.Model.EditTools.Background; using PDF_Master.Model; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using PDF_Master.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_Master.Helper; using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter; using System.Drawing; using System.Windows; using ComPDFKitViewer; using Size = System.Windows.Size; using System.Diagnostics; using System.Windows.Controls; using ComPDFKit.Import; using PDF_Master.ViewModels.Tools; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using TextBox = System.Windows.Controls.TextBox; using PDFSettings; using DryIoc; namespace PDF_Master.ViewModels.EditTools.Watermark { public class WatermarkDocumentContentViewModel : BindableBase, INavigationAware { public IEventAggregator eventAggregator; public IRegionManager regionManager; public CPDFViewer PDFViewer; public CPDFDocument Document; private CPDFWatermark watermark; private ViewContentViewModel viewContentViewModel; private CPDFViewer pdfViewer; private WatermarkInfo watermarkInfo; public EnumDelete EnumDelete = EnumDelete.StatusCreate; public bool firstin = true; public string ViewerRegionName { get; set; } private int _pageRangeNumber; public int PageRangeNumber { get { return _pageRangeNumber; } set { SetProperty(ref _pageRangeNumber, value); } } private Visibility _inputIndexBoxVisible = Visibility.Collapsed; public Visibility InputIndexBoxVisible { get { return _inputIndexBoxVisible; } set { SetProperty(ref _inputIndexBoxVisible, value); } } private int _currentPageIndex; public int CurrentPageIndex { get { return _currentPageIndex; } set { SetProperty(ref _currentPageIndex, value); } } public DelegateCommand ShowInputIndexBoxCommand { set; get; } public DelegateCommand GoToPageCommand { set; get; } public string Unicode = null; public WatermarkDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager) { this.regionManager = regionManager; this.eventAggregator = eventAggregator; ShowInputIndexBoxCommand = new DelegateCommand(ShowInputIndexBox); GoToPageCommand = new DelegateCommand(GoToPage); Unicode = App.mainWindowViewModel.SelectedItem.Unicode; eventAggregator.GetEvent().Subscribe(SetWatermark, e => e.Unicode == Unicode); ViewerRegionName = RegionNames.WatermarkViwerRegionName; eventAggregator.GetEvent().Subscribe(ConfirmEditToolsWatermark,e=>e==Unicode); eventAggregator.GetEvent().Subscribe(DeleteWatermark, e => e.Unicode == Unicode); } public void ShowInputIndexBox(object obj) { if (obj != null) { InputIndexBoxVisible = Visibility.Visible; } } public void GoToPage(object obj) { if (obj != null) { var args = obj as TextBox; if (args.Text == null || !Regex.Match(args.Text, "^\\d+$").Success) { return; } if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount) { PDFViewer.GoToPage(int.Parse(args.Text) - 1); InputIndexBoxVisible = Visibility.Collapsed; } } } public void ConfirmEditToolsWatermark(string unicode) { if (EnumDelete == EnumDelete.StatusDeleteAll) { viewContentViewModel.PDFViewer.Document.DeleteWatermarks(); } else { if (watermarkInfo != null) { if (EnumDelete == EnumDelete.StatusDeleteInitial) { viewContentViewModel.PDFViewer.Document.DeleteWatermarks(); CreateWatermark(viewContentViewModel.PDFViewer.Document); } else { CreateWatermark(viewContentViewModel.PDFViewer.Document); } } } viewContentViewModel.PDFViewer.UndoManager.CanSave = true; } public void DeleteWatermark(EnumDeleteUnicode enumDeleteunicode) { if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll || enumDeleteunicode.Status == EnumDelete.StatusDeleteInitial) { EnumDelete enumDelete = enumDeleteunicode.Status; EnumDelete = enumDelete; PDFViewer.Document.DeleteWatermarks(); PDFViewer.Document.ReleasePages(); PDFViewer.ReloadDocument(); } } public void SetWatermark(WatermarkInfoUnicode watermarkInfounicode) { WatermarkInfo watermarkInfo = watermarkInfounicode.Status; EditToolsHelper.GetPageRange(watermarkInfo.PageRangeIndex, PDFViewer.Document, ref watermarkInfo.PageRange, watermarkInfo.PageRange); this.watermarkInfo = watermarkInfo; EnumDelete = EnumDelete.StatusCreate; PDFViewer.InvalidChildVisual(false); } public void CreateWatermark(CPDFDocument document, bool IsNewDoc = false) { if (watermarkInfo != null) { if (IsNewDoc) { document.DeleteWatermarks(); } if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT) { watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT); watermark.SetText(watermarkInfo.Text); watermark.SetFontName(watermarkInfo.FontName); watermark.SetAutoSize(watermarkInfo.IsRelativeScale); if (watermarkInfo.IsRelativeScale) { watermark.SetFontSizeWithPercentage(watermarkInfo.TextSize); } else { watermark.SetFontSize(watermarkInfo.TextSize); } watermark.SetTextRGBColor(watermarkInfo.TextColor); } else { watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG); if (watermarkInfo.ImageArray != null) { watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight); } watermark.SetScale(watermarkInfo.ImageSize / 100); } watermark.SetRotation((float)((watermarkInfo.Rotation / 180) * Math.PI)); watermark.SetOpacity((byte)(((float)watermarkInfo.Opacity / 100) * 255)); watermark.SetFront(watermarkInfo.IsFront); watermark.SetVertalign(watermarkInfo.WatermarkVertalign); watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign); watermark.SetFullScreen(watermarkInfo.Isfull); watermark.SetVertOffset(watermarkInfo.VertOffset); watermark.SetHorizOffset(watermarkInfo.HorizOffset); watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing); watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing); if (IsNewDoc) { watermark.SetPages("0"); } else { watermark.SetPages(watermarkInfo.PageRange); } Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark()); Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark()); } } private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { } private void CurrentViewer_CustomDrawHandler(object sender, CustomDrawData e) { if (e.DrawPages.Count > 0 && e.DrawContext != null) { List PageIndexLists = new List(); foreach (DrawPageData drawPageData in e.DrawPages) { if (watermarkInfo != null) { char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true)) { //TODO //Trace.WriteLine(CommonHelper.GetPagesInRange(ref PageIndexLists, watermarkInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true)); //Trace.WriteLine(drawPageData.PageIndex); //foreach (var abbb in PageIndexLists) //{ // Trace.WriteLine("smgui _______" + abbb); //} if (PageIndexLists.Contains(drawPageData.PageIndex - 1)) { if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT && watermarkInfo.Text != "") { WriteableBitmap waterMarkBitmap = GetWatermark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex); e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound); } if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_IMG) { WriteableBitmap waterMarkBitmap = GetWatermark(PDFViewer.Document, e.Zoom, drawPageData.PageIndex); e.DrawContext.DrawImage(waterMarkBitmap, drawPageData.PageBound); } } } } } } CurrentPageIndex = PDFViewer.CurrentIndex + 1; } private WriteableBitmap GetWatermark(CPDFDocument oldDoc, double zoom, int pageIndex) { Size pageSize = oldDoc.GetPageSize(pageIndex); CPDFDocument newDoc = CPDFDocument.CreateDocument(); newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null); CreateWatermark(newDoc, true); CPDFPage newPage = newDoc.PageAtIndex(0); double scale = 96.0 / 72.0; zoom = zoom * 1.5; 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(1 / (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); newDoc.Release(); return WirteBitmap; } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out pdfViewer); navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); if (pdfViewer != null && viewContentViewModel != null) { if (!regionManager.Regions[ViewerRegionName].Views.Contains(PDFViewer)) { PDFViewer = new CPDFViewer(); PDFViewer.InitDocument(pdfViewer.Document); 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.PanTool); eventAggregator.GetEvent().Publish(new CPDFViewerUnicode { Unicode = Unicode, Status = PDFViewer }); PageRangeNumber = PDFViewer.Document.PageCount; PDFViewer.Zoom(0.5); } } } } }