using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model.EditTools.HeaderFooter; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Media; using System.Windows.Controls; using PDF_Master.Model; using System.Diagnostics; using System.Drawing.Printing; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify; using ComPDFKit.PDFWatermark; using PDF_Master.Model.EditTools.Watermark; using Microsoft.Office.Core; using PDF_Master.Model.EditTools.Bates; namespace PDF_Master.ViewModels.EditTools.HeaderFooter { public class HeaderFooterDocumentContentViewModel : BindableBase, INavigationAware { public IEventAggregator eventAggregator; public IRegionManager regionManager; public CPDFViewer PDFViewer; public CPDFDocument Document; private ViewContentViewModel viewContentViewModel; public EnumDelete EnumDelete = EnumDelete.StatusCreate; public CPDFViewer pdfViewer; private CPDFHeaderFooter headerFooter; private HeaderFooterInfo headerFooterInfo; public bool firstin = true; public string ViewerRegionName { get; set; } public string unicode = null; public string Unicode = null; private int _pageSize; public int PageRangeNumber { get { return _pageSize; } set { SetProperty(ref _pageSize, value); } } private int _currentPageIndex; public int CurrentPageIndex { get { return _currentPageIndex; } set { SetProperty(ref _currentPageIndex, value); } } private Visibility _inputIndexBoxVisible = Visibility.Collapsed; public Visibility InputIndexBoxVisible { get { return _inputIndexBoxVisible; } set { SetProperty(ref _inputIndexBoxVisible, value); } } public DelegateCommand ShowInputIndexBoxCommand { set; get; } public DelegateCommand GoToPageCommand { set; get; } public HeaderFooterDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager) { this.eventAggregator = eventAggregator; this.regionManager = regionManager; ShowInputIndexBoxCommand = new DelegateCommand(ShowInputIndexBox); GoToPageCommand = new DelegateCommand(GoToPage); Unicode = App.mainWindowViewModel.SelectedItem.Unicode; ViewerRegionName = RegionNames.HeaderFooterViewerRegionName; eventAggregator.GetEvent().Subscribe(SetHeaderFooter, e => e.Unicode == Unicode); eventAggregator.GetEvent().Subscribe(ConfirmEditToolsHeaderFooter, e => e == Unicode); eventAggregator.GetEvent().Subscribe(DeleteHeaderFooter, 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) { return; } if (int.Parse(args.Text) > 0 && int.Parse(args.Text) <= PDFViewer.Document.PageCount && Regex.Match(args.Text, "^\\d+$").Success) { PDFViewer.GoToPage(int.Parse(args.Text) - 1); InputIndexBoxVisible = Visibility.Collapsed; } } } public void ConfirmEditToolsHeaderFooter(string unicode) { if (EnumDelete == EnumDelete.StatusDeleteAll) { PDFViewer.Document.GetHeaderFooter().Clear(); } else { if (headerFooterInfo != null) { CreateHeaderFooter(viewContentViewModel.PDFViewer.Document); } } viewContentViewModel.PDFViewer.UndoManager.CanSave = true; } public void DeleteHeaderFooter(EnumDeleteUnicode enumDeleteunicode) { if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll) { EnumDelete enumDelete = enumDeleteunicode.Status; EnumDelete = enumDelete; PDFViewer.Document.ReleasePages(); PDFViewer.ReloadDocument(); } } public void SetHeaderFooter(HeaderFooterInfoUnicode headerFooterInfoUnicode) { EnumDelete = EnumDelete.StatusCreate; HeaderFooterInfo headerFooterInfo = headerFooterInfoUnicode.Status; EditToolsHelper.GetPageRange(headerFooterInfo.PageRangeIndex, PDFViewer.Document, ref headerFooterInfo.PageRange, headerFooterInfo.PageRange); this.headerFooterInfo = headerFooterInfo; //CreateHeaderFooter(PDFViewer.Document); PDFViewer.InvalidChildVisual(false); } public void CreateHeaderFooter(CPDFDocument document, bool IsNewDoc = false) { if (headerFooterInfo != null) { //document.(headerFooterInfo.TextData, headerFooterInfo.margin, headerFooterInfo.PageRange, headerFooterInfo.StarPagetNumber + 1); headerFooter = document.GetHeaderFooter(); for (int i = 0; i < 6; i++) { headerFooter.SetText(i, HomePageEditHelper.ReverseDate(headerFooterInfo.TextData[i].text, document)); headerFooter.SetFontName(i, headerFooterInfo.TextData[i].fontName); headerFooter.SetFontSize(i, headerFooterInfo.TextData[i].fontSize); headerFooter.SetTextColor(i, headerFooterInfo.TextData[i].Color); } if (IsNewDoc) { headerFooter.SetPageOffset(1); headerFooter.SetPages("0"); } else { headerFooter.SetPages(headerFooterInfo.PageRange); headerFooter.SetPageOffset(headerFooterInfo.StarPagetNumber + 1); } headerFooter.SetMargin(headerFooterInfo.margin); headerFooter.Update(); } } 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 (headerFooterInfo != null) { char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (CommonHelper.GetPagesInRange(ref PageIndexLists, headerFooterInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true)) { //TODO if (PageIndexLists.Contains(drawPageData.PageIndex-1)) { WriteableBitmap headerFooterBitmap = GetHeaderFooter(PDFViewer.Document, e.Zoom, drawPageData.PageIndex); e.DrawContext.DrawImage(headerFooterBitmap, drawPageData.PageBound); } } } } } CurrentPageIndex = PDFViewer.CurrentIndex + 1; } private WriteableBitmap GetHeaderFooter(CPDFDocument oldDoc, double zoom, int pageIndex) { Size pageSize = oldDoc.GetPageSize(pageIndex); CPDFDocument newDoc = CPDFDocument.CreateDocument(); newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null); CreateHeaderFooter(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) { 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); PageRangeNumber = PDFViewer.Document.PageCount; PDFViewer.Zoom(0.5); } } } } }