ViewContentViewModel.cs 41 KB

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