ViewContentViewModel.cs 39 KB

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