ViewContentViewModel.cs 70 KB

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