ViewContentViewModel.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. using Microsoft.Win32;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using PDF_Office.CustomControl;
  11. using PDF_Office.Model;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.IO;
  15. using ComPDFKitViewer.AnnotEvent;
  16. using PDF_Office.ViewModels.Tools;
  17. using Prism.Events;
  18. using PDF_Office.EventAggregators;
  19. using PDF_Office.Helper;
  20. using ComPDFKit.PDFDocument;
  21. using PDFSettings.Settings;
  22. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  23. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  24. using PDF_Office.Model.Dialog.ToolsDialogs;
  25. using DryIoc;
  26. using PDF_Office.Model.Dialog.ConverterDialogs;
  27. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  28. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  29. using System.Linq;
  30. namespace PDF_Office.ViewModels
  31. {
  32. public class ViewContentViewModel : BindableBase, INavigationAware
  33. {
  34. #region 属性、变量
  35. public OpenFileInfo OpenFileInfo = null;
  36. public CPDFViewer PDFViewer { get; set; }
  37. public MainContentViewModel mainViewModel { get; set; }
  38. public IRegionManager region;
  39. public IDialogService dialogs;
  40. public IEventAggregator events;
  41. public bool IsRightMenuCreateLink { get => isCreateLink; set => isCreateLink = value; }
  42. public string ViwerRegionName { get; set; }
  43. /// <summary>
  44. /// 分屏视图的region名称
  45. /// </summary>
  46. public string SplitViewerRegionName { get; set; }
  47. /// <summary>
  48. /// OCR视图名称
  49. /// </summary>
  50. public string OCRViewerRegionName { get; set; }
  51. public string BOTARegionName { get; set; }
  52. public string PropertyRegionName { get; set; }
  53. public string ToolContentRegionName { get; set; }
  54. public string ToolsBarContentRegionName { get; set; }
  55. /// <summary>
  56. /// 顶部提示的Content
  57. /// </summary>
  58. public string TipContentRegionName { get; set; }
  59. public string LeftTipContentRegionName { get; set; }
  60. public string ReadModeRegionName { get; set; }
  61. public string ConverterBarContentRegionName { get; set; }
  62. public string TextEditContentRegionName { get; set; }
  63. public string BackgroundContentRegionName { get; set; }
  64. public List<Point> FillAndSign = new List<Point>();
  65. /// <summary>
  66. /// 文档的密码
  67. /// </summary>
  68. public string PassWord { get; set; }
  69. private Visibility tipVisible = Visibility.Collapsed;
  70. /// <summary>
  71. /// 顶部提示栏的显示状态
  72. /// </summary>
  73. public Visibility TipVisible
  74. {
  75. get { return tipVisible; }
  76. set
  77. {
  78. SetProperty(ref tipVisible, value);
  79. }
  80. }
  81. private Visibility leftTipVisible = Visibility.Collapsed;
  82. public Visibility LeftTipVisible
  83. {
  84. get { return leftTipVisible; }
  85. set
  86. {
  87. SetProperty(ref leftTipVisible, value);
  88. }
  89. }
  90. private Visibility readModelTip = Visibility.Collapsed;
  91. public Visibility ReadModelTip
  92. {
  93. get { return readModelTip; }
  94. set
  95. {
  96. SetProperty(ref readModelTip, value);
  97. }
  98. }
  99. /// <summary>
  100. /// 底部工具栏 RegionName
  101. /// </summary>
  102. public string BottomToolRegionName { get; set; }
  103. private bool _isInPageEdit = false;
  104. /// <summary>
  105. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  106. /// </summary>
  107. public bool isInPageEdit
  108. {
  109. get { return _isInPageEdit; }
  110. set
  111. {
  112. SetProperty(ref _isInPageEdit, value);
  113. if (!value)
  114. {
  115. CanRedo = PDFViewer.UndoManager.CanRedo;
  116. CanUndo = PDFViewer.UndoManager.CanUndo;
  117. }
  118. }
  119. }
  120. public Action PageEditUndo { get; set; }
  121. public Action PageEditRedo { get; set; }
  122. /// <summary>
  123. ///工具条
  124. ///0:收起
  125. ///40:显示
  126. /// </summary>
  127. private int toolRowHeight = 40;
  128. public int ToolRowHeight
  129. {
  130. get { return toolRowHeight; }
  131. set
  132. {
  133. SetProperty(ref toolRowHeight, value);
  134. }
  135. }
  136. /// <summary>
  137. /// 水印,背景侧边栏宽度
  138. /// 0:收起
  139. /// 260:显示
  140. /// </summary>
  141. private int propertyColumnWidth = 0;
  142. public int PropertyColumnWidth
  143. {
  144. get { return propertyColumnWidth; }
  145. set
  146. {
  147. SetProperty(ref propertyColumnWidth, value);
  148. }
  149. }
  150. private int gridToolRow = 1;
  151. /// <summary>
  152. /// 控制ToolContent的Row
  153. /// </summary>
  154. public int GridToolRow
  155. {
  156. get { return gridToolRow; }
  157. set
  158. {
  159. SetProperty(ref gridToolRow, value);
  160. }
  161. }
  162. private int gridToolRowSpan = 3;
  163. /// <summary>
  164. /// 控制ToolContent的RowSpan
  165. /// </summary>
  166. public int GridToolRowSpan
  167. {
  168. get { return gridToolRowSpan; }
  169. set
  170. {
  171. SetProperty(ref gridToolRowSpan, value);
  172. }
  173. }
  174. private Visibility toolContentVisible = Visibility.Collapsed;
  175. /// <summary>
  176. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  177. /// 留意:显示前需要先注入内容、设置好行和跨行数
  178. /// </summary>
  179. public Visibility ToolContentVisible
  180. {
  181. get { return toolContentVisible; }
  182. set
  183. {
  184. SetProperty(ref toolContentVisible, value);
  185. }
  186. }
  187. private Visibility gridVisibility = Visibility.Visible;
  188. /// <summary>
  189. /// 是否正在加载中
  190. /// </summary>
  191. public Visibility GridVisibility
  192. {
  193. get { return gridVisibility; }
  194. set
  195. {
  196. SetProperty(ref gridVisibility, value);
  197. }
  198. }
  199. private Visibility isLoading = Visibility.Collapsed;
  200. private Visibility ocrContentVisible = Visibility.Collapsed;
  201. public Visibility OCRContentVisible
  202. {
  203. get { return ocrContentVisible; }
  204. set
  205. {
  206. SetProperty(ref ocrContentVisible, value);
  207. }
  208. }
  209. /// <summary>
  210. /// 是否正在加载中
  211. /// </summary>
  212. public Visibility IsLoading
  213. {
  214. get { return isLoading; }
  215. set
  216. {
  217. SetProperty(ref isLoading, value);
  218. }
  219. }
  220. private Visibility converterBarContentVisible = Visibility.Collapsed;
  221. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  222. /// <summary>
  223. /// 控制ToolsBarContent的显示
  224. /// 留意:显示前需要先注入内容、设置好行和跨行数
  225. /// </summary>
  226. public Visibility ConverterBarContentVisible
  227. {
  228. get { return converterBarContentVisible; }
  229. set
  230. {
  231. SetProperty(ref converterBarContentVisible, value);
  232. }
  233. }
  234. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  235. /// <summary>
  236. /// 控制ToolsBarContent的显示
  237. /// 留意:显示前需要先注入内容、设置好行和跨行数
  238. /// </summary>
  239. public Visibility ToolsBarContentVisible
  240. {
  241. get { return toolsBarContentVisible; }
  242. set
  243. {
  244. SetProperty(ref toolsBarContentVisible, value);
  245. }
  246. }
  247. private Visibility textEditToolContentVisible = Visibility.Collapsed;
  248. /// <summary>
  249. /// 控制ToolsBarContent的显示
  250. /// 留意:显示前需要先注入内容、设置好行和跨行数
  251. /// </summary>
  252. public Visibility TextEditToolContentVisible
  253. {
  254. get { return textEditToolContentVisible; }
  255. set
  256. {
  257. SetProperty(ref textEditToolContentVisible, value);
  258. }
  259. }
  260. private bool isPorpertyOpen = false;
  261. /// <summary>
  262. /// 属性栏是否展开
  263. /// </summary>
  264. public bool IsPropertyOpen
  265. {
  266. get { return isPorpertyOpen; }
  267. set
  268. {
  269. SetProperty(ref isPorpertyOpen, value);
  270. }
  271. }
  272. private Visibility isReadMode = Visibility.Visible;
  273. /// <summary>
  274. ///是否为阅读模式
  275. /// </summary>
  276. public Visibility IsReadMode
  277. {
  278. get { return isReadMode; }
  279. set
  280. {
  281. SetProperty(ref isReadMode, value);
  282. }
  283. }
  284. private bool canSave;
  285. /// <summary>
  286. /// 是否可以保存
  287. /// </summary>
  288. public bool CanSave
  289. {
  290. get { return canSave; }
  291. set
  292. {
  293. SetProperty(ref canSave, value);
  294. }
  295. }
  296. private bool canUndo;
  297. /// <summary>
  298. /// 是否可以进行Undo
  299. /// </summary>
  300. public bool CanUndo
  301. {
  302. get { return canUndo; }
  303. set
  304. {
  305. SetProperty(ref canUndo, value);
  306. }
  307. }
  308. private bool canRedo;
  309. /// <summary>
  310. /// 是否可以进行Redo
  311. /// </summary>
  312. public bool CanRedo
  313. {
  314. get { return canRedo; }
  315. set
  316. {
  317. SetProperty(ref canRedo, value);
  318. }
  319. }
  320. private GridLength botaWidth = new GridLength(48);
  321. /// <summary>
  322. /// BOTA栏的宽度
  323. /// </summary>
  324. public GridLength BOTAWidth
  325. {
  326. get { return botaWidth; }
  327. set
  328. {
  329. SetProperty(ref botaWidth, value);
  330. if (botaWidth.Value <= 48)
  331. {
  332. OpenBOTA = false;
  333. }
  334. }
  335. }
  336. private int selectedIndex;
  337. /// <summary>
  338. /// 工具栏选中项的索引
  339. /// </summary>
  340. public int TabSelectedIndex
  341. {
  342. get { return selectedIndex; }
  343. set
  344. {
  345. SetProperty(ref selectedIndex, value);
  346. }
  347. }
  348. private bool openBOTA = false;
  349. /// <summary>
  350. /// 是否展开BOTA
  351. /// </summary>
  352. public bool OpenBOTA
  353. {
  354. get { return openBOTA; }
  355. set
  356. {
  357. SetProperty(ref openBOTA, value);
  358. if (openBOTA && BOTAWidth.Value <= 48)
  359. {
  360. BOTAWidth = new GridLength(260);
  361. }
  362. //关闭BOTA的逻辑在xaml.cs代码里控制,无法绑定后台
  363. }
  364. }
  365. public SecurityInfo SecurityInfo = new SecurityInfo();
  366. private Dictionary<string, string> regionNameByTabItem;
  367. private Dictionary<string, string> barContentByTabItem;
  368. private string previousBar = "";
  369. public string CurrentBar = "";
  370. public string unicode = null;
  371. /// <summary>
  372. /// 用来避免重复触发导航事件的标志符
  373. /// </summary>
  374. private bool isOpenFile = false;
  375. /// <summary>
  376. /// 鼠标滚轮缩放的缩放值
  377. /// </summary>
  378. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  379. /// <summary>
  380. /// 注释-链接,提示语
  381. /// </summary>
  382. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  383. public Visibility LinkAnnotTipVisibility
  384. {
  385. get { return linkAnnotTipVisibility; }
  386. set
  387. {
  388. SetProperty(ref linkAnnotTipVisibility, value);
  389. }
  390. }
  391. 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";
  392. private bool isCreateLink = false;
  393. public string LinkAnnotTipText
  394. {
  395. get { return linkAnnotTipText; }
  396. set
  397. {
  398. SetProperty(ref linkAnnotTipText, value);
  399. }
  400. }
  401. #endregion 属性、变量
  402. #region 命令
  403. public DelegateCommand LoadFile { get; set; }
  404. public DelegateCommand Load { get; set; }
  405. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  406. public DelegateCommand SaveFile { get; set; }
  407. public DelegateCommand SaveAsFile { get; set; }
  408. public DelegateCommand SaveAsFlattenCommand { get; set; }
  409. public DelegateCommand UndoCommand { get; set; }
  410. public DelegateCommand RedoCommand { get; set; }
  411. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  412. public DelegateCommand PrintCommand { get; set; }
  413. public DelegateCommand SettingsCommand { get; set; }
  414. public DelegateCommand ShareCommand { get; set; }
  415. public DelegateCommand<object> CreateLinkCommand { get; set; }
  416. public DelegateCommand PropertyCommand { get; set; }
  417. public DelegateCommand ShowInFolderCommand { get; set; }
  418. public DelegateCommand CompressCommand { get; set; }
  419. public DelegateCommand MergeFileCommand { get; set; }
  420. public DelegateCommand EncryptCommand { get; set; }
  421. public DelegateCommand DecryptCommand { get; set; }
  422. public DelegateCommand<string> ConvertCommand { get; set; }
  423. public DelegateCommand CloseWindowCommand { get; set; }
  424. public DelegateCommand OpenFileCommand { get; set; }
  425. #endregion 命令
  426. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  427. {
  428. region = regionManager;
  429. dialogs = dialogService;
  430. events = eventAggregator;
  431. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  432. LoadFile = new DelegateCommand(loadFile);
  433. Load = new DelegateCommand(LoadControl);
  434. SaveFile = new DelegateCommand(() => { saveFile(); }, CanSaveExcute).ObservesProperty(() => CanSave);
  435. SaveAsFlattenCommand = new DelegateCommand(saveAsFlatten);
  436. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  437. UndoCommand = new DelegateCommand(Undo);
  438. RedoCommand = new DelegateCommand(Redo);
  439. ShareCommand = new DelegateCommand(share);
  440. PropertyCommand = new DelegateCommand(property);
  441. ShowInFolderCommand = new DelegateCommand(ShowInFolder);
  442. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  443. CompressCommand = new DelegateCommand(compress);
  444. MergeFileCommand = new DelegateCommand(mergeFile);
  445. EncryptCommand = new DelegateCommand(encrypt);
  446. DecryptCommand = new DelegateCommand(decrypt);
  447. ConvertCommand = new DelegateCommand<string>(convert);
  448. CloseWindowCommand = new DelegateCommand(closeWindow);
  449. OpenFileCommand = new DelegateCommand(openfile);
  450. ViwerRegionName = RegionNames.ViwerRegionName;
  451. SplitViewerRegionName = RegionNames.Viewer_SplitRegionName;
  452. BOTARegionName = RegionNames.BOTARegionName;
  453. PropertyRegionName = RegionNames.PropertyRegionName;
  454. BottomToolRegionName = RegionNames.BottomToolRegionName;
  455. ReadModeRegionName = RegionNames.ReadModeRegionName;
  456. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  457. PrintCommand = new DelegateCommand(ShowPrintDialog);
  458. SettingsCommand = new DelegateCommand(SettingsEvent);
  459. CreateLinkCommand = new DelegateCommand<object>(CreateLinkEvent);
  460. //未显示时无法注册上Region名称
  461. ToolContentVisible = Visibility.Visible;
  462. ToolsBarContentVisible = Visibility.Visible;
  463. OCRContentVisible = Visibility.Visible;
  464. TipVisible = Visibility.Visible;
  465. TipContentRegionName = RegionNames.TipContentRegionName;
  466. LeftTipContentRegionName = RegionNames.LeftTipContentRegionName;
  467. OCRViewerRegionName = RegionNames.OCRViewerRegionName;
  468. ToolContentRegionName = Guid.NewGuid().ToString();
  469. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  470. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  471. TextEditContentRegionName = Guid.NewGuid().ToString();
  472. ToolContentVisible = Visibility.Collapsed;
  473. ToolsBarContentVisible = Visibility.Collapsed;
  474. OCRContentVisible = Visibility.Collapsed;
  475. TipVisible = Visibility.Collapsed;
  476. regionNameByTabItem = new Dictionary<string, string>();
  477. barContentByTabItem = new Dictionary<string, string>();
  478. InitialregionNameByTabItem(ref regionNameByTabItem);
  479. InitialbarContentByTabItem(ref barContentByTabItem);
  480. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  481. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  482. eventAggregator.GetEvent<ShowTipEvent>().Subscribe(ShowSelectedTip, e => e.Unicode == unicode);
  483. //TODO:根据缓存 选择用户上次选择的菜单
  484. EnterSelectedBar("TabItemAnnotation");
  485. }
  486. private void openfile()
  487. {
  488. OpenFileDialog openFileDialog = new OpenFileDialog();
  489. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  490. openFileDialog.Multiselect = true;
  491. if ((bool)openFileDialog.ShowDialog())
  492. {
  493. if (openFileDialog.FileNames.Count() == 1)
  494. {
  495. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  496. {
  497. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  498. }
  499. else
  500. {
  501. App.mainWindowViewModel.AddTabItem(openFileDialog.FileName);
  502. }
  503. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  504. }
  505. else
  506. {
  507. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  508. if (fileList.Count <= 0)
  509. {
  510. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  511. return;
  512. }
  513. mainViewModel.OpenFile(fileList[0]);
  514. for (int i = 1; i < fileList.Count(); i++)
  515. {
  516. if (!App.OpenedFileList.Contains(fileList[i]))
  517. {
  518. App.mainWindowViewModel.AddTabItem(fileList[i]);
  519. }
  520. ToolMethod.SetFileThumbImg(fileList[i]);
  521. }
  522. }
  523. }
  524. }
  525. /// <summary>
  526. /// 关闭当前窗体
  527. /// </summary>
  528. private void closeWindow()
  529. {
  530. App.Current.MainWindow.Close();
  531. }
  532. /// <summary>
  533. /// 转档
  534. /// </summary>
  535. /// <param name="obj"></param>
  536. private void convert(string obj)
  537. {
  538. if (!string.IsNullOrEmpty(obj))
  539. {
  540. DialogParameters value = new DialogParameters();
  541. value.Add(ParameterNames.PDFViewer, PDFViewer);
  542. switch (obj)
  543. {
  544. case "Word":
  545. dialogs.ShowDialog(DialogNames.ConverterWordDialog, value, e =>
  546. {
  547. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  548. converterDialogsModel.OnOpened((DialogResult)e);
  549. });
  550. break;
  551. case "Excel":
  552. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, value, e =>
  553. {
  554. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  555. converterDialogsModel.OnOpened((DialogResult)e);
  556. });
  557. break;
  558. case "PPT":
  559. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, value, e =>
  560. {
  561. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  562. converterDialogsModel.OnOpened((DialogResult)e);
  563. });
  564. break;
  565. case "RTF":
  566. dialogs.ShowDialog(DialogNames.ConverterRTFDialog, value, e =>
  567. {
  568. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  569. converterDialogsModel.OnOpened((DialogResult)e);
  570. });
  571. break;
  572. case "CSV":
  573. dialogs.ShowDialog(DialogNames.ConverterCSVDialog, value, e =>
  574. {
  575. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  576. converterDialogsModel.OnOpened((DialogResult)e);
  577. });
  578. break;
  579. case "HTML":
  580. dialogs.ShowDialog(DialogNames.ConverterHTMLDialog, value, e =>
  581. {
  582. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  583. converterDialogsModel.OnOpened((DialogResult)e);
  584. });
  585. break;
  586. case "Text":
  587. dialogs.ShowDialog(DialogNames.ConverterTextDialog, value, e =>
  588. {
  589. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  590. converterDialogsModel.OnOpened((DialogResult)e);
  591. });
  592. break;
  593. case "Image":
  594. dialogs.ShowDialog(DialogNames.ConverterImgDialog, value, e =>
  595. {
  596. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  597. converterDialogsModel.OnOpened((DialogResult)e);
  598. });
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. }
  605. /// <summary>
  606. /// 解密
  607. /// </summary>
  608. private void decrypt()
  609. {
  610. if (!PDFViewer.Document.IsEncrypted)
  611. {
  612. MessageBoxEx.Show("No security settings available ");
  613. }
  614. else
  615. {
  616. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  617. if (result.IsDiscryptied)
  618. {
  619. if (result.Password != null)
  620. {
  621. string filePath = PDFViewer.Document.FilePath;
  622. PDFViewer.Document.Release();
  623. PDFViewer.InitDocument(filePath);
  624. PDFViewer.Document.UnlockWithPassword(result.Password);
  625. }
  626. DialogParameters value = new DialogParameters();
  627. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  628. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  629. }
  630. }
  631. }
  632. /// <summary>
  633. /// 加密
  634. /// </summary>
  635. private void encrypt()
  636. {
  637. if (!this.SecurityInfo.IsPasswordChanged)
  638. {
  639. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  640. if (result.IsDiscryptied)
  641. {
  642. if (result.Password != null)
  643. {
  644. string filePath = PDFViewer.Document.FilePath;
  645. PDFViewer.Document.Release();
  646. PDFViewer.InitDocument(filePath);
  647. PDFViewer.Document.UnlockWithPassword(result.Password);
  648. }
  649. }
  650. }
  651. DialogParameters value = new DialogParameters();
  652. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  653. value.Add(ParameterNames.ViewContentViewModel, this);
  654. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  655. {
  656. if (e.Result == ButtonResult.OK)
  657. {
  658. SecurityHelper.IsPasswordChanged = true;
  659. if (this.SecurityInfo.IsPasswordChanged)
  660. {
  661. this.PDFViewer.UndoManager.CanSave = true;
  662. }
  663. this.events.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  664. }
  665. });
  666. }
  667. /// <summary>
  668. /// 合并
  669. /// </summary>
  670. private void mergeFile()
  671. {
  672. DialogParameters value = new DialogParameters();
  673. value.Add(ParameterNames.PDFViewer, PDFViewer);
  674. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  675. }
  676. /// <summary>
  677. /// 压缩
  678. /// </summary>
  679. private void compress()
  680. {
  681. DialogParameters value = new DialogParameters();
  682. value.Add(ParameterNames.PDFViewer, PDFViewer);
  683. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  684. {
  685. CompressDialogModel compressDialogModel = new CompressDialogModel();
  686. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  687. });
  688. }
  689. /// <summary>
  690. /// 设置Tip状态栏的显示状态
  691. /// </summary>
  692. /// <param name="show">是否显示</param>
  693. public void ShowTip(bool show)
  694. {
  695. if (show)
  696. {
  697. TipVisible = Visibility.Visible;
  698. }
  699. else
  700. {
  701. TipVisible = Visibility.Collapsed;
  702. }
  703. }
  704. /// <summary>
  705. /// 设置LeftTip状态栏的显示状态
  706. /// </summary>
  707. /// <param name="show">是否显示</param>
  708. public void ShowLeftTip(bool show)
  709. {
  710. if (show)
  711. {
  712. LeftTipVisible = Visibility.Visible;
  713. }
  714. else
  715. {
  716. LeftTipVisible = Visibility.Collapsed;
  717. }
  718. }
  719. public void CheckHaveAllPermission()
  720. {
  721. if (!SecurityHelper.CheckHaveAllPermissions(PDFViewer.Document))
  722. {
  723. ShowLeftTip(true);
  724. NavigationParameters param = new NavigationParameters();
  725. param.Add(ParameterNames.PDFViewer, PDFViewer);
  726. region.RequestNavigate(LeftTipContentRegionName, "FileRestrictedTip", param);
  727. }
  728. }
  729. public void ShowSelectedTip(ShowTipEventArgs showTipEventArgs)
  730. {
  731. switch (showTipEventArgs.enumTipKind)
  732. {
  733. case EnumTipKind.StatusNone:
  734. ShowLeftTip(false);
  735. break;
  736. case EnumTipKind.StatusSetPasswordSuccessfully:
  737. ShowLeftTip(true);
  738. region.RequestNavigate(LeftTipContentRegionName, "SetPasswordSuccessfullyTip");
  739. break;
  740. default: break;
  741. }
  742. }
  743. /// <summary>
  744. /// 右键菜单创建链接
  745. /// </summary>
  746. /// <param name="obj"></param>
  747. private void CreateLinkEvent(object obj)
  748. {
  749. if (obj is object[] objArry)
  750. {
  751. if (objArry[0] is AnnotToolContentViewModel toolContentViewModel && objArry[1] is AnnotCommandArgs annotCommand)
  752. {
  753. LinkAnnotArgs linkArgs = new LinkAnnotArgs();
  754. linkArgs.URI = string.Empty;
  755. linkArgs.LinkType = LINK_TYPE.GOTO;
  756. linkArgs.PageIndex = annotCommand.PageIndex;
  757. //linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  758. this.IsRightMenuCreateLink = true;
  759. List<AnnotHandlerEventArgs> lists = new List<AnnotHandlerEventArgs>();
  760. lists.Add(linkArgs);
  761. AnnotHandlerEventArgs annotArgs = toolContentViewModel.GetLink(lists);
  762. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  763. PDFViewer.SetToolParam(annotArgs);
  764. this.IsPropertyOpen = true;
  765. }
  766. }
  767. }
  768. /// <summary>
  769. /// 在文件资源管理器中显示
  770. /// </summary>
  771. private void ShowInFolder()
  772. {
  773. CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
  774. }
  775. private void property()
  776. {
  777. DialogParameters valuePairs = new DialogParameters();
  778. valuePairs.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  779. dialogs.ShowDialog(DialogNames.PropertyDialog, valuePairs, null);
  780. }
  781. private void share()
  782. {
  783. try
  784. {
  785. var path = PDFViewer.Document.FilePath;
  786. string subject = "分享至" + " " + PDFViewer.Document.FileName;
  787. System.Diagnostics.Process.Start("outlook", "/a,\"" + path + "\"" + "/m \"" + "&subject=" + subject + "\"");
  788. }
  789. catch
  790. {
  791. AlertsMessage alertsMessage = new AlertsMessage();
  792. alertsMessage.ShowDialog("", "未检测到Ooutlook软件,请先安装Outlook", "OK");
  793. }
  794. }
  795. private void SettingsEvent()
  796. {
  797. dialogs.ShowDialog(DialogNames.SettingsDialog, null, null);
  798. }
  799. private bool CanSaveExcute()
  800. {
  801. return CanSave;
  802. }
  803. private void MenuEnterReadModeEvent(object obj)
  804. {
  805. }
  806. public void ShowPrintDialog()
  807. {
  808. DialogParameters printValue = new DialogParameters();
  809. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  810. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  811. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  812. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  813. }
  814. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  815. {
  816. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  817. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  818. //其他工具菜单栏共用一个ToolsBarContentRegionName
  819. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  820. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  821. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  822. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  823. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  824. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  825. }
  826. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  827. {
  828. dictionary.Add("TabItemPageEdit", "PageEditContent");
  829. dictionary.Add("TabItemTool", "ToolsBarContent");
  830. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  831. dictionary.Add("TabItemConvert", "ConverterBarContent");
  832. dictionary.Add("TabItemScan", "ScanContent");
  833. dictionary.Add("TabItemEdit", "TextEditToolContent");
  834. dictionary.Add("TabItemForm", "FormsToolContent");
  835. dictionary.Add("TabItemFill", "FillAndSignContent");
  836. }
  837. private void UpdateShowContent(string currentBar)
  838. {
  839. ToolContentVisible = Visibility.Collapsed;
  840. ToolsBarContentVisible = Visibility.Collapsed;
  841. ConverterBarContentVisible = Visibility.Collapsed;
  842. TextEditToolContentVisible = Visibility.Collapsed;
  843. OCRContentVisible = Visibility.Collapsed;
  844. switch (currentBar)
  845. {
  846. case "TabItemAnnotation":
  847. case "TabItemTool":
  848. case "TabItemForm":
  849. case "TabItemFill":
  850. ToolsBarContentVisible = Visibility.Visible;
  851. break;
  852. case "TabItemScan":
  853. ToolsBarContentVisible = Visibility.Visible;
  854. OCRContentVisible = Visibility.Visible;
  855. break;
  856. case "TabItemEdit":
  857. TextEditToolContentVisible = Visibility.Visible;
  858. break;
  859. case "TabItemConvert":
  860. ConverterBarContentVisible = Visibility.Visible;
  861. break;
  862. case "TabItemPageEdit":
  863. case "HeaderFooterContent":
  864. case "BatesContent":
  865. case "WatermarkContent":
  866. case "BackgroundContent":
  867. case "RedactionContent":
  868. ToolContentVisible = Visibility.Visible;
  869. break;
  870. default:
  871. break;
  872. }
  873. PDFEditMode(currentBar);
  874. FormMode(currentBar);
  875. }
  876. private void PDFEditMode(string currentBar)
  877. {
  878. if (currentBar == "TabItemEdit")
  879. {
  880. if (PDFViewer != null)
  881. {
  882. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  883. //SelectedPrpoertyPanel("TextEditProperty", null);
  884. //IsPropertyOpen = true;
  885. }
  886. }
  887. else
  888. {
  889. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  890. {
  891. IsPropertyOpen = false;
  892. PDFViewer.SetMouseMode(MouseModes.PanTool);
  893. }
  894. }
  895. }
  896. private void FormMode(string currentBar)
  897. {
  898. if (currentBar == "TabItemForm")
  899. {
  900. if (PDFViewer != null)
  901. {
  902. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  903. }
  904. }
  905. else
  906. {
  907. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)
  908. {
  909. PDFViewer.SetMouseMode(MouseModes.PanTool);
  910. }
  911. }
  912. }
  913. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  914. {
  915. if (!isInPageEdit)
  916. {
  917. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  918. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  919. if (e.PropertyName == "CanUndo")
  920. {
  921. CanUndo = PDFViewer.UndoManager.CanUndo;
  922. }
  923. if (e.PropertyName == "CanRedo")
  924. {
  925. CanRedo = PDFViewer.UndoManager.CanRedo;
  926. }
  927. }
  928. if (e.PropertyName == "CanSave")
  929. {
  930. CanSave = PDFViewer.UndoManager.CanSave;
  931. }
  932. }
  933. /// <summary>
  934. /// 选项卡切换事件
  935. /// </summary>
  936. /// <param name="e"></param>
  937. private void TabControlSelectonChangedEvent(object e)
  938. {
  939. var args = e as SelectionChangedEventArgs;
  940. if (args != null)
  941. {
  942. var item = args.AddedItems[0] as TabItem;
  943. CurrentBar = item.Name;
  944. if (previousBar != CurrentBar)
  945. {
  946. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  947. {
  948. EnterToolMode(barContentByTabItem[CurrentBar]);
  949. isInPageEdit = true;
  950. }
  951. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  952. {
  953. EnterSelectedBar(CurrentBar);
  954. isInPageEdit = false;
  955. }
  956. previousBar = CurrentBar;
  957. }
  958. }
  959. }
  960. /// <summary>
  961. /// 阅读模式
  962. /// </summary>
  963. /// <param name="viewContent"></param>
  964. public async void RbtnReadMode()
  965. {
  966. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  967. mainViewModel.IsBookMode = true;
  968. IsLoading = Visibility.Visible;
  969. await Task.Delay(1);
  970. PDFViewer.SetMouseMode(MouseModes.PanTool);
  971. NavigationParameters param = new NavigationParameters();
  972. param.Add(ParameterNames.PDFViewer, PDFViewer);
  973. param.Add(ParameterNames.ViewContentViewModel, this);
  974. region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  975. //ShowContent(CurrentBar, true);
  976. if (GridToolRow != 0)
  977. {
  978. GridToolRow = 0;
  979. }
  980. if (GridToolRowSpan != 4)
  981. {
  982. GridToolRowSpan = 4;
  983. }
  984. //isInPageEdit = true;
  985. UpdateShowContent("TabItemPageEdit");
  986. IsLoading = Visibility.Collapsed;
  987. }
  988. /// <summary>
  989. /// 退出阅读模式
  990. /// </summary>
  991. public async void UnReadModel()
  992. {
  993. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  994. mainViewModel.IsBookMode = false;
  995. IsLoading = Visibility.Visible;
  996. await Task.Delay(1);
  997. //PDFViewer.MouseMode = MouseModes.None;
  998. if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  999. {
  1000. if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1001. {
  1002. var contentRegion = region.Regions[ViwerRegionName];
  1003. contentRegion.Remove(PDFViewer);
  1004. }
  1005. region.AddToRegion(ViwerRegionName, PDFViewer);
  1006. }
  1007. if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  1008. {
  1009. EnterSelectedBar("TabItemAnnotation");
  1010. }
  1011. else
  1012. {
  1013. EnterSelectedBar(CurrentBar);
  1014. }
  1015. bool isExist = false;
  1016. if (region.Regions.ContainsRegionWithName(TipContentRegionName))
  1017. {
  1018. var views = region.Regions[TipContentRegionName].Views;
  1019. foreach (var item in views)
  1020. {
  1021. if (item is Views.TipContent.ReadModelTip readModelTip)
  1022. {
  1023. isExist = true;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. if (isExist == false)
  1029. {
  1030. region.RequestNavigate(TipContentRegionName, "ReadModelTip");
  1031. }
  1032. IsLoading = Visibility.Collapsed;
  1033. ShowTip(true);
  1034. await Task.Delay(3000);
  1035. //ReadModelTip = Visibility.Collapsed;
  1036. TipVisible = Visibility.Collapsed;
  1037. }
  1038. #region PDFViewer鼠标滚轮缩放事件
  1039. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  1040. {
  1041. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  1042. PDFViewer.Zoom(newZoom);
  1043. }
  1044. public double CheckZoomLevel(double zoom, bool IsGrowth)
  1045. {
  1046. double standardZoom = 100;
  1047. if (zoom <= 0.01)
  1048. {
  1049. return 0.01;
  1050. }
  1051. if (zoom >= 10)
  1052. {
  1053. return 10;
  1054. }
  1055. zoom *= 100;
  1056. for (int i = 0; i < zoomLevel.Length - 1; i++)
  1057. {
  1058. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  1059. {
  1060. standardZoom = zoomLevel[i + 1];
  1061. break;
  1062. }
  1063. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  1064. {
  1065. standardZoom = zoomLevel[i];
  1066. break;
  1067. }
  1068. }
  1069. return standardZoom / 100;
  1070. }
  1071. #endregion PDFViewer鼠标滚轮缩放事件
  1072. #region Navigate
  1073. public void OnNavigatedTo(NavigationContext navigationContext)
  1074. {
  1075. if (isOpenFile)
  1076. return;
  1077. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  1078. if (mainVM != null)
  1079. {
  1080. mainViewModel = mainVM;
  1081. mainViewModel.viewContentViewModel = this;
  1082. }
  1083. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1084. if (pdfview != null)
  1085. {
  1086. PDFViewer = pdfview;
  1087. if (pdfview.Tag != null)
  1088. {
  1089. //保存密码
  1090. PassWord = pdfview.Tag.ToString();
  1091. }
  1092. loadFile();
  1093. CheckHaveAllPermission();
  1094. }
  1095. isOpenFile = true;
  1096. }
  1097. public bool IsNavigationTarget(NavigationContext navigationContext)
  1098. {
  1099. if (navigationContext != null)
  1100. {
  1101. //另存为打开导航进来时 不能新建一个实例
  1102. return true;
  1103. }
  1104. else
  1105. {
  1106. //一次性打开多个文件时,需要新建实例
  1107. return false;
  1108. }
  1109. }
  1110. public void OnNavigatedFrom(NavigationContext navigationContext)
  1111. {
  1112. }
  1113. #endregion Navigate
  1114. #region 方法
  1115. /// <summary>
  1116. /// 视图面板 切换分屏模式
  1117. /// </summary>
  1118. /// <param name="mode"></param>
  1119. public void EnterSplitMode(SplitMode mode)
  1120. {
  1121. ///通知UI层更改布局
  1122. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  1123. }
  1124. private void Undo()
  1125. {
  1126. if (isInPageEdit)
  1127. {
  1128. //执行页面编辑的Undo
  1129. PageEditUndo?.Invoke();
  1130. }
  1131. else
  1132. {
  1133. PDFViewer.UndoManager.Undo();
  1134. }
  1135. }
  1136. private void Redo()
  1137. {
  1138. if (isInPageEdit)
  1139. {
  1140. //执行页面编辑的Redo
  1141. PageEditRedo?.Invoke();
  1142. }
  1143. else
  1144. {
  1145. PDFViewer.UndoManager.Redo();
  1146. }
  1147. }
  1148. private void LoadControl()
  1149. {
  1150. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  1151. // 非必要情况不要使用该异步方法,可能会导致一次性加载多个文件时出现因异步引起的regionname 错乱问题
  1152. //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  1153. // {
  1154. NavigationParameters parameters = new NavigationParameters();
  1155. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1156. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1157. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  1158. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  1159. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  1160. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  1161. //TODO 根据上一次关闭记录的菜单,选中TabItem
  1162. EnterSelectedBar("TabItemAnnotation");
  1163. //}
  1164. //));
  1165. //if (App.IsBookMode)
  1166. //{
  1167. // RbtnReadMode();
  1168. //}
  1169. //else
  1170. //{
  1171. // UnReadModel();
  1172. //}
  1173. }
  1174. /// <summary>
  1175. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  1176. /// </summary>
  1177. /// <param name="Content"></param>
  1178. /// <param name="annotPropertyPanel"></param>
  1179. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  1180. {
  1181. NavigationParameters parameters = new NavigationParameters();
  1182. //传其他参数:文档类,空注释面板;
  1183. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1184. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  1185. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1186. region.RequestNavigate(PropertyRegionName, Content, parameters);
  1187. }
  1188. /// <summary>
  1189. /// 将PDFViwer添加到Region
  1190. /// </summary>
  1191. private void loadFile()
  1192. {
  1193. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  1194. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  1195. CanSave = PDFViewer.UndoManager.CanSave;
  1196. CanUndo = PDFViewer.UndoManager.CanUndo;
  1197. CanRedo = PDFViewer.UndoManager.CanRedo;
  1198. region.Regions[ViwerRegionName].RemoveAll();
  1199. //OpenFileInfo 内容赋值给PDFViewer
  1200. GetOpenFileInfo();
  1201. region.AddToRegion(ViwerRegionName, PDFViewer);
  1202. }
  1203. private void GetOpenFileInfo()
  1204. {
  1205. OpenFileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1206. if (OpenFileInfo != null)
  1207. {
  1208. if (OpenFileInfo.LastViewMode != PDFViewer.ModeView)
  1209. {
  1210. PDFViewer.ChangeViewMode(OpenFileInfo.LastViewMode);
  1211. }
  1212. if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
  1213. {
  1214. PDFViewer.ChangeFitMode(OpenFileInfo.LastFitMode);
  1215. }
  1216. if (OpenFileInfo.LastFitMode == ComPDFKitViewer.FitMode.FitFree)
  1217. {
  1218. PDFViewer.Zoom(OpenFileInfo.LastZoom);
  1219. }
  1220. if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
  1221. {
  1222. PDFViewer.GoToPage(OpenFileInfo.LastPageIndex);
  1223. }
  1224. PDFViewer.SetSplitMode(OpenFileInfo.LastSplitMode);
  1225. PDFViewer.SetShowLink(OpenFileInfo.ShowHighLightLink);
  1226. if (OpenFileInfo.LastPageSpace)
  1227. {
  1228. PDFViewer.SetPageSpacing(8);
  1229. }
  1230. else
  1231. {
  1232. PDFViewer.SetPageSpacing(0);
  1233. }
  1234. if (OpenFileInfo.LastDrawMode == ComPDFKitViewer.DrawModes.Draw_Mode_Custom && OpenFileInfo.LastFillColor != 0)
  1235. {
  1236. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode, OpenFileInfo.LastFillColor);
  1237. }
  1238. else
  1239. {
  1240. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode);
  1241. }
  1242. }
  1243. }
  1244. /// <summary>
  1245. /// 已有路径文档的保存逻辑
  1246. /// </summary>
  1247. public bool saveFile()
  1248. {
  1249. try
  1250. {
  1251. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  1252. return saveAsFile();
  1253. //文档已被修复时 提示另存为
  1254. if (PDFViewer.Document.HasRepaired)
  1255. {
  1256. AlertsMessage alertsMessage = new AlertsMessage();
  1257. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  1258. if (alertsMessage.result == ContentResult.Ok)
  1259. return saveAsFile();
  1260. else
  1261. return false;
  1262. }
  1263. //文件路径无法存在时
  1264. if (!File.Exists(PDFViewer.Document.FilePath))
  1265. {
  1266. AlertsMessage alertsMessage = new AlertsMessage();
  1267. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  1268. if (alertsMessage.result == ContentResult.Ok)
  1269. return saveAsFile();
  1270. else
  1271. return false;
  1272. }
  1273. //只读文件无法写入时,提示另存为
  1274. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  1275. if (fileInfo.IsReadOnly)
  1276. {
  1277. AlertsMessage alertsMessage = new AlertsMessage();
  1278. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  1279. if (alertsMessage.result == ContentResult.Ok)
  1280. return saveAsFile();
  1281. else
  1282. return false;
  1283. }
  1284. if (SecurityInfo.IsPasswordChanged)
  1285. {
  1286. string currentFilePath = PDFViewer.Document.FilePath;
  1287. string tempFilePath = PDFViewer.Document.FilePath + ".temp.pdf";
  1288. string openPassword = null;
  1289. string permissionsPassword = null;
  1290. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1291. if (SecurityInfo.NeedOpenPassword)
  1292. {
  1293. openPassword = SecurityInfo.OpenPassword;
  1294. }
  1295. if (SecurityInfo.NeedPermissionsPassword)
  1296. {
  1297. permissionsPassword = SecurityInfo.PermissionsPassword;
  1298. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1299. }
  1300. //方案一
  1301. if (false)
  1302. {
  1303. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1304. AlertsMessage alertsMessage = new AlertsMessage();
  1305. alertsMessage.ShowDialog("", "文件安全设置已修改,需要另存为", "Cancel", "OK");
  1306. if (alertsMessage.result == ContentResult.Ok)
  1307. return saveAsFile();
  1308. else
  1309. return false;
  1310. }
  1311. else if (false)
  1312. {
  1313. //方案二
  1314. try
  1315. {
  1316. string currentPath = PDFViewer.Document.FilePath;
  1317. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1318. //PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1319. PDFViewer.Document.WriteToFilePath(tempPath);
  1320. PDFViewer.CloseDocument();
  1321. PDFViewer.InitDocument(tempPath);
  1322. PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1323. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1324. PDFViewer.Document.WriteToFilePath(currentPath);
  1325. PDFViewer.CloseDocument();
  1326. PDFViewer.InitDocument(currentPath);
  1327. System.IO.File.Delete(tempPath);
  1328. PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1329. CanSave = false;
  1330. return true;
  1331. }
  1332. catch
  1333. {
  1334. return false;
  1335. }
  1336. }
  1337. else if(false)
  1338. {
  1339. //方案三
  1340. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1341. if (PDFViewer.Document.WriteToFilePath(PDFViewer.Document.FilePath))
  1342. {
  1343. DoAfterSaveAs(PDFViewer.Document.FilePath);
  1344. return true;
  1345. }
  1346. else
  1347. {
  1348. return false;
  1349. }
  1350. }
  1351. }
  1352. bool result;
  1353. if (SecurityInfo.IsPasswordChanged)
  1354. {
  1355. string openPassword = null;
  1356. string permissionsPassword = null;
  1357. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1358. if (SecurityInfo.NeedOpenPassword)
  1359. {
  1360. openPassword = SecurityInfo.OpenPassword;
  1361. }
  1362. if (SecurityInfo.NeedPermissionsPassword)
  1363. {
  1364. permissionsPassword = SecurityInfo.PermissionsPassword;
  1365. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1366. }
  1367. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1368. }
  1369. //result = PDFViewer.Document.WriteToLoadedPath();
  1370. result = PDFViewer.Document.WriteToFilePath(PDFViewer.Document.FilePath);
  1371. if (result)
  1372. {
  1373. PDFViewer.UndoManager.CanSave = false;
  1374. App.Current.Dispatcher.Invoke(() =>
  1375. {
  1376. //保存时更新缩略图
  1377. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1378. try
  1379. {
  1380. if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PDFViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  1381. {
  1382. var size = PDFViewer.Document.GetPageSize(0);
  1383. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  1384. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  1385. if (File.Exists(folderPath))
  1386. File.Delete(folderPath);
  1387. DirectoryInfo folder = new DirectoryInfo(folderPath);
  1388. if (!folder.Exists)
  1389. folder.Create();
  1390. string imagePath = info.ThumbImgPath;
  1391. if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  1392. {
  1393. string imageName = Guid.NewGuid().ToString();
  1394. imagePath = System.IO.Path.Combine(folderPath, imageName);
  1395. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  1396. {
  1397. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1398. }
  1399. }
  1400. else
  1401. {
  1402. using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  1403. {
  1404. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1405. }
  1406. }
  1407. info.ThumbImgPath = imagePath;
  1408. SettingHelper.SetFileInfo(info);
  1409. }
  1410. }
  1411. catch
  1412. {
  1413. info.ThumbImgPath = null;
  1414. SettingHelper.SetFileInfo(info);
  1415. }
  1416. });
  1417. PDFViewer.ReloadDocument();
  1418. }
  1419. else
  1420. {
  1421. //文件被占用 保存失败时
  1422. AlertsMessage alertsMessage = new AlertsMessage();
  1423. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  1424. if (alertsMessage.result == ContentResult.Ok)
  1425. return saveAsFile();
  1426. else
  1427. return false;
  1428. }
  1429. return result;
  1430. }
  1431. catch { return false; }
  1432. }
  1433. /// <summary>
  1434. /// 另存为或新文档保存逻辑
  1435. /// </summary>
  1436. public bool saveAsFile(Action RedactionAction = null)
  1437. {
  1438. var dlg = new Microsoft.Win32.SaveFileDialog();
  1439. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1440. dlg.FileName = PDFViewer.Document.FileName;
  1441. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1442. {
  1443. bool result = false;
  1444. if (RedactionAction != null)
  1445. {
  1446. //保存前进行标记密文处理应用或擦除
  1447. RedactionAction.Invoke();
  1448. }
  1449. if (App.OpenedFileList.Contains(dlg.FileName))
  1450. {
  1451. //提示文件已经被打开
  1452. }
  1453. else
  1454. {
  1455. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  1456. if (result)
  1457. {
  1458. DoAfterSaveAs(dlg.FileName);
  1459. }
  1460. else
  1461. {
  1462. //提示文件被其他软件占用 无法保存
  1463. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  1464. }
  1465. ;
  1466. }
  1467. return result;
  1468. }
  1469. else
  1470. return false;
  1471. }
  1472. /// <summary>
  1473. /// 另存为Flatten
  1474. /// </summary>
  1475. private void saveAsFlatten()
  1476. {
  1477. var dlg = new SaveFileDialog();
  1478. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1479. dlg.FileName = PDFViewer.Document.FileName.Substring(0, PDFViewer.Document.FileName.LastIndexOf(".")) + "_Flatten.pdf";
  1480. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1481. {
  1482. saveFile();
  1483. CPDFDocument kmdoc = CPDFDocument.InitWithFilePath(PDFViewer.Document.FileName);
  1484. if (kmdoc == null)
  1485. return;
  1486. bool result = kmdoc.WriteFlattenToFilePath(dlg.FileName);
  1487. if (result)
  1488. System.Diagnostics.Process.Start("explorer", "/select,\"" + dlg.FileName + "\"");
  1489. kmdoc.Release();
  1490. }
  1491. }
  1492. /// <summary>
  1493. /// 另存为后进行的操作
  1494. /// 重新打开新文档
  1495. /// </summary>
  1496. /// <param name="targetPath"></param>
  1497. public void DoAfterSaveAs(string targetPath)
  1498. {
  1499. App.OpenedFileList.Remove(targetPath);
  1500. string oldFilePath = targetPath;
  1501. PDFViewer.UndoManager.CanSave = false;
  1502. mainViewModel.OpenFile(targetPath);
  1503. //TODO:通知各模块更新PDFview对象
  1504. //var result = OpenFile(targetPath, true);
  1505. //if (result)
  1506. //{
  1507. // FileChanged.Invoke(this, null);
  1508. // Zoomer.PdfViewer = PdfViewer;
  1509. // PageSelector.PdfViewer = PdfViewer;
  1510. // ViewModeSelector.PdfViewer = PdfViewer;
  1511. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  1512. // if (fileInfo != null)
  1513. // {
  1514. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  1515. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  1516. // if (fileInfo.LastFitMode == FitMode.FitFree)
  1517. // PdfViewer.Zoom(fileInfo.LastZoom);
  1518. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  1519. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  1520. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  1521. // else
  1522. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  1523. // }
  1524. //}
  1525. }
  1526. /// <summary>
  1527. /// 显示前添加内容到Region
  1528. /// </summary>
  1529. /// <param name="isPageEdit"></param>
  1530. private void ShowContent(string currentBar, bool isToolMode = false)
  1531. {
  1532. GridVisibility = Visibility.Visible;
  1533. //显示页面编辑或其他工具
  1534. if (currentBar == "TabItemPageEdit" || isToolMode)
  1535. {
  1536. if (currentBar == "TabItemPageEdit")//进入页面编辑
  1537. {
  1538. if (GridToolRow != 1)
  1539. {
  1540. GridToolRow = 1;
  1541. }
  1542. if (GridToolRowSpan != 3)
  1543. {
  1544. GridToolRowSpan = 3;
  1545. }
  1546. }
  1547. else//进入水印等其他工具模式
  1548. {
  1549. GridVisibility = Visibility.Collapsed;
  1550. if (GridToolRow != 0)
  1551. {
  1552. GridToolRow = 0;
  1553. }
  1554. if (GridToolRowSpan != 4)
  1555. {
  1556. GridToolRowSpan = 4;
  1557. }
  1558. }
  1559. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  1560. UpdateShowContent(currentBar);
  1561. }
  1562. else
  1563. {
  1564. if (GridToolRow != 1)
  1565. {
  1566. GridToolRow = 1;
  1567. }
  1568. UpdateShowContent(currentBar);
  1569. }
  1570. }
  1571. /// <summary>
  1572. /// 从二级工具栏进入需要修改界面布局的场景
  1573. /// </summary>
  1574. /// <param name="EditToolName"></param>
  1575. private void EnterEditTools(StringWithUnicode EditToolName)
  1576. {
  1577. EnterSelectedEditTool(EditToolName);
  1578. }
  1579. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  1580. {
  1581. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  1582. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  1583. {
  1584. PDFViewer.Document.ReleasePages();
  1585. PDFViewer.ReloadDocument();
  1586. }
  1587. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  1588. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1589. {
  1590. region.AddToRegion(ViwerRegionName, PDFViewer);
  1591. }
  1592. CurrentBar = "TabItemTool";
  1593. EnterSelectedBar(CurrentBar);
  1594. }
  1595. /// <summary>
  1596. /// 二级菜单指定目标处理
  1597. /// </summary>
  1598. /// <param name="e"></param>
  1599. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  1600. {
  1601. CurrentBar = EditToolName.EditToolsContentName;
  1602. EnterToolMode(CurrentBar);
  1603. }
  1604. /// <summary>
  1605. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  1606. /// </summary>
  1607. /// <param name="targetToolMode">要导航过去的控件名称</param>
  1608. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  1609. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  1610. {
  1611. IsLoading = Visibility.Visible;
  1612. await Task.Delay(3);
  1613. NavigationParameters param = new NavigationParameters();
  1614. if (valuePairs == null)
  1615. {
  1616. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1617. param.Add(ParameterNames.ViewContentViewModel, this);
  1618. }
  1619. else//有传入其他内容的参数时
  1620. {
  1621. param = valuePairs;
  1622. }
  1623. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  1624. ShowContent(CurrentBar, true);
  1625. IsLoading = Visibility.Collapsed;
  1626. }
  1627. private void EnterSelectedBar(string currentBar)
  1628. {
  1629. NavigationParameters param = new NavigationParameters();
  1630. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1631. param.Add(ParameterNames.ViewContentViewModel, this);
  1632. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  1633. if (currentBar == "TabItemScan")
  1634. {
  1635. region.RequestNavigate(RegionNames.OCRViewerRegionName, "ScanViwer", param);
  1636. }
  1637. ShowContent(currentBar);
  1638. }
  1639. /// <summary>
  1640. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  1641. /// </summary>
  1642. public void ExitToolMode()
  1643. {
  1644. ToolContentVisible = Visibility.Collapsed;
  1645. if (isInPageEdit)
  1646. {
  1647. TabSelectedIndex = 0;
  1648. isInPageEdit = false;
  1649. }
  1650. }
  1651. #endregion 方法
  1652. }
  1653. }