ViewContentViewModel.cs 45 KB

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