ViewContentViewModel.cs 44 KB

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