ViewContentViewModel.cs 32 KB

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