ViewContentViewModel.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. /// <summary>
  319. /// 注释-链接,提示语
  320. /// </summary>
  321. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  322. public Visibility LinkAnnotTipVisibility
  323. {
  324. get { return linkAnnotTipVisibility; }
  325. set
  326. {
  327. SetProperty(ref linkAnnotTipVisibility, value);
  328. }
  329. }
  330. private string linkAnnotTipText = "Please use the scroll bar, thumbnail tool to locate the target page, click or box the area to select the target range";
  331. public string LinkAnnotTipText
  332. {
  333. get { return linkAnnotTipText; }
  334. set
  335. {
  336. SetProperty(ref linkAnnotTipText, value);
  337. }
  338. }
  339. #endregion 属性、变量
  340. #region 命令
  341. public DelegateCommand LoadFile { get; set; }
  342. public DelegateCommand Load { get; set; }
  343. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  344. public DelegateCommand SaveFile { get; set; }
  345. public DelegateCommand SaveAsFile { get; set; }
  346. public DelegateCommand UndoCommand { get; set; }
  347. public DelegateCommand RedoCommand { get; set; }
  348. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  349. #endregion 命令
  350. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  351. {
  352. region = regionManager;
  353. dialogs = dialogService;
  354. events = eventAggregator;
  355. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  356. LoadFile = new DelegateCommand(loadFile);
  357. Load = new DelegateCommand(LoadControl);
  358. SaveFile = new DelegateCommand(() => { saveFile(); });
  359. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  360. UndoCommand = new DelegateCommand(Undo);
  361. RedoCommand = new DelegateCommand(Redo);
  362. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  363. ViwerRegionName = RegionNames.ViwerRegionName;
  364. SplitViewerRegionName = RegionNames.SplitScreenViewRegionName;
  365. BOTARegionName = RegionNames.BOTARegionName;
  366. PropertyRegionName = RegionNames.PropertyRegionName;
  367. BottomToolRegionName = RegionNames.BottomToolRegionName;
  368. ReadModeRegionName = RegionNames.ReadModeRegionName;
  369. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  370. //未显示时无法注册上Region名称
  371. ToolContentVisible = Visibility.Visible;
  372. ToolsBarContentVisible = Visibility.Visible;
  373. ToolContentRegionName = Guid.NewGuid().ToString();
  374. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  375. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  376. TextEditContentRegionName = Guid.NewGuid().ToString();
  377. ToolContentVisible = Visibility.Collapsed;
  378. ToolsBarContentVisible = Visibility.Collapsed;
  379. regionNameByTabItem = new Dictionary<string, string>();
  380. barContentByTabItem = new Dictionary<string, string>();
  381. InitialregionNameByTabItem(ref regionNameByTabItem);
  382. InitialbarContentByTabItem(ref barContentByTabItem);
  383. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  384. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  385. //TODO:根据缓存 选择用户上次选择的菜单
  386. EnterSelectedBar("TabItemAnnotation");
  387. }
  388. private void MenuEnterReadModeEvent(object obj)
  389. {
  390. }
  391. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  392. {
  393. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  394. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  395. //其他工具菜单栏共用一个ToolsBarContentRegionName
  396. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  397. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  398. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  399. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  400. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  401. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  402. }
  403. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  404. {
  405. dictionary.Add("TabItemPageEdit", "PageEditContent");
  406. dictionary.Add("TabItemTool", "ToolsBarContent");
  407. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  408. dictionary.Add("TabItemConvert", "ConverterBarContent");
  409. dictionary.Add("TabItemScan", "ScanContent");
  410. dictionary.Add("TabItemEdit", "TextEditToolContent");
  411. dictionary.Add("TabItemForm", "FormsToolContent");
  412. dictionary.Add("TabItemFill", "FillAndSignContent");
  413. }
  414. private void UpdateShowContent(string currentBar)
  415. {
  416. ToolContentVisible = Visibility.Collapsed;
  417. ToolsBarContentVisible = Visibility.Collapsed;
  418. ConverterBarContentVisible = Visibility.Collapsed;
  419. TextEditToolContentVisible = Visibility.Collapsed;
  420. switch (currentBar)
  421. {
  422. case "TabItemAnnotation":
  423. case "TabItemScan":
  424. case "TabItemTool":
  425. case "TabItemForm":
  426. case "TabItemFill":
  427. ToolsBarContentVisible = Visibility.Visible;
  428. break;
  429. case "TabItemEdit":
  430. TextEditToolContentVisible = Visibility.Visible;
  431. break;
  432. case "TabItemConvert":
  433. ConverterBarContentVisible = Visibility.Visible;
  434. break;
  435. case "TabItemPageEdit":
  436. case "HeaderFooterContent":
  437. case "BatesContent":
  438. case "WatermarkContent":
  439. case "BackgroundContent":
  440. case "RedactionContent":
  441. ToolContentVisible = Visibility.Visible;
  442. break;
  443. default:
  444. break;
  445. }
  446. PDFEditMode(currentBar);
  447. }
  448. private void PDFEditMode(string currentBar)
  449. {
  450. if (currentBar == "TabItemEdit")
  451. {
  452. if (PDFViewer != null)
  453. {
  454. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  455. SelectedPrpoertyPanel("TextEditProperty", null);
  456. IsPropertyOpen = true;
  457. }
  458. }
  459. else
  460. {
  461. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  462. {
  463. IsPropertyOpen = false;
  464. PDFViewer.SetMouseMode(MouseModes.PanTool);
  465. }
  466. }
  467. }
  468. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  469. {
  470. if (!isInPageEdit)
  471. {
  472. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  473. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  474. if (e.PropertyName == "CanUndo")
  475. {
  476. CanUndo = PDFViewer.UndoManager.CanUndo;
  477. }
  478. if (e.PropertyName == "CanRedo")
  479. {
  480. CanRedo = PDFViewer.UndoManager.CanRedo;
  481. }
  482. }
  483. if (e.PropertyName == "CanSave")
  484. {
  485. CanSave = PDFViewer.UndoManager.CanSave;
  486. }
  487. }
  488. /// <summary>
  489. /// 选项卡切换事件
  490. /// </summary>
  491. /// <param name="e"></param>
  492. private void TabControlSelectonChangedEvent(object e)
  493. {
  494. var args = e as SelectionChangedEventArgs;
  495. if (args != null)
  496. {
  497. var item = args.AddedItems[0] as TabItem;
  498. CurrentBar = item.Name;
  499. if (previousBar != CurrentBar)
  500. {
  501. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  502. {
  503. EnterToolMode(barContentByTabItem[CurrentBar]);
  504. isInPageEdit = true;
  505. }
  506. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  507. {
  508. EnterSelectedBar(CurrentBar);
  509. isInPageEdit = false;
  510. }
  511. previousBar = CurrentBar;
  512. }
  513. }
  514. }
  515. /// <summary>
  516. /// 阅读模式
  517. /// </summary>
  518. /// <param name="viewContent"></param>
  519. public async void RbtnReadMode(ViewContent viewContent)
  520. {
  521. App.IsBookMode = true;
  522. IsLoading = Visibility.Visible;
  523. await Task.Delay(1);
  524. NavigationParameters param = new NavigationParameters();
  525. param.Add(ParameterNames.PDFViewer, PDFViewer);
  526. param.Add(ParameterNames.ViewContentViewModel, this);
  527. region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  528. //ShowContent(CurrentBar, true);
  529. if (GridToolRow != 0)
  530. {
  531. GridToolRow = 0;
  532. }
  533. if (GridToolRowSpan != 4)
  534. {
  535. GridToolRowSpan = 4;
  536. }
  537. //isInPageEdit = true;
  538. UpdateShowContent("TabItemPageEdit");
  539. IsLoading = Visibility.Collapsed;
  540. }
  541. /// <summary>
  542. /// 退出阅读模式
  543. /// </summary>
  544. public async void UnReadModel()
  545. {
  546. App.IsBookMode = false;
  547. IsLoading = Visibility.Visible;
  548. await Task.Delay(1);
  549. //region.AddToRegion(ViwerRegionName, PDFViewer);
  550. if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  551. {
  552. if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  553. {
  554. var contentRegion = region.Regions[ViwerRegionName];
  555. contentRegion.Remove(PDFViewer);
  556. }
  557. region.AddToRegion(ViwerRegionName, PDFViewer);
  558. }
  559. if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  560. {
  561. EnterSelectedBar("TabItemAnnotation");
  562. }
  563. else
  564. {
  565. EnterSelectedBar(CurrentBar);
  566. }
  567. //isInPageEdit = false;
  568. //ShowContent(CurrentBar, false);
  569. //isInPageEdit = false;
  570. IsLoading = Visibility.Collapsed;
  571. }
  572. #region PDFViewer鼠标滚轮缩放事件
  573. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  574. {
  575. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  576. PDFViewer.Zoom(newZoom);
  577. }
  578. private double CheckZoomLevel(double zoom, bool IsGrowth)
  579. {
  580. double standardZoom = 100;
  581. if (zoom <= 0.01)
  582. {
  583. return 0.01;
  584. }
  585. if (zoom >= 10)
  586. {
  587. return 10;
  588. }
  589. zoom *= 100;
  590. for (int i = 0; i < zoomLevel.Length - 1; i++)
  591. {
  592. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  593. {
  594. standardZoom = zoomLevel[i + 1];
  595. break;
  596. }
  597. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  598. {
  599. standardZoom = zoomLevel[i];
  600. break;
  601. }
  602. }
  603. return standardZoom / 100;
  604. }
  605. #endregion PDFViewer鼠标滚轮缩放事件
  606. #region Navigate
  607. public void OnNavigatedTo(NavigationContext navigationContext)
  608. {
  609. if (isOpenFile)
  610. return;
  611. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  612. if (mainVM != null)
  613. {
  614. mainViewModel = mainVM;
  615. }
  616. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  617. if (pdfview != null)
  618. {
  619. PDFViewer = pdfview;
  620. loadFile();
  621. }
  622. isOpenFile = true;
  623. }
  624. public bool IsNavigationTarget(NavigationContext navigationContext)
  625. {
  626. return true;
  627. }
  628. public void OnNavigatedFrom(NavigationContext navigationContext)
  629. {
  630. }
  631. #endregion Navigate
  632. #region 方法
  633. /// <summary>
  634. /// 视图面板 切换分屏模式
  635. /// </summary>
  636. /// <param name="mode"></param>
  637. public void EnterSplitMode(SplitMode mode)
  638. {
  639. if (mode == SplitMode.Single)
  640. {
  641. //单屏时清空分屏区域内容
  642. region.Regions[SplitViewerRegionName].RemoveAll();
  643. }
  644. ///通知UI层更改布局
  645. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  646. }
  647. private void Undo()
  648. {
  649. if (isInPageEdit)
  650. {
  651. //执行页面编辑的Undo
  652. PageEditUndo?.Invoke();
  653. }
  654. else
  655. {
  656. PDFViewer.UndoManager.Undo();
  657. }
  658. }
  659. private void Redo()
  660. {
  661. if (isInPageEdit)
  662. {
  663. //执行页面编辑的Redo
  664. PageEditRedo?.Invoke();
  665. }
  666. else
  667. {
  668. PDFViewer.UndoManager.Redo();
  669. }
  670. }
  671. private void LoadControl()
  672. {
  673. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  674. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  675. {
  676. NavigationParameters parameters = new NavigationParameters();
  677. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  678. parameters.Add(ParameterNames.ViewContentViewModel, this);
  679. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  680. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  681. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  682. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  683. //TODO 根据上一次关闭记录的菜单,选中TabItem
  684. EnterSelectedBar("TabItemAnnotation");
  685. }
  686. ));
  687. }
  688. /// <summary>
  689. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  690. /// </summary>
  691. /// <param name="Content"></param>
  692. /// <param name="annotPropertyPanel"></param>
  693. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  694. {
  695. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  696. {
  697. NavigationParameters parameters = new NavigationParameters();
  698. //传其他参数:文档类,空注释面板;
  699. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  700. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  701. parameters.Add(ParameterNames.ViewContentViewModel, this);
  702. region.RequestNavigate(PropertyRegionName, Content, parameters);
  703. }
  704. ));
  705. }
  706. /// <summary>
  707. /// 将PDFViwer添加到Region
  708. /// </summary>
  709. private void loadFile()
  710. {
  711. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  712. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  713. CanSave = PDFViewer.UndoManager.CanSave;
  714. CanUndo = PDFViewer.UndoManager.CanUndo;
  715. CanRedo = PDFViewer.UndoManager.CanRedo;
  716. region.AddToRegion(ViwerRegionName, PDFViewer);
  717. }
  718. /// <summary>
  719. /// 已有路径文档的保存逻辑
  720. /// </summary>
  721. private bool saveFile()
  722. {
  723. try
  724. {
  725. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  726. return saveAsFile();
  727. //文档已被修复时 提示另存为
  728. if (PDFViewer.Document.HasRepaired)
  729. {
  730. AlertsMessage alertsMessage = new AlertsMessage();
  731. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  732. if (alertsMessage.result == ContentResult.Ok)
  733. return saveAsFile();
  734. else
  735. return false;
  736. }
  737. //文件路径无法存在时
  738. if (!File.Exists(PDFViewer.Document.FilePath))
  739. {
  740. AlertsMessage alertsMessage = new AlertsMessage();
  741. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  742. if (alertsMessage.result == ContentResult.Ok)
  743. return saveAsFile();
  744. else
  745. return false;
  746. }
  747. //只读文件无法写入时,提示另存为
  748. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  749. if (fileInfo.IsReadOnly)
  750. {
  751. AlertsMessage alertsMessage = new AlertsMessage();
  752. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  753. if (alertsMessage.result == ContentResult.Ok)
  754. return saveAsFile();
  755. else
  756. return false;
  757. }
  758. bool result = PDFViewer.Document.WriteToLoadedPath();
  759. if (result)
  760. {
  761. PDFViewer.UndoManager.CanSave = false;
  762. App.Current.Dispatcher.Invoke(() =>
  763. {
  764. //TODO:更新缩略图
  765. //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
  766. //try
  767. //{
  768. // if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  769. // {
  770. // var size = PdfViewer.Document.GetPageSize(0);
  771. // System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  772. // string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  773. // if (File.Exists(folderPath))
  774. // File.Delete(folderPath);
  775. // DirectoryInfo folder = new DirectoryInfo(folderPath);
  776. // if (!folder.Exists)
  777. // folder.Create();
  778. // string imagePath = info.ThumbImgPath;
  779. // if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  780. // {
  781. // string imageName = Guid.NewGuid().ToString();
  782. // imagePath = System.IO.Path.Combine(folderPath, imageName);
  783. // using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  784. // {
  785. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  786. // }
  787. // }
  788. // else
  789. // {
  790. // using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  791. // {
  792. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  793. // }
  794. // }
  795. // info.ThumbImgPath = imagePath;
  796. // SettingHelper.SetFileInfo(info);
  797. // }
  798. //}
  799. //catch
  800. //{
  801. // info.ThumbImgPath = null;
  802. // SettingHelper.SetFileInfo(info);
  803. //}
  804. });
  805. }
  806. else
  807. {
  808. //文件被占用 保存失败时
  809. AlertsMessage alertsMessage = new AlertsMessage();
  810. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  811. if (alertsMessage.result == ContentResult.Ok)
  812. return saveAsFile();
  813. else
  814. return false;
  815. }
  816. return result;
  817. }
  818. catch { return false; }
  819. }
  820. /// <summary>
  821. /// 另存为或新文档保存逻辑
  822. /// </summary>
  823. public bool saveAsFile(bool isFromRedaction = false)
  824. {
  825. var dlg = new SaveFileDialog();
  826. dlg.Filter = Properties.Resources.OpenDialogFilter;
  827. dlg.FileName = PDFViewer.Document.FileName;
  828. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  829. {
  830. bool result = false;
  831. if (isFromRedaction)
  832. {
  833. //应用标记密文并另存为保存
  834. try
  835. {
  836. PDFViewer.Document.ApplyRedaction();
  837. PDFViewer.Document.ReleasePages();
  838. PDFViewer.ReloadDocument();
  839. }
  840. catch { }
  841. }
  842. if (App.OpenedFileList.Contains(dlg.FileName))
  843. {
  844. //提示文件已经被打开
  845. }
  846. else
  847. {
  848. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  849. if (result)
  850. {
  851. DoAfterSaveAs(dlg.FileName);
  852. }
  853. else
  854. {
  855. //提示文件被其他软件占用 无法保存
  856. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  857. }
  858. ;
  859. }
  860. return result;
  861. }
  862. else
  863. return false;
  864. }
  865. /// <summary>
  866. /// 另存为后进行的操作
  867. /// 重新打开新文档
  868. /// </summary>
  869. /// <param name="targetPath"></param>
  870. public void DoAfterSaveAs(string targetPath)
  871. {
  872. App.OpenedFileList.Remove(targetPath);
  873. string oldFilePath = targetPath;
  874. PDFViewer.UndoManager.CanSave = false;
  875. mainViewModel.OpenFile(targetPath);
  876. //TODO:通知各模块更新PDFview对象
  877. //var result = OpenFile(targetPath, true);
  878. //if (result)
  879. //{
  880. // FileChanged.Invoke(this, null);
  881. // Zoomer.PdfViewer = PdfViewer;
  882. // PageSelector.PdfViewer = PdfViewer;
  883. // ViewModeSelector.PdfViewer = PdfViewer;
  884. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  885. // if (fileInfo != null)
  886. // {
  887. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  888. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  889. // if (fileInfo.LastFitMode == FitMode.FitFree)
  890. // PdfViewer.Zoom(fileInfo.LastZoom);
  891. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  892. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  893. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  894. // else
  895. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  896. // }
  897. //}
  898. }
  899. /// <summary>
  900. /// 显示前添加内容到Region
  901. /// </summary>
  902. /// <param name="isPageEdit"></param>
  903. private void ShowContent(string currentBar, bool isToolMode = false)
  904. {
  905. GridVisibility = Visibility.Visible;
  906. //显示页面编辑或其他工具
  907. if (currentBar == "TabItemPageEdit" || isToolMode)
  908. {
  909. if (currentBar == "TabItemPageEdit")//进入页面编辑
  910. {
  911. if (GridToolRow != 1)
  912. {
  913. GridToolRow = 1;
  914. }
  915. if (GridToolRowSpan != 3)
  916. {
  917. GridToolRowSpan = 3;
  918. }
  919. }
  920. else//进入水印等其他工具模式
  921. {
  922. GridVisibility = Visibility.Collapsed;
  923. if (GridToolRow != 0)
  924. {
  925. GridToolRow = 0;
  926. }
  927. if (GridToolRowSpan != 4)
  928. {
  929. GridToolRowSpan = 4;
  930. }
  931. }
  932. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  933. UpdateShowContent(currentBar);
  934. }
  935. else
  936. {
  937. if (GridToolRow != 1)
  938. {
  939. GridToolRow = 1;
  940. }
  941. UpdateShowContent(currentBar);
  942. }
  943. }
  944. /// <summary>
  945. /// 从二级工具栏进入需要修改界面布局的场景
  946. /// </summary>
  947. /// <param name="EditToolName"></param>
  948. private void EnterEditTools(StringWithUnicode EditToolName)
  949. {
  950. EnterSelectedEditTool(EditToolName);
  951. }
  952. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  953. {
  954. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  955. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  956. {
  957. PDFViewer.Document.ReleasePages();
  958. PDFViewer.ReloadDocument();
  959. }
  960. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  961. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  962. {
  963. region.AddToRegion(ViwerRegionName, PDFViewer);
  964. }
  965. CurrentBar = "TabItemTool";
  966. EnterSelectedBar(CurrentBar);
  967. }
  968. /// <summary>
  969. /// 二级菜单指定目标处理
  970. /// </summary>
  971. /// <param name="e"></param>
  972. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  973. {
  974. CurrentBar = EditToolName.EditToolsContentName;
  975. EnterToolMode(CurrentBar);
  976. }
  977. /// <summary>
  978. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  979. /// </summary>
  980. /// <param name="targetToolMode">要导航过去的控件名称</param>
  981. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  982. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  983. {
  984. IsLoading = Visibility.Visible;
  985. await Task.Delay(3);
  986. NavigationParameters param = new NavigationParameters();
  987. if (valuePairs == null)
  988. {
  989. param.Add(ParameterNames.PDFViewer, PDFViewer);
  990. param.Add(ParameterNames.ViewContentViewModel, this);
  991. }
  992. else//有传入其他内容的参数时
  993. {
  994. param = valuePairs;
  995. }
  996. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  997. ShowContent(CurrentBar, true);
  998. IsLoading = Visibility.Collapsed;
  999. }
  1000. private void EnterSelectedBar(string currentBar)
  1001. {
  1002. NavigationParameters param = new NavigationParameters();
  1003. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1004. param.Add(ParameterNames.ViewContentViewModel, this);
  1005. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  1006. ShowContent(currentBar);
  1007. }
  1008. /// <summary>
  1009. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  1010. /// </summary>
  1011. public void ExitToolMode()
  1012. {
  1013. ToolContentVisible = Visibility.Collapsed;
  1014. if (isInPageEdit)
  1015. {
  1016. TabSelectedIndex = 0;
  1017. isInPageEdit = false;
  1018. }
  1019. }
  1020. #endregion 方法
  1021. }
  1022. }