ViewContentViewModel.cs 63 KB

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