ViewContentViewModel.cs 34 KB

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