12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124 |
- using Microsoft.Win32;
- using Prism.Commands;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ComPDFKitViewer.PdfViewer;
- using Prism.Regions;
- using DryIoc;
- using System.Diagnostics;
- using Prism.Services.Dialogs;
- using PDF_Office.CustomControl;
- using PDF_Office.Model;
- using System.Windows;
- using System.Windows.Controls;
- using System.IO;
- using PDFSettings;
- using ComPDFKitViewer.AnnotEvent;
- using PDF_Office.ViewModels.Tools;
- using PDF_Office.Views;
- using Prism.Events;
- using PDF_Office.EventAggregators;
- using PDF_Office.Views.EditTools.Background;
- namespace PDF_Office.ViewModels
- {
- public class ViewContentViewModel : BindableBase, INavigationAware
- {
- #region 属性、变量
- public CPDFViewer PDFViewer { get; set; }
- private MainContentViewModel mainViewModel { get; set; }
- public IRegionManager region;
- public IDialogService dialogs;
- public string ViwerRegionName { get; set; }
- public string BOTARegionName { get; set; }
- public string PropertyRegionName { get; set; }
- public string ToolContentRegionName { get; set; }
- public string ToolsBarContentRegionName { get; set; }
- public string ReadModeRegionName { get; set; }
- public string ConverterBarContentRegionName { get; set; }
- public string TextEditContentRegionName { get; set; }
- public string BackgroundContentRegionName { get; set; }
- /// <summary>
- /// 底部工具栏 RegionName
- /// </summary>
- public string BottomToolRegionName { get; set; }
- private bool _isInPageEdit = false;
- /// <summary>
- /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
- /// </summary>
- public bool isInPageEdit
- {
- get { return _isInPageEdit; }
- set
- {
- _isInPageEdit = value;
- if (!value)
- {
- CanRedo = PDFViewer.UndoManager.CanRedo;
- CanUndo = PDFViewer.UndoManager.CanUndo;
- }
- }
- }
- public Action PageEditUndo { get; set; }
- public Action PageEditRedo { get; set; }
- /// <summary>
- ///工具条
- ///0:收起
- ///40:显示
- /// </summary>
- private int toolRowHeight = 40;
- public int ToolRowHeight
- {
- get { return toolRowHeight; }
- set
- {
- SetProperty(ref toolRowHeight, value);
- }
- }
- /// <summary>
- /// 水印,背景侧边栏宽度
- /// 0:收起
- /// 260:显示
- /// </summary>
- private int propertyColumnWidth = 0;
- public int PropertyColumnWidth
- {
- get { return propertyColumnWidth; }
- set
- {
- SetProperty(ref propertyColumnWidth, value);
- }
- }
- private int gridToolRow = 1;
- /// <summary>
- /// 控制ToolContent的Row
- /// </summary>
- public int GridToolRow
- {
- get { return gridToolRow; }
- set
- {
- SetProperty(ref gridToolRow, value);
- }
- }
- private int gridToolRowSpan = 3;
- /// <summary>
- /// 控制ToolContent的RowSpan
- /// </summary>
- public int GridToolRowSpan
- {
- get { return gridToolRowSpan; }
- set
- {
- SetProperty(ref gridToolRowSpan, value);
- }
- }
- private Visibility toolContentVisible = Visibility.Collapsed;
- /// <summary>
- /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
- /// 留意:显示前需要先注入内容、设置好行和跨行数
- /// </summary>
- public Visibility ToolContentVisible
- {
- get { return toolContentVisible; }
- set
- {
- SetProperty(ref toolContentVisible, value);
- }
- }
- private Visibility gridVisibility = Visibility.Visible;
- /// <summary>
- /// 是否正在加载中
- /// </summary>
- public Visibility GridVisibility
- {
- get { return gridVisibility; }
- set
- {
- SetProperty(ref gridVisibility, value);
- }
- }
- private Visibility isLoading = Visibility.Collapsed;
- /// <summary>
- /// 是否正在加载中
- /// </summary>
- public Visibility IsLoading
- {
- get { return isLoading; }
- set
- {
- SetProperty(ref isLoading, value);
- }
- }
- private Visibility converterBarContentVisible = Visibility.Collapsed;
- private Visibility toolsbarContentVisible = Visibility.Collapsed;
- /// <summary>
- /// 控制ToolsBarContent的显示
- /// 留意:显示前需要先注入内容、设置好行和跨行数
- /// </summary>
- public Visibility ConverterBarContentVisible
- {
- get { return converterBarContentVisible; }
- set
- {
- SetProperty(ref converterBarContentVisible, value);
- }
- }
- private Visibility toolsBarContentVisible = Visibility.Collapsed;
- /// <summary>
- /// 控制ToolsBarContent的显示
- /// 留意:显示前需要先注入内容、设置好行和跨行数
- /// </summary>
- public Visibility ToolsBarContentVisible
- {
- get { return toolsBarContentVisible; }
- set
- {
- SetProperty(ref toolsBarContentVisible, value);
- }
- }
- private Visibility textEditToolContentVisible = Visibility.Collapsed;
- /// <summary>
- /// 控制ToolsBarContent的显示
- /// 留意:显示前需要先注入内容、设置好行和跨行数
- /// </summary>
- public Visibility TextEditToolContentVisible
- {
- get { return textEditToolContentVisible; }
- set
- {
- SetProperty(ref textEditToolContentVisible, value);
- }
- }
- private bool isPorpertyOpen = false;
- /// <summary>
- /// 属性栏是否展开
- /// </summary>
- public bool IsPropertyOpen
- {
- get { return isPorpertyOpen; }
- set
- {
- SetProperty(ref isPorpertyOpen, value);
- }
- }
- private Visibility isReadMode = Visibility.Visible;
- /// <summary>
- ///是否为阅读模式
- /// </summary>
- public Visibility IsReadMode
- {
- get { return isReadMode; }
- set
- {
- SetProperty(ref isReadMode, value);
- }
- }
- private bool canSave;
- /// <summary>
- /// 是否可以保存
- /// </summary>
- public bool CanSave
- {
- get { return canSave; }
- set
- {
- SetProperty(ref canSave, value);
- }
- }
- private bool canUndo;
- /// <summary>
- /// 是否可以进行Undo
- /// </summary>
- public bool CanUndo
- {
- get { return canUndo; }
- set
- {
- SetProperty(ref canUndo, value);
- }
- }
- private bool canRedo;
- /// <summary>
- /// 是否可以进行Redo
- /// </summary>
- public bool CanRedo
- {
- get { return canRedo; }
- set
- {
- SetProperty(ref canRedo, value);
- }
- }
- private GridLength botaWidth = new GridLength(48);
- /// <summary>
- /// BOTA栏的宽度
- /// </summary>
- public GridLength BOTAWidth
- {
- get { return botaWidth; }
- set
- {
- SetProperty(ref botaWidth, value);
- if (botaWidth.Value <= 48)
- {
- OpenBOTA = false;
- }
- }
- }
- private int selectedIndex;
- /// <summary>
- /// 工具栏选中项的索引
- /// </summary>
- public int TabSelectedIndex
- {
- get { return selectedIndex; }
- set
- {
- SetProperty(ref selectedIndex, value);
- }
- }
- private bool openBOTA = false;
- /// <summary>
- /// 是否展开BOTA
- /// </summary>
- public bool OpenBOTA
- {
- get { return openBOTA; }
- set
- {
- openBOTA = value;
- if (openBOTA && BOTAWidth.Value <= 48)
- {
- BOTAWidth = new GridLength(256);
- }
- }
- }
- private Dictionary<string, string> regionNameByTabItem;
- private Dictionary<string, string> barContentByTabItem;
- private string previousBar = "";
- public string CurrentBar = "";
- public string unicode = null;
- /// <summary>
- /// 用来避免重复触发导航事件的标志符
- /// </summary>
- private bool isOpenFile = false;
- /// <summary>
- /// 鼠标滚轮缩放的缩放值
- /// </summary>
- private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- #endregion 属性、变量
- #region 命令
- public DelegateCommand LoadFile { get; set; }
- public DelegateCommand Load { get; set; }
- public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
- public DelegateCommand SaveFile { get; set; }
- public DelegateCommand SaveAsFile { get; set; }
- public DelegateCommand UndoCommand { get; set; }
- public DelegateCommand RedoCommand { get; set; }
- public DelegateCommand<object> MenuEnterReadMode { get; set; }
- public DelegateCommand PrintCommand { get; set; }
- #endregion 命令
- public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
- {
- region = regionManager;
- dialogs = dialogService;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- LoadFile = new DelegateCommand(loadFile);
- Load = new DelegateCommand(LoadControl);
- SaveFile = new DelegateCommand(() => { saveFile(); });
- SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
- UndoCommand = new DelegateCommand(Undo);
- RedoCommand = new DelegateCommand(Redo);
- TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
- ViwerRegionName = RegionNames.ViwerRegionName;
- BOTARegionName = RegionNames.BOTARegionName;
- PropertyRegionName = RegionNames.PropertyRegionName;
- BottomToolRegionName = RegionNames.BottomToolRegionName;
- ReadModeRegionName = RegionNames.ReadModeRegionName;
- MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
- PrintCommand = new DelegateCommand(ShowPrintDialog);
- //未显示时无法注册上Region名称
- ToolContentVisible = Visibility.Visible;
- ToolsBarContentVisible = Visibility.Visible;
- ToolContentRegionName = Guid.NewGuid().ToString();
- ToolsBarContentRegionName = Guid.NewGuid().ToString();
- ConverterBarContentRegionName = Guid.NewGuid().ToString();
- TextEditContentRegionName = Guid.NewGuid().ToString();
- ToolContentVisible = Visibility.Collapsed;
- ToolsBarContentVisible = Visibility.Collapsed;
- regionNameByTabItem = new Dictionary<string, string>();
- barContentByTabItem = new Dictionary<string, string>();
- InitialregionNameByTabItem(ref regionNameByTabItem);
- InitialbarContentByTabItem(ref barContentByTabItem);
- eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
- eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
- //TODO:根据缓存 选择用户上次选择的菜单
- EnterSelectedBar("TabItemAnnotation");
- }
- private void MenuEnterReadModeEvent(object obj)
- {
- }
- public void ShowPrintDialog()
- {
- DialogParameters printValue = new DialogParameters();
- printValue.Add(ParameterNames.PDFViewer, PDFViewer);
- printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
- printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
- dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
- }
- private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
- {
- dictionary.Add("TabItemPageEdit", ToolContentRegionName);
- dictionary.Add("TabItemTool", ToolsBarContentRegionName);
- //其他工具菜单栏共用一个ToolsBarContentRegionName
- dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
- dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
- dictionary.Add("TabItemScan", ToolsBarContentRegionName);
- dictionary.Add("TabItemEdit", TextEditContentRegionName);
- dictionary.Add("TabItemForm", ToolsBarContentRegionName);
- dictionary.Add("TabItemFill", ToolsBarContentRegionName);
- }
- private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
- {
- dictionary.Add("TabItemPageEdit", "PageEditContent");
- dictionary.Add("TabItemTool", "ToolsBarContent");
- dictionary.Add("TabItemAnnotation", "AnnotToolContent");
- dictionary.Add("TabItemConvert", "ConverterBarContent");
- dictionary.Add("TabItemScan", "");
- dictionary.Add("TabItemEdit", "TextEditToolContent");
- dictionary.Add("TabItemForm", "FormsToolContent");
- dictionary.Add("TabItemFill", "FillAndSignContent");
- }
- private void UpdateShowContent(string currentBar)
- {
- ToolContentVisible = Visibility.Collapsed;
- ToolsBarContentVisible = Visibility.Collapsed;
- ConverterBarContentVisible = Visibility.Collapsed;
- TextEditToolContentVisible = Visibility.Collapsed;
-
- switch (currentBar)
- {
- case "TabItemAnnotation":
- case "TabItemScan":
- case "TabItemTool":
- case "TabItemForm":
- case "TabItemFill":
- ToolsBarContentVisible = Visibility.Visible;
- break;
- case "TabItemEdit":
- TextEditToolContentVisible = Visibility.Visible;
- break;
- case "TabItemConvert":
- ConverterBarContentVisible = Visibility.Visible;
- break;
- case "TabItemPageEdit":
- case "HeaderFooterContent":
- case "BatesContent":
- case "WatermarkContent":
- case "BackgroundContent":
- case "RedactionContent":
- ToolContentVisible = Visibility.Visible;
- break;
- default:
- break;
- }
- PDFEditMode(currentBar);
- }
- private void PDFEditMode(string currentBar)
- {
- if(currentBar == "TabItemEdit")
- {
- if (PDFViewer != null)
- {
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- SelectedPrpoertyPanel("TextEditProperty", null);
- IsPropertyOpen = true;
- }
- }
- else
- {
- if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
- {
- IsPropertyOpen = false;
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- }
- }
- }
- private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if (!isInPageEdit)
- {
- //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
- //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
- if (e.PropertyName == "CanUndo")
- {
- CanUndo = PDFViewer.UndoManager.CanUndo;
- }
- if (e.PropertyName == "CanRedo")
- {
- CanRedo = PDFViewer.UndoManager.CanRedo;
- }
- }
- if (e.PropertyName == "CanSave")
- {
- CanSave = PDFViewer.UndoManager.CanSave;
- }
- }
- /// <summary>
- /// 选项卡切换事件
- /// </summary>
- /// <param name="e"></param>
- private void TabControlSelectonChangedEvent(object e)
- {
- var args = e as SelectionChangedEventArgs;
- if (args != null)
- {
- var item = args.AddedItems[0] as TabItem;
- CurrentBar = item.Name;
- if (previousBar != CurrentBar)
- {
- if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
- {
- EnterToolMode(barContentByTabItem[CurrentBar]);
- isInPageEdit = true;
- }
- else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
- {
- EnterSelectedBar(CurrentBar);
- isInPageEdit = false;
- }
- previousBar = CurrentBar;
- }
- }
- }
- /// <summary>
- /// 阅读模式
- /// </summary>
- /// <param name="viewContent"></param>
- public async void RbtnReadMode(ViewContent viewContent)
- {
- App.IsBookMode = true;
- IsLoading = Visibility.Visible;
- await Task.Delay(1);
- NavigationParameters param = new NavigationParameters();
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.ViewContentViewModel, this);
- region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
- //ShowContent(CurrentBar, true);
- if (GridToolRow != 0)
- {
- GridToolRow = 0;
- }
- if (GridToolRowSpan != 4)
- {
- GridToolRowSpan = 4;
- }
- //isInPageEdit = true;
- UpdateShowContent("TabItemPageEdit");
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 退出阅读模式
- /// </summary>
- public async void UnReadModel()
- {
- App.IsBookMode = false;
- IsLoading = Visibility.Visible;
- await Task.Delay(1);
- //region.AddToRegion(ViwerRegionName, PDFViewer);
- if (region.Regions.ContainsRegionWithName(ViwerRegionName))
- {
- if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
- {
- var contentRegion = region.Regions[ViwerRegionName];
- contentRegion.Remove(PDFViewer);
- }
- region.AddToRegion(ViwerRegionName, PDFViewer);
- }
- if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
- {
- EnterSelectedBar("TabItemAnnotation");
- }
- else
- {
- EnterSelectedBar(CurrentBar);
- }
- //isInPageEdit = false;
- //ShowContent(CurrentBar, false);
- //isInPageEdit = false;
- IsLoading = Visibility.Collapsed;
- }
- #region PDFViewer鼠标滚轮缩放事件
- public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
- {
- double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
- PDFViewer.Zoom(newZoom);
- }
- private double CheckZoomLevel(double zoom, bool IsGrowth)
- {
- double standardZoom = 100;
- if (zoom <= 0.01)
- {
- return 0.01;
- }
- if (zoom >= 10)
- {
- return 10;
- }
- zoom *= 100;
- for (int i = 0; i < zoomLevel.Length - 1; i++)
- {
- if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
- {
- standardZoom = zoomLevel[i + 1];
- break;
- }
- if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
- {
- standardZoom = zoomLevel[i];
- break;
- }
- }
- return standardZoom / 100;
- }
- #endregion PDFViewer鼠标滚轮缩放事件
- #region Navigate
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- if (isOpenFile)
- return;
- var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
- if (mainVM != null)
- {
- mainViewModel = mainVM;
- }
- var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
- if (pdfview != null)
- {
- PDFViewer = pdfview;
- loadFile();
- }
- isOpenFile = true;
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- #endregion Navigate
- #region 方法
- private void Undo()
- {
- if (isInPageEdit)
- {
- //执行页面编辑的Undo
- PageEditUndo?.Invoke();
- }
- else
- {
- PDFViewer.UndoManager.Undo();
- }
- }
- private void Redo()
- {
- if (isInPageEdit)
- {
- //执行页面编辑的Redo
- PageEditRedo?.Invoke();
- }
- else
- {
- PDFViewer.UndoManager.Redo();
- }
- }
- private void LoadControl()
- {
- //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- NavigationParameters parameters = new NavigationParameters();
- parameters.Add(ParameterNames.PDFViewer, PDFViewer);
- parameters.Add(ParameterNames.ViewContentViewModel, this);
- region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
- region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
- region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
- //TODO 根据上一次关闭记录的菜单,选中TabItem
- EnterSelectedBar("TabItemAnnotation");
- }
- ));
- }
- /// <summary>
- /// 各个注释(选中和创建注释)导航到对应注释的属性面板
- /// </summary>
- /// <param name="Content"></param>
- /// <param name="annotPropertyPanel"></param>
- public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
- {
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- NavigationParameters parameters = new NavigationParameters();
- //传其他参数:文档类,空注释面板;
- parameters.Add(ParameterNames.PDFViewer, PDFViewer);
- parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
- region.RequestNavigate(PropertyRegionName, Content, parameters);
- }
- ));
- }
- /// <summary>
- /// 将PDFViwer添加到Region
- /// </summary>
- private void loadFile()
- {
- PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
- PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
- CanSave = PDFViewer.UndoManager.CanSave;
- CanUndo = PDFViewer.UndoManager.CanUndo;
- CanRedo = PDFViewer.UndoManager.CanRedo;
- region.AddToRegion(ViwerRegionName, PDFViewer);
- }
- /// <summary>
- /// 已有路径文档的保存逻辑
- /// </summary>
- private bool saveFile()
- {
- try
- {
- if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
- return saveAsFile();
- //文档已被修复时 提示另存为
- if (PDFViewer.Document.HasRepaired)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
- if (alertsMessage.result == ContentResult.Ok)
- return saveAsFile();
- else
- return false;
- }
- //文件路径无法存在时
- if (!File.Exists(PDFViewer.Document.FilePath))
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
- if (alertsMessage.result == ContentResult.Ok)
- return saveAsFile();
- else
- return false;
- }
- //只读文件无法写入时,提示另存为
- FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
- if (fileInfo.IsReadOnly)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
- if (alertsMessage.result == ContentResult.Ok)
- return saveAsFile();
- else
- return false;
- }
- bool result = PDFViewer.Document.WriteToLoadedPath();
- if (result)
- {
- PDFViewer.UndoManager.CanSave = false;
- App.Current.Dispatcher.Invoke(() =>
- {
- //TODO:更新缩略图
- //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
- //try
- //{
- // if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
- // {
- // var size = PdfViewer.Document.GetPageSize(0);
- // System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
- // string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
- // if (File.Exists(folderPath))
- // File.Delete(folderPath);
- // DirectoryInfo folder = new DirectoryInfo(folderPath);
- // if (!folder.Exists)
- // folder.Create();
- // string imagePath = info.ThumbImgPath;
- // if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
- // {
- // string imageName = Guid.NewGuid().ToString();
- // imagePath = System.IO.Path.Combine(folderPath, imageName);
- // using (FileStream stream = new FileStream(imagePath, FileMode.Create))
- // {
- // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- // }
- // }
- // else
- // {
- // using (FileStream stream = new FileStream(imagePath, FileMode.Open))
- // {
- // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- // }
- // }
- // info.ThumbImgPath = imagePath;
- // SettingHelper.SetFileInfo(info);
- // }
- //}
- //catch
- //{
- // info.ThumbImgPath = null;
- // SettingHelper.SetFileInfo(info);
- //}
- });
- }
- else
- {
- //文件被占用 保存失败时
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
- if (alertsMessage.result == ContentResult.Ok)
- return saveAsFile();
- else
- return false;
- }
- return result;
- }
- catch { return false; }
- }
- /// <summary>
- /// 另存为或新文档保存逻辑
- /// </summary>
- private bool saveAsFile()
- {
- var dlg = new SaveFileDialog();
- dlg.Filter = Properties.Resources.OpenDialogFilter;
- dlg.FileName = PDFViewer.Document.FileName;
- if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
- {
- bool result = false;
- if (App.OpenedFileList.Contains(dlg.FileName))
- {
- //提示文件已经被打开
- }
- else
- {
- result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
- if (result)
- {
- DoAfterSaveAs(dlg.FileName);
- }
- else
- {
- //提示文件被其他软件占用 无法保存
- //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
- }
- ;
- }
- return result;
- }
- else
- return false;
- }
- /// <summary>
- /// 另存为后进行的操作
- /// 重新打开新文档
- /// </summary>
- /// <param name="targetPath"></param>
- public void DoAfterSaveAs(string targetPath)
- {
- App.OpenedFileList.Remove(targetPath);
- string oldFilePath = targetPath;
- PDFViewer.UndoManager.CanSave = false;
- mainViewModel.OpenFile(targetPath);
- //TODO:通知各模块更新PDFview对象
- //var result = OpenFile(targetPath, true);
- //if (result)
- //{
- // FileChanged.Invoke(this, null);
- // Zoomer.PdfViewer = PdfViewer;
- // PageSelector.PdfViewer = PdfViewer;
- // ViewModeSelector.PdfViewer = PdfViewer;
- // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
- // if (fileInfo != null)
- // {
- // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
- // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
- // if (fileInfo.LastFitMode == FitMode.FitFree)
- // PdfViewer.Zoom(fileInfo.LastZoom);
- // PdfViewer.GoToPage(fileInfo.LastPageIndex);
- // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
- // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
- // else
- // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
- // }
- //}
- }
- /// <summary>
- /// 显示前添加内容到Region
- /// </summary>
- /// <param name="isPageEdit"></param>
- private void ShowContent(string currentBar, bool isToolMode = false)
- {
- GridVisibility = Visibility.Visible;
- //显示页面编辑或其他工具
- if (currentBar == "TabItemPageEdit" || isToolMode)
- {
- if (currentBar == "TabItemPageEdit")//进入页面编辑
- {
- if (GridToolRow != 1)
- {
- GridToolRow = 1;
- }
- if (GridToolRowSpan != 3)
- {
- GridToolRowSpan = 3;
- }
- }
- else//进入水印等其他工具模式
- {
- GridVisibility = Visibility.Collapsed;
- if (GridToolRow != 0)
- {
- GridToolRow = 0;
- }
- if (GridToolRowSpan != 4)
- {
- GridToolRowSpan = 4;
- }
- }
- //ToolContent的visible跟toolsbarContent 的visible是互斥的
- UpdateShowContent(currentBar);
- }
- else
- {
- if (GridToolRow != 1)
- {
- GridToolRow = 1;
- }
- UpdateShowContent(currentBar);
- }
- }
- /// <summary>
- /// 从二级工具栏进入需要修改界面布局的场景
- /// </summary>
- /// <param name="EditToolName"></param>
- private void EnterEditTools(StringWithUnicode EditToolName)
- {
- EnterSelectedEditTool(EditToolName);
- }
- private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
- {
- EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
- if (enumCloseMode == EnumCloseMode.StatusConfirm)
- {
- PDFViewer.Document.ReleasePages();
- PDFViewer.ReloadDocument();
- }
- //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
- if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
- {
- region.AddToRegion(ViwerRegionName, PDFViewer);
- }
- CurrentBar = "TabItemTool";
- EnterSelectedBar(CurrentBar);
- }
- /// <summary>
- /// 二级菜单指定目标处理
- /// </summary>
- /// <param name="e"></param>
- private void EnterSelectedEditTool(StringWithUnicode EditToolName)
- {
- CurrentBar = EditToolName.EditToolsContentName;
- EnterToolMode(CurrentBar);
- }
- /// <summary>
- /// 进入工具编辑(如页面编辑、水印、密文等)模式
- /// </summary>
- /// <param name="targetToolMode">要导航过去的控件名称</param>
- /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
- private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
- {
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- NavigationParameters param = new NavigationParameters();
- if (valuePairs == null)
- {
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.ViewContentViewModel, this);
- }
- else//有传入其他内容的参数时
- {
- param = valuePairs;
- }
- region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
- ShowContent(CurrentBar, true);
- IsLoading = Visibility.Collapsed;
- }
- private void EnterSelectedBar(string currentBar)
- {
- NavigationParameters param = new NavigationParameters();
- param.Add(ParameterNames.PDFViewer, PDFViewer);
- param.Add(ParameterNames.ViewContentViewModel, this);
- region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
- ShowContent(currentBar);
- }
- /// <summary>
- /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
- /// </summary>
- public void ExitToolMode()
- {
- ToolContentVisible = Visibility.Collapsed;
- if (isInPageEdit)
- {
- TabSelectedIndex = 0;
- isInPageEdit = false;
- }
- }
- #endregion 方法
- }
- }
|