123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model.EditTools.Bates;
- 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_Office.Model;
- using PDF_Office.Model.EditTools.HeaderFooter;
- using PDF_Office.Model.EditTools.Background;
- namespace PDF_Office.ViewModels.EditTools.Bates
- {
- public class BatesDocumentContentViewModel : 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 CPDFBates bates;
- private BatesInfo batesInfo;
- 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<object> ShowInputIndexBoxCommand { set; get; }
- public DelegateCommand<object> GoToPageCommand { set; get; }
- public BatesDocumentContentViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
- {
- this.regionManager = regionManager;
- this.eventAggregator = eventAggregator;
- ShowInputIndexBoxCommand = new DelegateCommand<object>(ShowInputIndexBox);
- GoToPageCommand = new DelegateCommand<object>(GoToPage);
- Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- ViewerRegionName = RegionNames.BatesViewerRegionName;
- eventAggregator.GetEvent<SetBatesEvent>().Subscribe(SetBates, e => e.Unicode == Unicode);
- eventAggregator.GetEvent<ConfirmEditToolsBatesEvent>().Subscribe(ConfirmEditToolsBates, e => e == Unicode);
- eventAggregator.GetEvent<DeleteBatesEvent>().Subscribe(DeleteBates, 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 ConfirmEditToolsBates(string unicode)
- {
- if (EnumDelete == EnumDelete.StatusDeleteAll)
- {
- PDFViewer.Document.GetBates().Clear();
- }
- else
- {
- if (batesInfo != null)
- {
- CreateBates(viewContentViewModel.PDFViewer.Document);
- }
- }
- viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
- }
- public void DeleteBates(EnumDeleteUnicode enumDeleteunicode)
- {
- if (enumDeleteunicode.Status == EnumDelete.StatusDeleteAll)
- {
- EnumDelete enumDelete = enumDeleteunicode.Status;
- EnumDelete = enumDelete;
- PDFViewer.Document.GetBates().Clear();
- PDFViewer.Document.ReleasePages();
- PDFViewer.ReloadDocument();
- }
- }
- public void SetBates(BatesInfoUnicode batesInfoUnicode)
- {
- EnumDelete = EnumDelete.StatusCreate;
- BatesInfo batesInfo = batesInfoUnicode.Status;
- this.batesInfo = batesInfo;
- PDFViewer.InvalidChildVisual(false);
- }
- public void CreateBates(CPDFDocument document, bool IsNewDoc = false)
- {
- if (batesInfo != null)
- {
- if (bates != null) { bates.Release(); }
- bates = document.GetBates();
- for (int i = 0; i < 6; i++)
- {
- bates.SetText(i, batesInfo.TextData[i].text);
- bates.SetFontName(i, batesInfo.TextData[i].fontName);
- bates.SetFontSize(i, batesInfo.TextData[i].fontSize);
- bates.SetTextColor(i, batesInfo.TextData[i].Color);
- }
- bates.SetPageOffset(batesInfo.StarPagetNumber + 1);
- if (IsNewDoc) { bates.SetPages("0"); }
- else
- {
- bates.SetPages(batesInfo.PageRange);
- }
- bates.SetMargin(batesInfo.margin);
- bates.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<int> PageIndexLists = new List<int>();
- foreach (DrawPageData drawPageData in e.DrawPages)
- {
- if (batesInfo != null)
- {
- char[] enumerationSeparator = new char[] { ',' };
- char[] rangeSeparator = new char[] { '-' };
- if (CommonHelper.GetPagesInRange(ref PageIndexLists, batesInfo.PageRange, Document.PageCount, enumerationSeparator, rangeSeparator, true))
- { //TODO
- if (PageIndexLists.Contains(drawPageData.PageIndex - 1))
- {
- WriteableBitmap backgroundBitmap = GetBates(PDFViewer.Document, e.Zoom, drawPageData.PageIndex);
- e.DrawContext.DrawImage(backgroundBitmap, drawPageData.PageBound);
- }
- }
- }
- }
- }
- CurrentPageIndex = PDFViewer.CurrentIndex + 1;
- }
- private WriteableBitmap GetBates(CPDFDocument oldDoc, double zoom, int pageIndex)
- {
- Size pageSize = oldDoc.GetPageSize(pageIndex);
- CPDFDocument newDoc = CPDFDocument.CreateDocument();
- newDoc.InsertPage(0, pageSize.Width, pageSize.Height, null);
- CreateBates(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);
- return WirteBitmap;
- }
- 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);
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- if (pdfViewer != null)
- {
- 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.Default);
- PageRangeNumber = PDFViewer.Document.PageCount;
- PDFViewer.Zoom(0.5);
- }
- }
- }
- }
|