ViewContentViewModel.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. using Microsoft.Win32;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using ComPDFKitViewer.PdfViewer;
  10. using Prism.Regions;
  11. using DryIoc;
  12. using System.Diagnostics;
  13. using Prism.Services.Dialogs;
  14. using PDF_Office.CustomControl;
  15. using PDF_Office.Model;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.IO;
  19. using PDFSettings;
  20. using ComPDFKitViewer.AnnotEvent;
  21. using PDF_Office.ViewModels.Tools;
  22. using PDF_Office.Views;
  23. using Prism.Events;
  24. using PDF_Office.EventAggregators;
  25. using PDF_Office.Views.EditTools.Background;
  26. namespace PDF_Office.ViewModels
  27. {
  28. public class ViewContentViewModel : BindableBase, INavigationAware
  29. {
  30. #region 属性、变量
  31. private CPDFViewer PDFViewer { get; set; }
  32. private MainContentViewModel mainViewModel { get; set; }
  33. public IRegionManager region;
  34. public IDialogService dialogs;
  35. public string ViwerRegionName { get; set; }
  36. public string BOTARegionName { get; set; }
  37. public string PropertyRegionName { get; set; }
  38. public string ToolContentRegionName { get; set; }
  39. public string ToolsBarContentRegionName { get; set; }
  40. public string ReadModeRegionName { get; set; }
  41. public string ConverterBarContentRegionName { get; set; }
  42. public string BackgroundContentRegionName { get; set; }
  43. /// <summary>
  44. /// 底部工具栏 RegionName
  45. /// </summary>
  46. public string BottomToolRegionName { get; set; }
  47. private bool _isInPageEdit = false;
  48. /// <summary>
  49. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  50. /// </summary>
  51. public bool isInPageEdit
  52. {
  53. get { return _isInPageEdit; }
  54. set { _isInPageEdit = value;
  55. if (!value)
  56. {
  57. CanRedo = PDFViewer.UndoManager.CanRedo;
  58. CanUndo = PDFViewer.UndoManager.CanUndo;
  59. }
  60. }
  61. }
  62. public Action PageEditUndo { get; set; }
  63. public Action PageEditRedo { get; set; }
  64. /// <summary>
  65. ///工具条
  66. ///0:收起
  67. ///40:显示
  68. /// </summary>
  69. private int toolRowHeight = 40;
  70. public int ToolRowHeight
  71. {
  72. get { return toolRowHeight; }
  73. set
  74. {
  75. SetProperty(ref toolRowHeight, value);
  76. }
  77. }
  78. /// <summary>
  79. /// 水印,背景侧边栏宽度
  80. /// 0:收起
  81. /// 260:显示
  82. /// </summary>
  83. private int propertyColumnWidth = 0;
  84. public int PropertyColumnWidth
  85. {
  86. get { return propertyColumnWidth; }
  87. set
  88. {
  89. SetProperty(ref propertyColumnWidth, value);
  90. }
  91. }
  92. private int gridToolRow = 1;
  93. /// <summary>
  94. /// 控制ToolContent的Row
  95. /// </summary>
  96. public int GridToolRow
  97. {
  98. get { return gridToolRow; }
  99. set
  100. {
  101. SetProperty(ref gridToolRow, value);
  102. }
  103. }
  104. private int gridToolRowSpan = 3;
  105. /// <summary>
  106. /// 控制ToolContent的RowSpan
  107. /// </summary>
  108. public int GridToolRowSpan
  109. {
  110. get { return gridToolRowSpan; }
  111. set
  112. {
  113. SetProperty(ref gridToolRowSpan, value);
  114. }
  115. }
  116. private Visibility toolContentVisible = Visibility.Collapsed;
  117. /// <summary>
  118. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  119. /// 留意:显示前需要先注入内容、设置好行和跨行数
  120. /// </summary>
  121. public Visibility ToolContentVisible
  122. {
  123. get { return toolContentVisible; }
  124. set
  125. {
  126. SetProperty(ref toolContentVisible, value);
  127. }
  128. }
  129. private Visibility gridVisibility = Visibility.Visible;
  130. /// <summary>
  131. /// 是否正在加载中
  132. /// </summary>
  133. public Visibility GridVisibility
  134. {
  135. get { return gridVisibility; }
  136. set
  137. {
  138. SetProperty(ref gridVisibility, value);
  139. }
  140. }
  141. private Visibility isLoading = Visibility.Collapsed;
  142. /// <summary>
  143. /// 是否正在加载中
  144. /// </summary>
  145. public Visibility IsLoading
  146. {
  147. get { return isLoading; }
  148. set
  149. {
  150. SetProperty(ref isLoading, value);
  151. }
  152. }
  153. private Visibility converterBarContentVisible = Visibility.Collapsed;
  154. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  155. /// <summary>
  156. /// 控制ToolsBarContent的显示
  157. /// 留意:显示前需要先注入内容、设置好行和跨行数
  158. /// </summary>
  159. public Visibility ConverterBarContentVisible
  160. {
  161. get { return converterBarContentVisible; }
  162. set
  163. {
  164. SetProperty(ref converterBarContentVisible, value);
  165. }
  166. }
  167. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  168. /// <summary>
  169. /// 控制ToolsBarContent的显示
  170. /// 留意:显示前需要先注入内容、设置好行和跨行数
  171. /// </summary>
  172. public Visibility ToolsBarContentVisible
  173. {
  174. get { return toolsBarContentVisible; }
  175. set
  176. {
  177. SetProperty(ref toolsBarContentVisible, value);
  178. }
  179. }
  180. private bool isPorpertyOpen = false;
  181. /// <summary>
  182. /// 属性栏是否展开
  183. /// </summary>
  184. public bool IsPropertyOpen
  185. {
  186. get { return isPorpertyOpen; }
  187. set
  188. {
  189. SetProperty(ref isPorpertyOpen, value);
  190. }
  191. }
  192. private Visibility isReadMode = Visibility.Visible;
  193. /// <summary>
  194. ///是否为阅读模式
  195. /// </summary>
  196. public Visibility IsReadMode
  197. {
  198. get { return isReadMode; }
  199. set
  200. {
  201. SetProperty(ref isReadMode, value);
  202. }
  203. }
  204. private bool canSave;
  205. /// <summary>
  206. /// 是否可以保存
  207. /// </summary>
  208. public bool CanSave
  209. {
  210. get { return canSave; }
  211. set
  212. {
  213. SetProperty(ref canSave, value);
  214. }
  215. }
  216. private bool canUndo;
  217. /// <summary>
  218. /// 是否可以进行Undo
  219. /// </summary>
  220. public bool CanUndo
  221. {
  222. get { return canUndo; }
  223. set
  224. {
  225. SetProperty(ref canUndo, value);
  226. }
  227. }
  228. private bool canRedo;
  229. /// <summary>
  230. /// 是否可以进行Redo
  231. /// </summary>
  232. public bool CanRedo
  233. {
  234. get { return canRedo; }
  235. set
  236. {
  237. SetProperty(ref canRedo, value);
  238. }
  239. }
  240. private GridLength botaWidth = new GridLength(48);
  241. /// <summary>
  242. /// BOTA栏的宽度
  243. /// </summary>
  244. public GridLength BOTAWidth
  245. {
  246. get { return botaWidth; }
  247. set
  248. {
  249. SetProperty(ref botaWidth, value);
  250. if (botaWidth.Value <= 48)
  251. {
  252. OpenBOTA = false;
  253. }
  254. }
  255. }
  256. private int selectedIndex;
  257. /// <summary>
  258. /// 工具栏选中项的索引
  259. /// </summary>
  260. public int TabSelectedIndex
  261. {
  262. get { return selectedIndex; }
  263. set
  264. {
  265. SetProperty(ref selectedIndex, value);
  266. }
  267. }
  268. private bool openBOTA = false;
  269. /// <summary>
  270. /// 是否展开BOTA
  271. /// </summary>
  272. public bool OpenBOTA
  273. {
  274. get { return openBOTA; }
  275. set
  276. {
  277. openBOTA = value;
  278. if (openBOTA && BOTAWidth.Value <= 48)
  279. {
  280. BOTAWidth = new GridLength(256);
  281. }
  282. }
  283. }
  284. private Dictionary<string, string> regionNameByTabItem;
  285. private Dictionary<string, string> barContentByTabItem;
  286. private string previousBar = "";
  287. public string CurrentBar = "";
  288. /// <summary>
  289. /// 用来避免重复触发导航事件的标志符
  290. /// </summary>
  291. private bool isOpenFile = false;
  292. /// <summary>
  293. /// 鼠标滚轮缩放的缩放值
  294. /// </summary>
  295. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  296. #endregion 属性、变量
  297. #region 命令
  298. public DelegateCommand LoadFile { get; set; }
  299. public DelegateCommand Load { get; set; }
  300. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  301. public DelegateCommand SaveFile { get; set; }
  302. public DelegateCommand SaveAsFile { get; set; }
  303. public DelegateCommand UndoCommand { get; set; }
  304. public DelegateCommand RedoCommand { get; set; }
  305. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  306. #endregion 命令
  307. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  308. {
  309. region = regionManager;
  310. dialogs = dialogService;
  311. LoadFile = new DelegateCommand(loadFile);
  312. Load = new DelegateCommand(LoadControl);
  313. SaveFile = new DelegateCommand(() => { saveFile(); });
  314. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  315. UndoCommand = new DelegateCommand(Undo);
  316. RedoCommand = new DelegateCommand(Redo);
  317. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  318. ViwerRegionName = RegionNames.ViwerRegionName;
  319. BOTARegionName = RegionNames.BOTARegionName;
  320. PropertyRegionName = RegionNames.PropertyRegionName;
  321. BottomToolRegionName = RegionNames.BottomToolRegionName;
  322. ReadModeRegionName = RegionNames.ReadModeRegionName;
  323. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  324. //未显示时无法注册上Region名称
  325. ToolContentVisible = Visibility.Visible;
  326. ToolsBarContentVisible = Visibility.Visible;
  327. ToolContentRegionName = Guid.NewGuid().ToString();
  328. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  329. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  330. ToolContentVisible = Visibility.Collapsed;
  331. ToolsBarContentVisible = Visibility.Collapsed;
  332. regionNameByTabItem = new Dictionary<string, string>();
  333. barContentByTabItem = new Dictionary<string, string>();
  334. InitialregionNameByTabItem(ref regionNameByTabItem);
  335. InitialbarContentByTabItem(ref barContentByTabItem);
  336. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools);
  337. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool);
  338. //TODO:根据缓存 选择用户上次选择的菜单
  339. EnterSelectedBar("TabItemAnnotation");
  340. }
  341. private void MenuEnterReadModeEvent(object obj)
  342. {
  343. }
  344. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  345. {
  346. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  347. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  348. //其他工具菜单栏共用一个ToolsBarContentRegionName
  349. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  350. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  351. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  352. dictionary.Add("TabItemEdit", ToolsBarContentRegionName);
  353. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  354. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  355. }
  356. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  357. {
  358. dictionary.Add("TabItemPageEdit", "PageEditContent");
  359. dictionary.Add("TabItemTool", "ToolsBarContent");
  360. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  361. dictionary.Add("TabItemConvert", "ConverterBarContent");
  362. dictionary.Add("TabItemScan", "");
  363. dictionary.Add("TabItemEdit", "");
  364. dictionary.Add("TabItemForm", "");
  365. dictionary.Add("TabItemFill", "FillAndSignContent");
  366. }
  367. private void UpdateShowContent(string currentBar)
  368. {
  369. ToolContentVisible = Visibility.Collapsed;
  370. ToolsBarContentVisible = Visibility.Collapsed;
  371. ConverterBarContentVisible = Visibility.Collapsed;
  372. switch (currentBar)
  373. {
  374. case "TabItemAnnotation":
  375. case "TabItemScan":
  376. case "TabItemTool":
  377. case "TabItemForm":
  378. case "TabItemEdit":
  379. ToolsBarContentVisible = Visibility.Visible;
  380. if(currentBar == "TabItemEdit")
  381. {
  382. IsPropertyOpen = !IsPropertyOpen;
  383. if(IsPropertyOpen == true)
  384. SelectedPrpoertyPanel("TextEditProperty", null);
  385. }
  386. break;
  387. case "TabItemConvert":
  388. ConverterBarContentVisible = Visibility.Visible;
  389. break;
  390. case "TabItemPageEdit":
  391. case "WatermarkContent":
  392. case "BackgroundContent":
  393. ToolContentVisible = Visibility.Visible;
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  400. {
  401. if (!isInPageEdit)
  402. {
  403. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  404. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  405. if (e.PropertyName == "CanUndo")
  406. {
  407. CanUndo = PDFViewer.UndoManager.CanUndo;
  408. }
  409. if (e.PropertyName == "CanRedo")
  410. {
  411. CanRedo = PDFViewer.UndoManager.CanRedo;
  412. }
  413. }
  414. if (e.PropertyName == "CanSave")
  415. {
  416. CanSave = PDFViewer.UndoManager.CanSave;
  417. }
  418. }
  419. /// <summary>
  420. /// 选项卡切换事件
  421. /// </summary>
  422. /// <param name="e"></param>
  423. private void TabControlSelectonChangedEvent(object e)
  424. {
  425. var args = e as SelectionChangedEventArgs;
  426. if (args != null)
  427. {
  428. var item = args.AddedItems[0] as TabItem;
  429. CurrentBar = item.Name;
  430. if (previousBar != CurrentBar)
  431. {
  432. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  433. {
  434. EnterToolMode(barContentByTabItem[CurrentBar]);
  435. isInPageEdit = true;
  436. }
  437. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  438. {
  439. EnterSelectedBar(CurrentBar);
  440. isInPageEdit = false;
  441. }
  442. previousBar = CurrentBar;
  443. }
  444. }
  445. }
  446. #region PDFViewer鼠标滚轮缩放事件
  447. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  448. {
  449. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  450. PDFViewer.Zoom(newZoom);
  451. }
  452. private double CheckZoomLevel(double zoom, bool IsGrowth)
  453. {
  454. double standardZoom = 100;
  455. if (zoom <= 0.01)
  456. {
  457. return 0.01;
  458. }
  459. if (zoom >= 10)
  460. {
  461. return 10;
  462. }
  463. zoom *= 100;
  464. for (int i = 0; i < zoomLevel.Length - 1; i++)
  465. {
  466. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  467. {
  468. standardZoom = zoomLevel[i + 1];
  469. break;
  470. }
  471. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  472. {
  473. standardZoom = zoomLevel[i];
  474. break;
  475. }
  476. }
  477. return standardZoom / 100;
  478. }
  479. #endregion PDFViewer鼠标滚轮缩放事件
  480. #region Navigate
  481. public void OnNavigatedTo(NavigationContext navigationContext)
  482. {
  483. if (isOpenFile)
  484. return;
  485. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  486. if (mainVM != null)
  487. {
  488. mainViewModel = mainVM;
  489. }
  490. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  491. if (pdfview != null)
  492. {
  493. PDFViewer = pdfview;
  494. loadFile();
  495. }
  496. isOpenFile = true;
  497. }
  498. public bool IsNavigationTarget(NavigationContext navigationContext)
  499. {
  500. return true;
  501. }
  502. public void OnNavigatedFrom(NavigationContext navigationContext)
  503. {
  504. }
  505. #endregion Navigate
  506. #region 方法
  507. private void Undo()
  508. {
  509. if (isInPageEdit)
  510. {
  511. //执行页面编辑的Undo
  512. PageEditUndo?.Invoke();
  513. }
  514. else
  515. {
  516. PDFViewer.UndoManager.Undo();
  517. }
  518. }
  519. private void Redo()
  520. {
  521. if (isInPageEdit)
  522. {
  523. //执行页面编辑的Redo
  524. PageEditRedo?.Invoke();
  525. }
  526. else
  527. {
  528. PDFViewer.UndoManager.Redo();
  529. }
  530. }
  531. private void LoadControl()
  532. {
  533. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  534. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  535. {
  536. NavigationParameters parameters = new NavigationParameters();
  537. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  538. parameters.Add(ParameterNames.ViewContentViewModel, this);
  539. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  540. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  541. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  542. //TODO 根据上一次关闭记录的菜单,选中TabItem
  543. EnterSelectedBar("TabItemAnnotation");
  544. }
  545. ));
  546. }
  547. /// <summary>
  548. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  549. /// </summary>
  550. /// <param name="Content"></param>
  551. /// <param name="annotPropertyPanel"></param>
  552. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  553. {
  554. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  555. {
  556. NavigationParameters parameters = new NavigationParameters();
  557. //传其他参数:文档类,空注释面板;
  558. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  559. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  560. region.RequestNavigate(PropertyRegionName, Content, parameters);
  561. }
  562. ));
  563. }
  564. /// <summary>
  565. /// 将PDFViwer添加到Region
  566. /// </summary>
  567. private void loadFile()
  568. {
  569. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  570. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  571. CanSave = PDFViewer.UndoManager.CanSave;
  572. CanUndo = PDFViewer.UndoManager.CanUndo;
  573. CanRedo = PDFViewer.UndoManager.CanRedo;
  574. region.AddToRegion(ViwerRegionName, PDFViewer);
  575. }
  576. /// <summary>
  577. /// 已有路径文档的保存逻辑
  578. /// </summary>
  579. private bool saveFile()
  580. {
  581. try
  582. {
  583. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  584. return saveAsFile();
  585. //文档已被修复时 提示另存为
  586. if (PDFViewer.Document.HasRepaired)
  587. {
  588. AlertsMessage alertsMessage = new AlertsMessage();
  589. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  590. if (alertsMessage.result == ContentResult.Ok)
  591. return saveAsFile();
  592. else
  593. return false;
  594. }
  595. //文件路径无法存在时
  596. if (!File.Exists(PDFViewer.Document.FilePath))
  597. {
  598. AlertsMessage alertsMessage = new AlertsMessage();
  599. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  600. if (alertsMessage.result == ContentResult.Ok)
  601. return saveAsFile();
  602. else
  603. return false;
  604. }
  605. //只读文件无法写入时,提示另存为
  606. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  607. if (fileInfo.IsReadOnly)
  608. {
  609. AlertsMessage alertsMessage = new AlertsMessage();
  610. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  611. if (alertsMessage.result == ContentResult.Ok)
  612. return saveAsFile();
  613. else
  614. return false;
  615. }
  616. bool result = PDFViewer.Document.WriteToLoadedPath();
  617. if (result)
  618. {
  619. PDFViewer.UndoManager.CanSave = false;
  620. App.Current.Dispatcher.Invoke(() =>
  621. {
  622. //TODO:更新缩略图
  623. //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
  624. //try
  625. //{
  626. // if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  627. // {
  628. // var size = PdfViewer.Document.GetPageSize(0);
  629. // System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  630. // string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  631. // if (File.Exists(folderPath))
  632. // File.Delete(folderPath);
  633. // DirectoryInfo folder = new DirectoryInfo(folderPath);
  634. // if (!folder.Exists)
  635. // folder.Create();
  636. // string imagePath = info.ThumbImgPath;
  637. // if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  638. // {
  639. // string imageName = Guid.NewGuid().ToString();
  640. // imagePath = System.IO.Path.Combine(folderPath, imageName);
  641. // using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  642. // {
  643. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  644. // }
  645. // }
  646. // else
  647. // {
  648. // using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  649. // {
  650. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  651. // }
  652. // }
  653. // info.ThumbImgPath = imagePath;
  654. // SettingHelper.SetFileInfo(info);
  655. // }
  656. //}
  657. //catch
  658. //{
  659. // info.ThumbImgPath = null;
  660. // SettingHelper.SetFileInfo(info);
  661. //}
  662. });
  663. }
  664. else
  665. {
  666. //文件被占用 保存失败时
  667. AlertsMessage alertsMessage = new AlertsMessage();
  668. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  669. if (alertsMessage.result == ContentResult.Ok)
  670. return saveAsFile();
  671. else
  672. return false;
  673. }
  674. return result;
  675. }
  676. catch { return false; }
  677. }
  678. /// <summary>
  679. /// 另存为或新文档保存逻辑
  680. /// </summary>
  681. private bool saveAsFile()
  682. {
  683. var dlg = new SaveFileDialog();
  684. dlg.Filter = Properties.Resources.OpenDialogFilter;
  685. dlg.FileName = PDFViewer.Document.FileName;
  686. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  687. {
  688. bool result = false;
  689. if (App.OpenedFileList.Contains(dlg.FileName))
  690. {
  691. //提示文件已经被打开
  692. }
  693. else
  694. {
  695. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  696. if (result)
  697. {
  698. DoAfterSaveAs(dlg.FileName);
  699. }
  700. else
  701. {
  702. //提示文件被其他软件占用 无法保存
  703. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  704. }
  705. ;
  706. }
  707. return result;
  708. }
  709. else
  710. return false;
  711. }
  712. /// <summary>
  713. /// 另存为后进行的操作
  714. /// 重新打开新文档
  715. /// </summary>
  716. /// <param name="targetPath"></param>
  717. public void DoAfterSaveAs(string targetPath)
  718. {
  719. App.OpenedFileList.Remove(targetPath);
  720. string oldFilePath = targetPath;
  721. PDFViewer.UndoManager.CanSave = false;
  722. mainViewModel.OpenFile(targetPath);
  723. //TODO:通知各模块更新PDFview对象
  724. //var result = OpenFile(targetPath, true);
  725. //if (result)
  726. //{
  727. // FileChanged.Invoke(this, null);
  728. // Zoomer.PdfViewer = PdfViewer;
  729. // PageSelector.PdfViewer = PdfViewer;
  730. // ViewModeSelector.PdfViewer = PdfViewer;
  731. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  732. // if (fileInfo != null)
  733. // {
  734. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  735. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  736. // if (fileInfo.LastFitMode == FitMode.FitFree)
  737. // PdfViewer.Zoom(fileInfo.LastZoom);
  738. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  739. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  740. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  741. // else
  742. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  743. // }
  744. //}
  745. }
  746. /// <summary>
  747. /// 显示前添加内容到Region
  748. /// </summary>
  749. /// <param name="isPageEdit"></param>
  750. private void ShowContent(string currentBar, bool isToolMode = false)
  751. {
  752. GridVisibility = Visibility.Visible;
  753. //显示页面编辑或其他工具
  754. if (currentBar == "TabItemPageEdit" || isToolMode)
  755. {
  756. if (currentBar == "TabItemPageEdit")//进入页面编辑
  757. {
  758. if (GridToolRow != 1)
  759. {
  760. GridToolRow = 1;
  761. }
  762. if (GridToolRowSpan != 3)
  763. {
  764. GridToolRowSpan = 3;
  765. }
  766. }
  767. else//进入水印等其他工具模式
  768. {
  769. GridVisibility = Visibility.Collapsed;
  770. if (GridToolRow != 0)
  771. {
  772. GridToolRow = 0;
  773. }
  774. if (GridToolRowSpan != 4)
  775. {
  776. GridToolRowSpan = 4;
  777. }
  778. }
  779. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  780. UpdateShowContent(currentBar);
  781. }
  782. else
  783. {
  784. if (GridToolRow != 1)
  785. {
  786. GridToolRow = 1;
  787. }
  788. UpdateShowContent(currentBar);
  789. }
  790. }
  791. /// <summary>
  792. /// 从二级工具栏进入需要修改界面布局的场景
  793. /// </summary>
  794. /// <param name="EditToolName"></param>
  795. private void EnterEditTools(string EditToolName)
  796. {
  797. EnterSelectedEditTool(EditToolName);
  798. }
  799. private void CloseEditTool()
  800. {
  801. CurrentBar = "TabItemTool";
  802. EnterSelectedBar(CurrentBar);
  803. }
  804. /// <summary>
  805. /// 二级菜单指定目标处理
  806. /// </summary>
  807. /// <param name="e"></param>
  808. private void EnterSelectedEditTool(string EditToolName)
  809. {
  810. CurrentBar = EditToolName;
  811. EnterToolMode(EditToolName);
  812. }
  813. /// <summary>
  814. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  815. /// </summary>
  816. /// <param name="targetToolMode">要导航过去的控件名称</param>
  817. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  818. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  819. {
  820. IsLoading = Visibility.Visible;
  821. await Task.Delay(3);
  822. NavigationParameters param = new NavigationParameters();
  823. if (valuePairs == null)
  824. {
  825. param.Add(ParameterNames.PDFViewer, PDFViewer);
  826. param.Add(ParameterNames.ViewContentViewModel, this);
  827. }
  828. else//有传入其他内容的参数时
  829. {
  830. param = valuePairs;
  831. }
  832. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  833. ShowContent(CurrentBar, true);
  834. IsLoading = Visibility.Collapsed;
  835. }
  836. private void EnterSelectedBar(string currentBar)
  837. {
  838. NavigationParameters param = new NavigationParameters();
  839. param.Add(ParameterNames.PDFViewer, PDFViewer);
  840. param.Add(ParameterNames.ViewContentViewModel, this);
  841. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  842. ShowContent(currentBar);
  843. }
  844. /// <summary>
  845. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  846. /// </summary>
  847. public void ExitToolMode()
  848. {
  849. ToolContentVisible = Visibility.Collapsed;
  850. if (isInPageEdit)
  851. {
  852. TabSelectedIndex = 0;
  853. isInPageEdit = false;
  854. }
  855. }
  856. #endregion 方法
  857. }
  858. }