ViewContentViewModel.cs 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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_Master.CustomControl;
  11. using PDF_Master.Model;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.IO;
  15. using ComPDFKitViewer.AnnotEvent;
  16. using PDF_Master.ViewModels.Tools;
  17. using Prism.Events;
  18. using PDF_Master.EventAggregators;
  19. using PDF_Master.Helper;
  20. using ComPDFKit.PDFDocument;
  21. using PDFSettings.Settings;
  22. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  23. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  24. using PDF_Master.Model.Dialog.ToolsDialogs;
  25. using DryIoc;
  26. using PDF_Master.Model.Dialog.ConverterDialogs;
  27. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  28. using PDF_Master.Views.PropertyPanel.AnnotPanel;
  29. using System.Linq;
  30. using System.Text;
  31. using PDF_Master.Views.Tools;
  32. using System.Windows.Input;
  33. using PDF_Master.ViewModels.PropertyPanel.AnnotPanel;
  34. using ImTools;
  35. using PDF_Master.Properties;
  36. using PDF_Master.ViewModels.BOTA;
  37. using PDF_Master.Views.BOTA;
  38. using System.Drawing;
  39. using PDF_Master.ViewModels.Tools.AnnotManager;
  40. using PDF_Master.Model.FillAndSign;
  41. namespace PDF_Master.ViewModels
  42. {
  43. public class ViewContentViewModel : BindableBase, INavigationAware
  44. {
  45. #region 文案
  46. private string T_file;
  47. public string T_File
  48. {
  49. get { return T_file; }
  50. set
  51. {
  52. SetProperty(ref T_file, value);
  53. }
  54. }
  55. private string T_save;
  56. public string T_Save
  57. {
  58. get { return T_save; }
  59. set
  60. {
  61. SetProperty(ref T_save, value);
  62. }
  63. }
  64. private string T_undo;
  65. public string T_Undo
  66. {
  67. get { return T_undo; }
  68. set
  69. {
  70. SetProperty(ref T_undo, value);
  71. }
  72. }
  73. private string T_redo;
  74. public string T_Redo
  75. {
  76. get { return T_redo; }
  77. set
  78. {
  79. SetProperty(ref T_redo, value);
  80. }
  81. }
  82. private string T_annotation;
  83. public string T_Annotation
  84. {
  85. get { return T_annotation; }
  86. set
  87. {
  88. SetProperty(ref T_annotation, value);
  89. }
  90. }
  91. private string T_pageEdit;
  92. public string T_PageEdit
  93. {
  94. get { return T_pageEdit; }
  95. set
  96. {
  97. SetProperty(ref T_pageEdit, value);
  98. }
  99. }
  100. private string T_converter;
  101. public string T_Converter
  102. {
  103. get { return T_converter; }
  104. set
  105. {
  106. SetProperty(ref T_converter, value);
  107. }
  108. }
  109. private string T_fillSign;
  110. public string T_FillSign
  111. {
  112. get { return T_fillSign; }
  113. set
  114. {
  115. SetProperty(ref T_fillSign, value);
  116. }
  117. }
  118. private string T_tools;
  119. public string T_Tools
  120. {
  121. get { return T_tools; }
  122. set
  123. {
  124. SetProperty(ref T_tools, value);
  125. }
  126. }
  127. private string T_print;
  128. public string T_Print
  129. {
  130. get { return T_print; }
  131. set
  132. {
  133. SetProperty(ref T_print, value);
  134. }
  135. }
  136. private string T_share;
  137. public string T_Share
  138. {
  139. get { return T_share; }
  140. set
  141. {
  142. SetProperty(ref T_share, value);
  143. }
  144. }
  145. private string T_properties;
  146. public string T_Properties
  147. {
  148. get { return T_properties; }
  149. set
  150. {
  151. SetProperty(ref T_properties, value);
  152. }
  153. }
  154. private void InitStirng()
  155. {
  156. T_File = App.MainPageLoader.GetString("ViewTopToolbar_File");
  157. T_Save = App.MainPageLoader.GetString("ViewTopToolbar_Save");
  158. T_Undo = App.MainPageLoader.GetString("ViewTopToolbar_Undo");
  159. T_Redo = App.MainPageLoader.GetString("ViewTopToolbar_Redo");
  160. T_Annotation = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  161. T_PageEdit = App.MainPageLoader.GetString("ViewTopToolbar_PageEdit");
  162. T_Converter = App.MainPageLoader.GetString("ViewTopToolbar_Converter");
  163. T_FillSign = App.MainPageLoader.GetString("ViewTopToolbar_Fill&Sign");
  164. T_Tools = App.MainPageLoader.GetString("ViewTopToolbar_Tools");
  165. T_Print = App.MainPageLoader.GetString("ViewTopToolbar_Print");
  166. T_Share = App.MainPageLoader.GetString("ViewTopToolbar_Share");
  167. T_Properties = App.MainPageLoader.GetString("ViewTopToolbar_Properties");
  168. }
  169. #endregion 文案
  170. #region 属性、变量
  171. public OpenFileInfo OpenFileInfo = null;
  172. public CPDFViewer PDFViewer { get; set; }
  173. public MainContentViewModel mainViewModel { get; set; }
  174. public IRegionManager region;
  175. public IDialogService dialogs;
  176. public IEventAggregator events;
  177. public bool IsRightMenuCreateLink { get => isCreateLink; set => isCreateLink = value; }
  178. public string ViwerRegionName { get; set; }
  179. /// <summary>
  180. /// 分屏视图的region名称
  181. /// </summary>
  182. public string SplitViewerRegionName { get; set; }
  183. /// <summary>
  184. /// OCR视图名称
  185. /// </summary>
  186. public string OCRViewerRegionName { get; set; }
  187. public string BOTARegionName { get; set; }
  188. public string PropertyRegionName { get; set; }
  189. public string ToolContentRegionName { get; set; }
  190. public string ToolsBarContentRegionName { get; set; }
  191. /// <summary>
  192. /// 顶部提示的Content
  193. /// </summary>
  194. public string TipContentRegionName { get; set; }
  195. public string LeftTipContentRegionName { get; set; }
  196. //便签弹窗
  197. public string NotePopupRegionName { get; set; }
  198. public string ReadModeRegionName { get; set; }
  199. public string ConverterBarContentRegionName { get; set; }
  200. public string TextEditContentRegionName { get; set; }
  201. public string BackgroundContentRegionName { get; set; }
  202. public PasswordInfo PasswordInfo = new PasswordInfo();
  203. //若point不赋值或从列表中找不到point,系统会提供默认值为(0,0)的新point,所以改用Tuple<int,int>
  204. public List<FillAndSignIsShape> FillAndSign = new List<FillAndSignIsShape>();
  205. public List<Tuple<int, int>> AnnotSignatures = new List<Tuple<int, int>>();
  206. /// <summary>
  207. /// 文档的密码
  208. /// </summary>
  209. public string PassWord { get; set; }
  210. public PasswordInfo passwordInfo = new PasswordInfo();
  211. private Visibility tipVisible = Visibility.Collapsed;
  212. /// <summary>
  213. /// 顶部提示栏的显示状态
  214. /// </summary>
  215. public Visibility TipVisible
  216. {
  217. get { return tipVisible; }
  218. set
  219. {
  220. SetProperty(ref tipVisible, value);
  221. }
  222. }
  223. private Visibility leftTipVisible = Visibility.Collapsed;
  224. public Visibility LeftTipVisible
  225. {
  226. get { return leftTipVisible; }
  227. set
  228. {
  229. SetProperty(ref leftTipVisible, value);
  230. }
  231. }
  232. private Visibility notePopupVisible = Visibility.Collapsed;
  233. public Visibility NotePopupVisible
  234. {
  235. get { return notePopupVisible; }
  236. set
  237. {
  238. SetProperty(ref notePopupVisible, value);
  239. }
  240. }
  241. public bool IsNoteAdd = false;
  242. private double canvasNoteLeft;
  243. public double CanvasNoteLeft
  244. {
  245. get { return canvasNoteLeft; }
  246. set
  247. {
  248. SetProperty(ref canvasNoteLeft, value);
  249. }
  250. }
  251. private double canvasNoteBottom;
  252. public double CanvasNoteBottom
  253. {
  254. get { return canvasNoteBottom; }
  255. set
  256. {
  257. SetProperty(ref canvasNoteBottom, value);
  258. }
  259. }
  260. /// <summary>
  261. /// 底部工具栏 RegionName
  262. /// </summary>
  263. public string BottomToolRegionName { get; set; }
  264. private bool _isInPageEdit = false;
  265. /// <summary>
  266. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  267. /// </summary>
  268. public bool isInPageEdit
  269. {
  270. get { return _isInPageEdit; }
  271. set
  272. {
  273. SetProperty(ref _isInPageEdit, value);
  274. if (!value)
  275. {
  276. CanRedo = PDFViewer.UndoManager.CanRedo;
  277. CanUndo = PDFViewer.UndoManager.CanUndo;
  278. }
  279. }
  280. }
  281. public Action PageEditUndo { get; set; }
  282. public Action PageEditRedo { get; set; }
  283. /// <summary>
  284. ///工具条
  285. ///0:收起
  286. ///40:显示
  287. /// </summary>
  288. private int toolRowHeight = 40;
  289. public int ToolRowHeight
  290. {
  291. get { return toolRowHeight; }
  292. set
  293. {
  294. SetProperty(ref toolRowHeight, value);
  295. }
  296. }
  297. /// <summary>
  298. /// 水印,背景侧边栏宽度
  299. /// 0:收起
  300. /// 260:显示
  301. /// </summary>
  302. private int propertyColumnWidth = 0;
  303. public int PropertyColumnWidth
  304. {
  305. get { return propertyColumnWidth; }
  306. set
  307. {
  308. SetProperty(ref propertyColumnWidth, value);
  309. }
  310. }
  311. private int gridToolRow = 1;
  312. /// <summary>
  313. /// 控制ToolContent的Row
  314. /// </summary>
  315. public int GridToolRow
  316. {
  317. get { return gridToolRow; }
  318. set
  319. {
  320. SetProperty(ref gridToolRow, value);
  321. }
  322. }
  323. private int gridToolRowSpan = 3;
  324. /// <summary>
  325. /// 控制ToolContent的RowSpan
  326. /// </summary>
  327. public int GridToolRowSpan
  328. {
  329. get { return gridToolRowSpan; }
  330. set
  331. {
  332. SetProperty(ref gridToolRowSpan, value);
  333. }
  334. }
  335. private Visibility toolContentVisible = Visibility.Collapsed;
  336. /// <summary>
  337. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  338. /// 留意:显示前需要先注入内容、设置好行和跨行数
  339. /// </summary>
  340. public Visibility ToolContentVisible
  341. {
  342. get { return toolContentVisible; }
  343. set
  344. {
  345. SetProperty(ref toolContentVisible, value);
  346. }
  347. }
  348. private Visibility gridVisibility = Visibility.Visible;
  349. /// <summary>
  350. /// 是否正在加载中
  351. /// </summary>
  352. public Visibility GridVisibility
  353. {
  354. get { return gridVisibility; }
  355. set
  356. {
  357. SetProperty(ref gridVisibility, value);
  358. }
  359. }
  360. private Visibility isLoading = Visibility.Collapsed;
  361. private Visibility ocrContentVisible = Visibility.Collapsed;
  362. public Visibility OCRContentVisible
  363. {
  364. get { return ocrContentVisible; }
  365. set
  366. {
  367. SetProperty(ref ocrContentVisible, value);
  368. }
  369. }
  370. /// <summary>
  371. /// 是否正在加载中
  372. /// </summary>
  373. public Visibility IsLoading
  374. {
  375. get { return isLoading; }
  376. set
  377. {
  378. SetProperty(ref isLoading, value);
  379. }
  380. }
  381. private Visibility converterBarContentVisible = Visibility.Collapsed;
  382. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  383. /// <summary>
  384. /// 控制ToolsBarContent的显示
  385. /// 留意:显示前需要先注入内容、设置好行和跨行数
  386. /// </summary>
  387. public Visibility ConverterBarContentVisible
  388. {
  389. get { return converterBarContentVisible; }
  390. set
  391. {
  392. SetProperty(ref converterBarContentVisible, value);
  393. }
  394. }
  395. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  396. /// <summary>
  397. /// 控制ToolsBarContent的显示
  398. /// 留意:显示前需要先注入内容、设置好行和跨行数
  399. /// </summary>
  400. public Visibility ToolsBarContentVisible
  401. {
  402. get { return toolsBarContentVisible; }
  403. set
  404. {
  405. SetProperty(ref toolsBarContentVisible, value);
  406. }
  407. }
  408. private Visibility textEditToolContentVisible = Visibility.Collapsed;
  409. /// <summary>
  410. /// 控制ToolsBarContent的显示
  411. /// 留意:显示前需要先注入内容、设置好行和跨行数
  412. /// </summary>
  413. public Visibility TextEditToolContentVisible
  414. {
  415. get { return textEditToolContentVisible; }
  416. set
  417. {
  418. SetProperty(ref textEditToolContentVisible, value);
  419. }
  420. }
  421. private bool isPorpertyOpen = false;
  422. /// <summary>
  423. /// 属性栏是否展开
  424. /// </summary>
  425. public bool IsPropertyOpen
  426. {
  427. get { return isPorpertyOpen; }
  428. set
  429. {
  430. //如果设置为手动展开属性面板时,则不响应代码控制的展开
  431. if (Settings.Default.AppProperties.InitialVIew.AutoExpandProperty)
  432. {
  433. if (mainViewModel.IsBookMode)
  434. {
  435. this.events.GetEvent<ReadModePropertyOpenEvent>().Publish(new ReadModePropertyOpenArgs() { Unicode = unicode });
  436. }
  437. SetProperty(ref isPorpertyOpen, value);
  438. }
  439. }
  440. }
  441. private Visibility isReadMode = Visibility.Visible;
  442. /// <summary>
  443. ///是否为阅读模式
  444. /// </summary>
  445. public Visibility IsReadMode
  446. {
  447. get { return isReadMode; }
  448. set
  449. {
  450. SetProperty(ref isReadMode, value);
  451. }
  452. }
  453. private bool canSave;
  454. /// <summary>
  455. /// 是否可以保存
  456. /// </summary>
  457. public bool CanSave
  458. {
  459. get { return canSave; }
  460. set
  461. {
  462. if (events != null && canSave == false)
  463. this.events.GetEvent<FillAndSignEvent>().Publish(new FillAndSigntEventArgs { Action = "Clear", AppUnicode = unicode });
  464. SetProperty(ref canSave, value);
  465. }
  466. }
  467. private bool canUndo;
  468. /// <summary>
  469. /// 是否可以进行Undo
  470. /// </summary>
  471. public bool CanUndo
  472. {
  473. get { return canUndo; }
  474. set
  475. {
  476. SetProperty(ref canUndo, value);
  477. }
  478. }
  479. private bool canRedo;
  480. /// <summary>
  481. /// 是否可以进行Redo
  482. /// </summary>
  483. public bool CanRedo
  484. {
  485. get { return canRedo; }
  486. set
  487. {
  488. SetProperty(ref canRedo, value);
  489. }
  490. }
  491. private GridLength botaWidth = new GridLength(48);
  492. /// <summary>
  493. /// BOTA栏的宽度
  494. /// </summary>
  495. public GridLength BOTAWidth
  496. {
  497. get { return botaWidth; }
  498. set
  499. {
  500. SetProperty(ref botaWidth, value);
  501. if (botaWidth.Value <= 48)
  502. {
  503. OpenBOTA = false;
  504. }
  505. }
  506. }
  507. private int selectedIndex;
  508. /// <summary>
  509. /// 工具栏选中项的索引
  510. /// </summary>
  511. public int TabSelectedIndex
  512. {
  513. get { return selectedIndex; }
  514. set
  515. {
  516. SetProperty(ref selectedIndex, value);
  517. }
  518. }
  519. private bool openBOTA = false;
  520. /// <summary>
  521. /// 是否展开BOTA
  522. /// </summary>
  523. public bool OpenBOTA
  524. {
  525. get { return openBOTA; }
  526. set
  527. {
  528. SetProperty(ref openBOTA, value);
  529. if (openBOTA && BOTAWidth.Value <= 48)
  530. {
  531. BOTAWidth = new GridLength(260);
  532. }
  533. //关闭BOTA的逻辑在xaml.cs代码里控制,无法绑定后台
  534. }
  535. }
  536. public SecurityInfo SecurityInfo = new SecurityInfo();
  537. private Dictionary<string, string> regionNameByTabItem;
  538. private Dictionary<string, string> barContentByTabItem;
  539. private string previousBar = "";
  540. public string CurrentBar = "";
  541. public string unicode = null;
  542. /// <summary>
  543. /// 用来避免重复触发导航事件的标志符
  544. /// </summary>
  545. private bool isOpenFile = false;
  546. /// <summary>
  547. /// 鼠标滚轮缩放的缩放值
  548. /// </summary>
  549. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  550. /// <summary>
  551. /// 注释-链接,提示语
  552. /// </summary>
  553. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  554. public Visibility LinkAnnotTipVisibility
  555. {
  556. get { return linkAnnotTipVisibility; }
  557. set
  558. {
  559. SetProperty(ref linkAnnotTipVisibility, value);
  560. }
  561. }
  562. 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";
  563. private bool isCreateLink = false;
  564. public string LinkAnnotTipText
  565. {
  566. get { return linkAnnotTipText; }
  567. set
  568. {
  569. SetProperty(ref linkAnnotTipText, value);
  570. }
  571. }
  572. #endregion 属性、变量
  573. #region 命令
  574. public DelegateCommand Load { get; set; }
  575. public DelegateCommand MouseDown { get; set; }
  576. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  577. public DelegateCommand SaveFile { get; set; }
  578. public DelegateCommand SaveAsFile { get; set; }
  579. public DelegateCommand SaveAsFlattenCommand { get; set; }
  580. public DelegateCommand UndoCommand { get; set; }
  581. public DelegateCommand RedoCommand { get; set; }
  582. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  583. public DelegateCommand PrintCommand { get; set; }
  584. public DelegateCommand SelectCommand { get; set; }
  585. public DelegateCommand SettingsCommand { get; set; }
  586. public DelegateCommand ShareCommand { get; set; }
  587. public DelegateCommand<object> CreateLinkCommand { get; set; }
  588. public DelegateCommand PropertyCommand { get; set; }
  589. public DelegateCommand ShowInFolderCommand { get; set; }
  590. public DelegateCommand CompressCommand { get; set; }
  591. public DelegateCommand MergeFileCommand { get; set; }
  592. public DelegateCommand EncryptCommand { get; set; }
  593. public DelegateCommand DecryptCommand { get; set; }
  594. public DelegateCommand<string> ConvertCommand { get; set; }
  595. public DelegateCommand CloseWindowCommand { get; set; }
  596. public DelegateCommand OpenFileCommand { get; set; }
  597. public DelegateCommand<string> HelpCommand { get; set; }
  598. public DelegateCommand CreateBlankFileCommand { get; set; }
  599. public DelegateCommand CreateFromFile { get; set; }
  600. public DelegateCommand ClosePropertyCommand { get; set; }
  601. #endregion 命令
  602. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  603. {
  604. InitStirng();
  605. region = regionManager;
  606. dialogs = dialogService;
  607. events = eventAggregator;
  608. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  609. Load = new DelegateCommand(LoadControl);
  610. MouseDown = new DelegateCommand(MouseDownEvent);
  611. SaveFile = new DelegateCommand(() => { saveFile(); }, CanSaveExcute).ObservesProperty(() => CanSave);
  612. SaveAsFlattenCommand = new DelegateCommand(saveAsFlatten);
  613. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  614. UndoCommand = new DelegateCommand(Undo);
  615. RedoCommand = new DelegateCommand(Redo);
  616. ShareCommand = new DelegateCommand(share);
  617. PropertyCommand = new DelegateCommand(property);
  618. ShowInFolderCommand = new DelegateCommand(ShowInFolder);
  619. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  620. CompressCommand = new DelegateCommand(compress);
  621. MergeFileCommand = new DelegateCommand(mergeFile);
  622. EncryptCommand = new DelegateCommand(encrypt);
  623. DecryptCommand = new DelegateCommand(decrypt);
  624. ConvertCommand = new DelegateCommand<string>(convert);
  625. CloseWindowCommand = new DelegateCommand(closeWindow);
  626. OpenFileCommand = new DelegateCommand(openfile);
  627. HelpCommand = new DelegateCommand<string>(help);
  628. CreateBlankFileCommand = new DelegateCommand(createBlankFile);
  629. CreateFromFile = new DelegateCommand(createfromFile);
  630. ViwerRegionName = RegionNames.ViwerRegionName;
  631. SplitViewerRegionName = RegionNames.Viewer_SplitRegionName;
  632. BOTARegionName = RegionNames.BOTARegionName;
  633. PropertyRegionName = RegionNames.PropertyRegionName;
  634. BottomToolRegionName = RegionNames.BottomToolRegionName;
  635. ReadModeRegionName = RegionNames.ReadModeRegionName;
  636. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  637. PrintCommand = new DelegateCommand(ShowPrintDialog);
  638. SelectCommand = new DelegateCommand(SelectClick);
  639. SettingsCommand = new DelegateCommand(SettingsEvent);
  640. CreateLinkCommand = new DelegateCommand<object>(CreateLinkEvent);
  641. ClosePropertyCommand = new DelegateCommand(CloseProperty);
  642. //未显示时无法注册上Region名称
  643. ToolContentVisible = Visibility.Visible;
  644. ToolsBarContentVisible = Visibility.Visible;
  645. OCRContentVisible = Visibility.Visible;
  646. TipVisible = Visibility.Visible;
  647. TipContentRegionName = RegionNames.TipContentRegionName;
  648. LeftTipContentRegionName = RegionNames.LeftTipContentRegionName;
  649. NotePopupRegionName = RegionNames.NotePopupRegionName;
  650. OCRViewerRegionName = RegionNames.OCRViewerRegionName;
  651. ToolContentRegionName = Guid.NewGuid().ToString();
  652. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  653. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  654. TextEditContentRegionName = Guid.NewGuid().ToString();
  655. ToolContentVisible = Visibility.Collapsed;
  656. ToolsBarContentVisible = Visibility.Collapsed;
  657. OCRContentVisible = Visibility.Collapsed;
  658. TipVisible = Visibility.Collapsed;
  659. regionNameByTabItem = new Dictionary<string, string>();
  660. barContentByTabItem = new Dictionary<string, string>();
  661. InitialregionNameByTabItem(ref regionNameByTabItem);
  662. InitialbarContentByTabItem(ref barContentByTabItem);
  663. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  664. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  665. eventAggregator.GetEvent<ShowTipEvent>().Subscribe(ShowSelectedTipAsync, e => e.Unicode == unicode);
  666. //TODO:根据缓存 选择用户上次选择的菜单
  667. EnterSelectedBar("TabItemAnnotation");
  668. }
  669. private void MouseDownEvent()
  670. {
  671. if (NotePopupVisible == Visibility.Visible && IsNoteAdd == false)
  672. {
  673. NotePopupVisible = Visibility.Collapsed;
  674. if (region.Regions.ContainsRegionWithName(NotePopupRegionName))
  675. {
  676. var views = region.Regions[NotePopupRegionName].Views;
  677. foreach (var item in views)
  678. {
  679. if (item is StickyNotePopup sticky)
  680. {
  681. var contentRegion = region.Regions[NotePopupRegionName];
  682. contentRegion.Remove(sticky);
  683. break;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. private async void createfromFile()
  690. {
  691. string txt = Properties.Resources.txtex;
  692. string word = Properties.Resources.wordex;
  693. string ppt = Properties.Resources.pptex;
  694. string excel = Properties.Resources.excelex;
  695. string html = Properties.Resources.htmlex;
  696. string image = Properties.Resources.imageex;
  697. string allfiles = txt + word + excel + ppt + image + html;
  698. OpenFileDialog dialog = new OpenFileDialog();
  699. dialog.Multiselect = true;
  700. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  701. $"Microsoft Office Word({word})|{word}|" +
  702. $"Microsoft Office Excel({excel})|{excel}|" +
  703. $"Microsoft Office PowerPoint({ppt})|{ppt}|" +
  704. $"Txt({txt})|{txt}|" +
  705. $"Picture({image})|{image}|" +
  706. $"Html({html})|{html}");
  707. if ((bool)dialog.ShowDialog())
  708. {
  709. for (int i = 0; i < dialog.FileNames.Length; i++)
  710. {
  711. if (App.OpenedFileList.Contains(dialog.FileNames[i]))
  712. {
  713. App.mainWindowViewModel.SelectItem(dialog.FileNames[i]);
  714. }
  715. else
  716. {
  717. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  718. {
  719. App.mainWindowViewModel.AddTabItem(dialog.FileNames[i]);
  720. }));
  721. await Task.Delay(20);
  722. ToolMethod.SetFileThumbImg(dialog.FileNames[i]);
  723. }
  724. }
  725. }
  726. }
  727. /// <summary>
  728. ///创建空白文档
  729. /// </summary>
  730. private async void createBlankFile()
  731. {
  732. App.mainWindowViewModel.AddTab.Execute();
  733. await Task.Delay(30);
  734. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile();
  735. }
  736. private void help(string args)
  737. {
  738. string url = "";
  739. switch (args)
  740. {
  741. case "Guid":
  742. //打开内嵌文档
  743. if (App.OpenedFileList.Contains(App.GuidPDFPath))
  744. {
  745. //如果已经打开时,则选中内嵌文档
  746. App.mainWindowViewModel.SelectItem(App.GuidPDFPath);
  747. return;
  748. }
  749. if (File.Exists(App.GuidPDFPath))
  750. {
  751. App.mainWindowViewModel.AddTabItem(App.GuidPDFPath);
  752. }
  753. break;
  754. case "Online":
  755. url = @"https://www.pdfreaderpro.com/windows/help";
  756. break;
  757. case "More":
  758. url = @"https://www.pdfreaderpro.com/product?utm_source=MacApp&utm_campaign=ProductLink&utm_medium=PdfProduct";
  759. break;
  760. case "Template":
  761. url = @"https://www.pdfreaderpro.com/templates?utm_source=MacApp&utm_campaign=TemplatesLink&utm_medium=PdfTemplates";
  762. break;
  763. case "Blog":
  764. //TODO:显示订阅电子报弹窗
  765. break;
  766. case "ComPDF":
  767. url = @"https://www.compdf.com/?utm_source=macapp&utm_medium=pdfmac&utm_campaign=compdfkit-promp";
  768. break;
  769. case "FeedBack":
  770. //调用邮箱 发送邮件
  771. StringBuilder sr = new StringBuilder();
  772. sr.AppendFormat("{0} - {1};{2};{3} - {4}", App.Name, App.Version, "Propose a New Feature", ServiceHelper.ComputerInfo, ServiceHelper.SystemInfo);
  773. System.Diagnostics.Process.Start(string.Format("mailto:support@pdfreaderpro.com?subject={0}", sr));
  774. break;
  775. default:
  776. break;
  777. }
  778. if (!string.IsNullOrEmpty(url))
  779. {
  780. //需要跳转网页的情况
  781. System.Diagnostics.Process.Start(url);
  782. }
  783. }
  784. /// <summary>
  785. /// 打开文件,跟首页的打开文件有区别,需要新开一个页签
  786. /// </summary>
  787. private void openfile()
  788. {
  789. OpenFileDialog openFileDialog = new OpenFileDialog();
  790. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  791. openFileDialog.Multiselect = true;
  792. if ((bool)openFileDialog.ShowDialog())
  793. {
  794. if (openFileDialog.FileNames.Count() == 1)
  795. {
  796. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  797. {
  798. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  799. }
  800. else
  801. {
  802. App.mainWindowViewModel.AddTabItem(openFileDialog.FileName);
  803. }
  804. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  805. }
  806. else
  807. {
  808. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  809. if (fileList.Count <= 0)
  810. {
  811. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  812. return;
  813. }
  814. mainViewModel.OpenFile(fileList[0]);
  815. for (int i = 1; i < fileList.Count(); i++)
  816. {
  817. if (!App.OpenedFileList.Contains(fileList[i]))
  818. {
  819. App.mainWindowViewModel.AddTabItem(fileList[i]);
  820. }
  821. ToolMethod.SetFileThumbImg(fileList[i]);
  822. }
  823. }
  824. }
  825. }
  826. /// <summary>
  827. /// 关闭当前窗体
  828. /// </summary>
  829. private void closeWindow()
  830. {
  831. App.Current.MainWindow.Close();
  832. }
  833. /// <summary>
  834. /// 转档
  835. /// </summary>
  836. /// <param name="obj"></param>
  837. private void convert(string obj)
  838. {
  839. if (!string.IsNullOrEmpty(obj))
  840. {
  841. DialogParameters value = new DialogParameters();
  842. value.Add(ParameterNames.PDFViewer, PDFViewer);
  843. switch (obj)
  844. {
  845. case "Word":
  846. dialogs.ShowDialog(DialogNames.ConverterWordDialog, value, e =>
  847. {
  848. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  849. converterDialogsModel.OnOpened((DialogResult)e);
  850. });
  851. break;
  852. case "Excel":
  853. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, value, e =>
  854. {
  855. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  856. converterDialogsModel.OnOpened((DialogResult)e);
  857. });
  858. break;
  859. case "PPT":
  860. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, value, e =>
  861. {
  862. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  863. converterDialogsModel.OnOpened((DialogResult)e);
  864. });
  865. break;
  866. case "RTF":
  867. dialogs.ShowDialog(DialogNames.ConverterRTFDialog, value, e =>
  868. {
  869. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  870. converterDialogsModel.OnOpened((DialogResult)e);
  871. });
  872. break;
  873. case "CSV":
  874. dialogs.ShowDialog(DialogNames.ConverterCSVDialog, value, e =>
  875. {
  876. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  877. converterDialogsModel.OnOpened((DialogResult)e);
  878. });
  879. break;
  880. case "HTML":
  881. dialogs.ShowDialog(DialogNames.ConverterHTMLDialog, value, e =>
  882. {
  883. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  884. converterDialogsModel.OnOpened((DialogResult)e);
  885. });
  886. break;
  887. case "Text":
  888. dialogs.ShowDialog(DialogNames.ConverterTextDialog, value, e =>
  889. {
  890. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  891. converterDialogsModel.OnOpened((DialogResult)e);
  892. });
  893. break;
  894. case "Image":
  895. dialogs.ShowDialog(DialogNames.ConverterImgDialog, value, e =>
  896. {
  897. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  898. converterDialogsModel.OnOpened((DialogResult)e);
  899. });
  900. break;
  901. default:
  902. break;
  903. }
  904. }
  905. }
  906. /// <summary>
  907. /// 解密
  908. /// </summary>
  909. private void decrypt()
  910. {
  911. if (!PDFViewer.Document.IsEncrypted)
  912. {
  913. MessageBoxEx.Show("No security settings available ");
  914. }
  915. else
  916. {
  917. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  918. if (result.IsDiscryptied)
  919. {
  920. if (result.Password != null)
  921. {
  922. string filePath = PDFViewer.Document.FilePath;
  923. PDFViewer.Document.Release();
  924. PDFViewer.InitDocument(filePath);
  925. PDFViewer.Document.UnlockWithPassword(result.Password);
  926. }
  927. DialogParameters value = new DialogParameters();
  928. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  929. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  930. }
  931. }
  932. }
  933. /// <summary>
  934. /// 加密
  935. /// </summary>
  936. private void encrypt()
  937. {
  938. if (!this.SecurityInfo.IsPasswordChanged)
  939. {
  940. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  941. if (result.IsDiscryptied)
  942. {
  943. if (result.Password != null)
  944. {
  945. string filePath = PDFViewer.Document.FilePath;
  946. PDFViewer.Document.Release();
  947. PDFViewer.InitDocument(filePath);
  948. PDFViewer.Document.UnlockWithPassword(result.Password);
  949. }
  950. }
  951. }
  952. DialogParameters value = new DialogParameters();
  953. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  954. value.Add(ParameterNames.ViewContentViewModel, this);
  955. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  956. {
  957. if (e.Result == ButtonResult.OK)
  958. {
  959. SecurityHelper.IsPasswordChanged = true;
  960. if (this.SecurityInfo.IsPasswordChanged)
  961. {
  962. this.PDFViewer.UndoManager.CanSave = true;
  963. }
  964. this.events.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  965. }
  966. });
  967. }
  968. /// <summary>
  969. /// 合并
  970. /// </summary>
  971. private void mergeFile()
  972. {
  973. DialogParameters value = new DialogParameters();
  974. value.Add(ParameterNames.PDFViewer, PDFViewer);
  975. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  976. }
  977. /// <summary>
  978. /// 压缩
  979. /// </summary>
  980. private void compress()
  981. {
  982. DialogParameters value = new DialogParameters();
  983. value.Add(ParameterNames.PDFViewer, PDFViewer);
  984. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  985. {
  986. CompressDialogModel compressDialogModel = new CompressDialogModel();
  987. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  988. });
  989. }
  990. /// <summary>
  991. /// 设置Tip状态栏的显示状态
  992. /// </summary>
  993. /// <param name="show">是否显示</param>
  994. public void ShowTip(bool show)
  995. {
  996. if (show)
  997. {
  998. TipVisible = Visibility.Visible;
  999. }
  1000. else
  1001. {
  1002. TipVisible = Visibility.Collapsed;
  1003. }
  1004. }
  1005. /// <summary>
  1006. /// 设置LeftTip状态栏的显示状态
  1007. /// </summary>
  1008. /// <param name="show">是否显示</param>
  1009. public void ShowLeftTip(bool show)
  1010. {
  1011. if (show)
  1012. {
  1013. LeftTipVisible = Visibility.Visible;
  1014. }
  1015. else
  1016. {
  1017. LeftTipVisible = Visibility.Collapsed;
  1018. }
  1019. }
  1020. public void CheckHaveAllPermission()
  1021. {
  1022. if (!SecurityHelper.CheckHaveAllPermissions(PDFViewer.Document))
  1023. {
  1024. ShowLeftTip(true);
  1025. NavigationParameters param = new NavigationParameters();
  1026. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1027. region.RequestNavigate(LeftTipContentRegionName, "FileRestrictedTip", param);
  1028. }
  1029. }
  1030. public async void ShowSelectedTipAsync(ShowTipEventArgs showTipEventArgs)
  1031. {
  1032. switch (showTipEventArgs.enumTipKind)
  1033. {
  1034. case EnumTipKind.StatusNone:
  1035. ShowLeftTip(false);
  1036. break;
  1037. case EnumTipKind.StatusSetPasswordSuccessfully:
  1038. ShowLeftTip(true);
  1039. region.RequestNavigate(LeftTipContentRegionName, "SetPasswordSuccessfullyTip");
  1040. break;
  1041. case EnumTipKind.StatusRemoveSecuritySuccessfully:
  1042. ShowLeftTip(false);
  1043. region.RequestNavigate(TipContentRegionName, "RemoveSecuritySuccessTip");
  1044. IsLoading = Visibility.Collapsed;
  1045. ShowTip(true);
  1046. await Task.Delay(3000);
  1047. //ReadModelTip = Visibility.Collapsed;
  1048. TipVisible = Visibility.Collapsed;
  1049. break;
  1050. default: break;
  1051. }
  1052. }
  1053. /// <summary>
  1054. /// 右键菜单创建链接
  1055. /// </summary>
  1056. /// <param name="obj"></param>
  1057. private void CreateLinkEvent(object obj)
  1058. {
  1059. if (obj is object[] objArry)
  1060. {
  1061. if (objArry[0] is AnnotToolContentViewModel toolContentViewModel && objArry[1] is AnnotCommandArgs annotCommand)
  1062. {
  1063. LinkAnnotArgs linkArgs = new LinkAnnotArgs();
  1064. linkArgs.URI = string.Empty;
  1065. linkArgs.LinkType = LINK_TYPE.GOTO;
  1066. linkArgs.PageIndex = annotCommand.PageIndex;
  1067. //linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  1068. this.IsRightMenuCreateLink = true;
  1069. List<AnnotHandlerEventArgs> lists = new List<AnnotHandlerEventArgs>();
  1070. lists.Add(linkArgs);
  1071. AnnotHandlerEventArgs annotArgs = toolContentViewModel.GetLink(lists);
  1072. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  1073. PDFViewer.SetToolParam(annotArgs);
  1074. this.IsPropertyOpen = true;
  1075. }
  1076. }
  1077. }
  1078. private void CloseProperty()
  1079. {
  1080. IsPropertyOpen = false;
  1081. }
  1082. /// <summary>
  1083. /// 在文件资源管理器中显示
  1084. /// </summary>
  1085. private void ShowInFolder()
  1086. {
  1087. CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
  1088. }
  1089. private void property()
  1090. {
  1091. DialogParameters valuePairs = new DialogParameters();
  1092. valuePairs.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  1093. dialogs.ShowDialog(DialogNames.PropertyDialog, valuePairs, null);
  1094. }
  1095. private void share()
  1096. {
  1097. try
  1098. {
  1099. var path = PDFViewer.Document.FilePath;
  1100. string subject = "分享至" + " " + PDFViewer.Document.FileName;
  1101. System.Diagnostics.Process.Start("outlook", "/a,\"" + path + "\"" + "/m \"" + "&subject=" + subject + "\"");
  1102. }
  1103. catch
  1104. {
  1105. AlertsMessage alertsMessage = new AlertsMessage();
  1106. alertsMessage.ShowDialog("", "未检测到Ooutlook软件,请先安装Outlook", "OK");
  1107. }
  1108. }
  1109. private void SettingsEvent()
  1110. {
  1111. dialogs.ShowDialog(DialogNames.SettingsDialog, null, null);
  1112. if (PDFViewer != null)
  1113. {
  1114. PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1115. }
  1116. }
  1117. private bool CanSaveExcute()
  1118. {
  1119. return CanSave;
  1120. }
  1121. private void MenuEnterReadModeEvent(object obj)
  1122. {
  1123. }
  1124. public void ShowPrintDialog()
  1125. {
  1126. DialogParameters printValue = new DialogParameters();
  1127. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  1128. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  1129. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  1130. printValue.Add(ParameterNames.Unicode, unicode);
  1131. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  1132. }
  1133. public void SelectClick()
  1134. {
  1135. if (OpenBOTA == false)
  1136. {
  1137. OpenBOTA = true;
  1138. }
  1139. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  1140. if (isBook == false)
  1141. {
  1142. bOTAContent.TabItemSearch.IsSelected = true;
  1143. }
  1144. }
  1145. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  1146. {
  1147. bool isTabItem = false;
  1148. bOTAContentViewModel = null;
  1149. bOTAContent = null;
  1150. if (region.Regions.ContainsRegionWithName(this.BOTARegionName))
  1151. {
  1152. var views = region.Regions[this.BOTARegionName].Views;
  1153. var model = views.FirstOrDefault(q => q is BOTAContent);
  1154. if (model is BOTAContent bOTAContent1)
  1155. {
  1156. bOTAContent = bOTAContent1;
  1157. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  1158. if (bOTAContentViewModel.CurrentBar == tabItemText)
  1159. {
  1160. isTabItem = true;
  1161. }
  1162. }
  1163. }
  1164. return isTabItem;
  1165. }
  1166. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  1167. {
  1168. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  1169. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  1170. //其他工具菜单栏共用一个ToolsBarContentRegionName
  1171. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  1172. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  1173. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  1174. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  1175. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  1176. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  1177. }
  1178. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  1179. {
  1180. dictionary.Add("TabItemPageEdit", "PageEditContent");
  1181. dictionary.Add("TabItemTool", "ToolsBarContent");
  1182. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  1183. dictionary.Add("TabItemConvert", "ConverterBarContent");
  1184. dictionary.Add("TabItemScan", "ScanContent");
  1185. dictionary.Add("TabItemEdit", "TextEditToolContent");
  1186. dictionary.Add("TabItemForm", "FormsToolContent");
  1187. dictionary.Add("TabItemFill", "FillAndSignContent");
  1188. }
  1189. private void UpdateShowContent(string currentBar)
  1190. {
  1191. ToolContentVisible = Visibility.Collapsed;
  1192. ToolsBarContentVisible = Visibility.Collapsed;
  1193. ConverterBarContentVisible = Visibility.Collapsed;
  1194. TextEditToolContentVisible = Visibility.Collapsed;
  1195. OCRContentVisible = Visibility.Collapsed;
  1196. switch (currentBar)
  1197. {
  1198. case "TabItemAnnotation":
  1199. case "TabItemTool":
  1200. case "TabItemForm":
  1201. case "TabItemFill":
  1202. ToolsBarContentVisible = Visibility.Visible;
  1203. break;
  1204. case "TabItemScan":
  1205. ToolsBarContentVisible = Visibility.Visible;
  1206. OCRContentVisible = Visibility.Visible;
  1207. break;
  1208. case "TabItemEdit":
  1209. TextEditToolContentVisible = Visibility.Visible;
  1210. break;
  1211. case "TabItemConvert":
  1212. ConverterBarContentVisible = Visibility.Visible;
  1213. break;
  1214. case "TabItemPageEdit":
  1215. case "HeaderFooterContent":
  1216. case "BatesContent":
  1217. case "WatermarkContent":
  1218. case "BackgroundContent":
  1219. case "RedactionContent":
  1220. ToolContentVisible = Visibility.Visible;
  1221. break;
  1222. default:
  1223. break;
  1224. }
  1225. PDFEditMode(currentBar);
  1226. FormMode(currentBar);
  1227. }
  1228. private void PDFEditMode(string currentBar)
  1229. {
  1230. if (currentBar == "TabItemEdit")
  1231. {
  1232. if (PDFViewer != null)
  1233. {
  1234. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  1235. //SelectedPrpoertyPanel("TextEditProperty", null);
  1236. //IsPropertyOpen = true;
  1237. }
  1238. }
  1239. else
  1240. {
  1241. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  1242. {
  1243. IsPropertyOpen = false;
  1244. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1245. }
  1246. }
  1247. }
  1248. private void FormMode(string currentBar)
  1249. {
  1250. if (currentBar == "TabItemForm")
  1251. {
  1252. if (PDFViewer != null)
  1253. {
  1254. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  1255. }
  1256. }
  1257. else
  1258. {
  1259. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)
  1260. {
  1261. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1262. }
  1263. }
  1264. }
  1265. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  1266. {
  1267. if (!isInPageEdit)
  1268. {
  1269. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  1270. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  1271. if (e.PropertyName == "CanUndo")
  1272. {
  1273. CanUndo = PDFViewer.UndoManager.CanUndo;
  1274. }
  1275. if (e.PropertyName == "CanRedo")
  1276. {
  1277. CanRedo = PDFViewer.UndoManager.CanRedo;
  1278. }
  1279. }
  1280. if (e.PropertyName == "CanSave")
  1281. {
  1282. CanSave = PDFViewer.UndoManager.CanSave;
  1283. }
  1284. }
  1285. /// <summary>
  1286. /// 选项卡切换事件
  1287. /// </summary>
  1288. /// <param name="e"></param>
  1289. private void TabControlSelectonChangedEvent(object e)
  1290. {
  1291. var args = e as SelectionChangedEventArgs;
  1292. if (args != null)
  1293. {
  1294. var item = args.AddedItems[0] as TabItem;
  1295. CurrentBar = item.Name;
  1296. if (previousBar != CurrentBar)
  1297. {
  1298. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  1299. {
  1300. EnterToolMode(barContentByTabItem[CurrentBar]);
  1301. isInPageEdit = true;
  1302. }
  1303. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  1304. {
  1305. EnterSelectedBar(CurrentBar);
  1306. isInPageEdit = false;
  1307. }
  1308. previousBar = CurrentBar;
  1309. }
  1310. }
  1311. }
  1312. /// <summary>
  1313. /// 阅读模式
  1314. /// </summary>
  1315. /// <param name="viewContent"></param>
  1316. public async void On_ReadMode()
  1317. {
  1318. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  1319. mainViewModel.IsBookMode = true;
  1320. IsLoading = Visibility.Visible;
  1321. await Task.Delay(1);
  1322. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1323. #region 旧版本
  1324. //NavigationParameters param = new NavigationParameters();
  1325. //param.Add(ParameterNames.PDFViewer, PDFViewer);
  1326. //param.Add(ParameterNames.ViewContentViewModel, this);
  1327. //region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  1328. ////ShowContent(CurrentBar, true);
  1329. //if (GridToolRow != 0)
  1330. //{
  1331. // GridToolRow = 0;
  1332. //}
  1333. //if (GridToolRowSpan != 4)
  1334. //{
  1335. // GridToolRowSpan = 4;
  1336. //}
  1337. ////isInPageEdit = true;
  1338. //UpdateShowContent("TabItemPageEdit");
  1339. #endregion 旧版本
  1340. this.events.GetEvent<ReadModeEvent>().Publish(new ReadModeArgs() { Unicode = unicode, IsReadMode = true });
  1341. if (region.Regions.ContainsRegionWithName(TipContentRegionName))
  1342. {
  1343. var views = region.Regions[TipContentRegionName].Views;
  1344. var isHasView = views.FindFirst(q => q is Views.TipContent.EnterReadModeTip);
  1345. if (isHasView == null)
  1346. {
  1347. region.RequestNavigate(TipContentRegionName, "EnterReadModeTip");
  1348. }
  1349. else
  1350. {
  1351. region.Regions[TipContentRegionName].Activate(isHasView);
  1352. }
  1353. }
  1354. IsLoading = Visibility.Collapsed;
  1355. ShowTip(true);
  1356. await Task.Delay(3000);
  1357. TipVisible = Visibility.Collapsed;
  1358. }
  1359. /// <summary>
  1360. /// 退出阅读模式
  1361. /// </summary>
  1362. public async void Off_ReadModel()
  1363. {
  1364. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  1365. mainViewModel.IsBookMode = false;
  1366. IsLoading = Visibility.Visible;
  1367. await Task.Delay(1);
  1368. //PDFViewer.MouseMode = MouseModes.None;
  1369. #region 旧版本
  1370. //if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  1371. //{
  1372. // if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1373. // {
  1374. // var contentRegion = region.Regions[ViwerRegionName];
  1375. // contentRegion.Remove(PDFViewer);
  1376. // }
  1377. // //还原背景色
  1378. // PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1379. // region.AddToRegion(ViwerRegionName, PDFViewer);
  1380. //}
  1381. //if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  1382. //{
  1383. // EnterSelectedBar("TabItemAnnotation");
  1384. //}
  1385. //else
  1386. //{
  1387. // EnterSelectedBar(CurrentBar);
  1388. //}
  1389. #endregion 旧版本
  1390. //还原背景色
  1391. PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1392. this.events.GetEvent<ReadModeEvent>().Publish(new ReadModeArgs() { Unicode = unicode, IsReadMode = false });
  1393. if (region.Regions.ContainsRegionWithName(TipContentRegionName))
  1394. {
  1395. var views = region.Regions[TipContentRegionName].Views;
  1396. var isHasView = views.FindFirst(q => q is Views.TipContent.ReadModelTip);
  1397. if (isHasView == null)
  1398. {
  1399. region.RequestNavigate(TipContentRegionName, "ReadModelTip");
  1400. }
  1401. else
  1402. {
  1403. region.Regions[TipContentRegionName].Activate(isHasView);
  1404. }
  1405. }
  1406. IsLoading = Visibility.Collapsed;
  1407. ShowTip(true);
  1408. await Task.Delay(3000);
  1409. //ReadModelTip = Visibility.Collapsed;
  1410. TipVisible = Visibility.Collapsed;
  1411. }
  1412. #region PDFViewer鼠标滚轮缩放事件
  1413. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  1414. {
  1415. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  1416. PDFViewer.Zoom(newZoom);
  1417. }
  1418. public double CheckZoomLevel(double zoom, bool IsGrowth)
  1419. {
  1420. double standardZoom = 100;
  1421. if (zoom <= 0.01)
  1422. {
  1423. return 0.01;
  1424. }
  1425. if (zoom >= 10)
  1426. {
  1427. return 10;
  1428. }
  1429. zoom *= 100;
  1430. for (int i = 0; i < zoomLevel.Length - 1; i++)
  1431. {
  1432. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  1433. {
  1434. standardZoom = zoomLevel[i + 1];
  1435. break;
  1436. }
  1437. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  1438. {
  1439. standardZoom = zoomLevel[i];
  1440. break;
  1441. }
  1442. }
  1443. return standardZoom / 100;
  1444. }
  1445. #endregion PDFViewer鼠标滚轮缩放事件
  1446. #region Navigate
  1447. public void OnNavigatedTo(NavigationContext navigationContext)
  1448. {
  1449. App.mainWindowViewModel.OphVis = Visibility.Collapsed;
  1450. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  1451. if (mainVM != null)
  1452. {
  1453. mainViewModel = mainVM;
  1454. mainViewModel.viewContentViewModel = this;
  1455. }
  1456. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1457. if (pdfview != null)
  1458. {
  1459. PDFViewer = pdfview;
  1460. if (loadFile())
  1461. {
  1462. CheckHaveAllPermission();
  1463. }
  1464. }
  1465. navigationContext.Parameters.TryGetValue(ParameterNames.PasswordInfo, out PasswordInfo);
  1466. //快捷键
  1467. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1468. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  1469. }
  1470. public bool IsNavigationTarget(NavigationContext navigationContext)
  1471. {
  1472. if (navigationContext != null)
  1473. {
  1474. //另存为打开导航进来时 不能新建一个实例
  1475. return true;
  1476. }
  1477. else
  1478. {
  1479. //一次性打开多个文件时,需要新建实例
  1480. return false;
  1481. }
  1482. }
  1483. public void OnNavigatedFrom(NavigationContext navigationContext)
  1484. {
  1485. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1486. }
  1487. #endregion Navigate
  1488. #region 方法
  1489. /// <summary>
  1490. /// 快捷键
  1491. /// </summary>
  1492. /// <param name="sender"></param>
  1493. /// <param name="e"></param>
  1494. /// <exception cref="NotImplementedException"></exception>
  1495. private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1496. {
  1497. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  1498. {
  1499. if (mainViewModel.IsBookMode == true)
  1500. {
  1501. Off_ReadModel();
  1502. }
  1503. //内容选择 退出
  1504. if (PDFViewer.MouseMode != MouseModes.PanTool)
  1505. {
  1506. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1507. if (annotToolContent != null && toolContentViewModel != null)
  1508. {
  1509. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1510. {
  1511. if (toolContentViewModel.SnapshotEditMenuViewModel.SnapToolArgs != null)
  1512. {
  1513. toolContentViewModel.SnapshotEditMenuViewModel.SnapToolArgs.ReSnapshot();
  1514. annotToolContent.BtnSelecttool.IsChecked = false;
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. if (KeyEventsHelper.IsModifierKey(ModifierKeys.Control, Key.C))
  1521. {
  1522. //内容选择 复制 快捷键
  1523. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1524. if (annotToolContent != null && toolContentViewModel != null)
  1525. {
  1526. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1527. {
  1528. toolContentViewModel.SnapshotEditMenuViewModel.SnapCopyCommand.Execute();
  1529. }
  1530. }
  1531. }
  1532. if (KeyEventsHelper.IsModifierKey(ModifierKeys.Control, Key.P))
  1533. {
  1534. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1535. if (annotToolContent != null && toolContentViewModel != null)
  1536. {
  1537. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1538. {
  1539. toolContentViewModel.SnapshotEditMenuViewModel.PrintCommand.Execute();
  1540. }
  1541. }
  1542. }
  1543. }
  1544. private AnnotToolContent GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel)
  1545. {
  1546. AnnotToolContent toolContent = null;
  1547. toolContentViewModel = null;
  1548. if (region.Regions.ContainsRegionWithName(ToolsBarContentRegionName))
  1549. {
  1550. var views = region.Regions[ToolsBarContentRegionName].Views;
  1551. var model = views.FirstOrDefault(q => q is AnnotToolContent);
  1552. if (model is AnnotToolContent annotToolContent)
  1553. {
  1554. toolContent = annotToolContent;
  1555. toolContentViewModel = annotToolContent.DataContext as AnnotToolContentViewModel;
  1556. }
  1557. }
  1558. return toolContent;
  1559. }
  1560. /// <summary>
  1561. /// 视图面板 切换分屏模式
  1562. /// </summary>
  1563. /// <param name="mode"></param>
  1564. public void EnterSplitMode(SplitMode mode)
  1565. {
  1566. ///通知UI层更改布局
  1567. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  1568. }
  1569. private void Undo()
  1570. {
  1571. if (isInPageEdit)
  1572. {
  1573. //执行页面编辑的Undo
  1574. PageEditUndo?.Invoke();
  1575. }
  1576. else
  1577. {
  1578. PDFViewer.UndoManager.Undo();
  1579. }
  1580. }
  1581. private void Redo()
  1582. {
  1583. if (isInPageEdit)
  1584. {
  1585. //执行页面编辑的Redo
  1586. PageEditRedo?.Invoke();
  1587. }
  1588. else
  1589. {
  1590. PDFViewer.UndoManager.Redo();
  1591. }
  1592. }
  1593. private void LoadControl()
  1594. {
  1595. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  1596. // 非必要情况不要使用该异步方法,可能会导致一次性加载多个文件时出现因异步引起的regionname 错乱问题
  1597. //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  1598. // {
  1599. NavigationParameters parameters = new NavigationParameters();
  1600. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1601. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1602. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  1603. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  1604. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  1605. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  1606. //TODO 根据上一次关闭记录的菜单,选中TabItem
  1607. EnterSelectedBar("TabItemAnnotation");
  1608. //}
  1609. //));
  1610. //if (App.IsBookMode)
  1611. //{
  1612. // RbtnReadMode();
  1613. //}
  1614. //else
  1615. //{
  1616. // UnReadModel();
  1617. //}
  1618. }
  1619. /// <summary>
  1620. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  1621. /// </summary>
  1622. /// <param name="Content"></param>
  1623. /// <param name="annotPropertyPanel"></param>
  1624. public void SelectedPrpoertyPanel(string Content, AnnotTransfer annotPropertyPanel)
  1625. {
  1626. if (Content == "PropertyPanelContent")
  1627. {
  1628. IsPropertyOpen = false;
  1629. }
  1630. NavigationParameters parameters = new NavigationParameters();
  1631. //传其他参数:文档类,空注释面板;
  1632. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1633. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  1634. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1635. region.RequestNavigate(PropertyRegionName, Content, parameters);
  1636. }
  1637. /// <summary>
  1638. /// 将PDFViwer添加到Region
  1639. /// </summary>
  1640. private bool loadFile()
  1641. {
  1642. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  1643. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  1644. CanSave = PDFViewer.UndoManager.CanSave;
  1645. CanUndo = PDFViewer.UndoManager.CanUndo;
  1646. CanRedo = PDFViewer.UndoManager.CanRedo;
  1647. if (passwordInfo != null && (!string.IsNullOrEmpty(passwordInfo.OpenPassword) || !string.IsNullOrEmpty(passwordInfo.PermissionsPassword)))
  1648. {
  1649. if (!string.IsNullOrEmpty(passwordInfo.OpenPassword))
  1650. {
  1651. PDFViewer.Document.UnlockWithPassword(passwordInfo.OpenPassword);
  1652. }
  1653. if (!string.IsNullOrEmpty(passwordInfo.PermissionsPassword))
  1654. {
  1655. PDFViewer.Document.UnlockWithPassword(passwordInfo.PermissionsPassword);
  1656. }
  1657. }
  1658. else
  1659. {
  1660. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
  1661. if (result.IsDiscryptied)
  1662. {
  1663. if (result.Password != null)
  1664. {
  1665. string filePath = PDFViewer.Document.FilePath;
  1666. PDFViewer.Document.UnlockWithPassword(result.Password);
  1667. PDFViewer.Tag = result.Password;
  1668. //记录密码到内存
  1669. this.PassWord = result.Password;
  1670. }
  1671. }
  1672. if (result.IsDiscryptied == false)
  1673. {
  1674. //未成功解密文档时,释放Document对象,返回
  1675. PDFViewer.Document.Release();
  1676. //取消解密时关闭当前页签
  1677. App.mainWindowViewModel.CloseTabItem(App.mainWindowViewModel.SelectedItem);
  1678. return false;
  1679. }
  1680. }
  1681. PDFViewer.Load();
  1682. PDFViewer.SetFormFieldHighlight(Settings.Default.AppProperties.InitialVIew.HighlightLink);
  1683. PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1684. //还原上一次关闭前的视图设置
  1685. GetOpenFileInfo();
  1686. region.AddToRegion(ViwerRegionName, PDFViewer);
  1687. if (!App.OpenedFileList.Contains(PDFViewer.Document.FilePath))
  1688. {
  1689. App.OpenedFileList.Add(PDFViewer.Document.FilePath);
  1690. }
  1691. return true;
  1692. }
  1693. /// <summary>
  1694. /// 还原上一次浏览的视图相关设置
  1695. /// </summary>
  1696. private void GetOpenFileInfo()
  1697. {
  1698. OpenFileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1699. //偏好设置里设置了记录上次视图设置时
  1700. if (OpenFileInfo != null && Settings.Default.AppProperties.Description.RecoveryViewWhenOpen)
  1701. {
  1702. if (OpenFileInfo.LastViewMode != PDFViewer.ModeView)
  1703. {
  1704. PDFViewer.ChangeViewMode(OpenFileInfo.LastViewMode);
  1705. }
  1706. if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
  1707. {
  1708. PDFViewer.ChangeFitMode(OpenFileInfo.LastFitMode);
  1709. }
  1710. if (OpenFileInfo.LastFitMode == ComPDFKitViewer.FitMode.FitFree)
  1711. {
  1712. PDFViewer.Zoom(OpenFileInfo.LastZoom);
  1713. }
  1714. if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
  1715. {
  1716. PDFViewer.GoToPage(OpenFileInfo.LastPageIndex);
  1717. }
  1718. PDFViewer.SetSplitMode(OpenFileInfo.LastSplitMode);
  1719. PDFViewer.SetShowLink(OpenFileInfo.ShowHighLightLink);
  1720. if (OpenFileInfo.LastPageSpace)
  1721. {
  1722. PDFViewer.SetPageSpacing(8);
  1723. }
  1724. else
  1725. {
  1726. PDFViewer.SetPageSpacing(0);
  1727. }
  1728. if (OpenFileInfo.LastDrawMode == ComPDFKitViewer.DrawModes.Draw_Mode_Custom && OpenFileInfo.LastFillColor != 0)
  1729. {
  1730. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode, OpenFileInfo.LastFillColor);
  1731. }
  1732. else
  1733. {
  1734. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode);
  1735. }
  1736. }
  1737. else
  1738. {
  1739. //新文档应用默认设置
  1740. PDFViewer.ChangeViewMode(Settings.Default.AppProperties.InitialVIew.PageView);
  1741. PDFViewer.ChangeFitMode(Settings.Default.AppProperties.InitialVIew.ZoomMode);
  1742. PDFViewer.SetShowLink(Settings.Default.AppProperties.InitialVIew.HighlightLink);
  1743. }
  1744. }
  1745. /// <summary>
  1746. /// 已有路径文档的保存逻辑
  1747. /// </summary>
  1748. public bool saveFile()
  1749. {
  1750. try
  1751. {
  1752. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  1753. return saveAsFile();
  1754. //文档已被修复时 提示另存为
  1755. if (PDFViewer.Document.HasRepaired)
  1756. {
  1757. AlertsMessage alertsMessage = new AlertsMessage();
  1758. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  1759. if (alertsMessage.result == ContentResult.Ok)
  1760. return saveAsFile();
  1761. else
  1762. return false;
  1763. }
  1764. //文件路径无法存在时
  1765. if (!File.Exists(PDFViewer.Document.FilePath))
  1766. {
  1767. AlertsMessage alertsMessage = new AlertsMessage();
  1768. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  1769. if (alertsMessage.result == ContentResult.Ok)
  1770. return saveAsFile();
  1771. else
  1772. return false;
  1773. }
  1774. //只读文件无法写入时,提示另存为
  1775. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  1776. if (fileInfo.IsReadOnly)
  1777. {
  1778. AlertsMessage alertsMessage = new AlertsMessage();
  1779. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  1780. if (alertsMessage.result == ContentResult.Ok)
  1781. return saveAsFile();
  1782. else
  1783. return false;
  1784. }
  1785. //Security Fixed:
  1786. //修改密码后原文档保存的步骤:
  1787. //在设置密码窗口传出“已修改”信号,并允许延迟保存
  1788. //
  1789. if (SecurityInfo.IsPasswordChanged)
  1790. {
  1791. string currentFilePath = PDFViewer.Document.FilePath;
  1792. string tempFilePath = PDFViewer.Document.FilePath + ".temp.pdf";
  1793. string openPassword = string.Empty;
  1794. string permissionsPassword = string.Empty;
  1795. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1796. if (SecurityInfo.NeedOpenPassword)
  1797. {
  1798. openPassword = SecurityInfo.OpenPassword;
  1799. }
  1800. if (SecurityInfo.NeedPermissionsPassword)
  1801. {
  1802. permissionsPassword = SecurityInfo.PermissionsPassword;
  1803. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1804. }
  1805. try
  1806. {
  1807. string currentPath = PDFViewer.Document.FilePath;
  1808. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1809. if (PDFViewer.Document.Descrypt(tempPath))
  1810. {
  1811. PDFViewer.CloseDocument();
  1812. PDFViewer.InitDocument(tempPath);
  1813. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1814. if (PDFViewer.Document.WriteToFilePath(currentPath))
  1815. {
  1816. PDFViewer.CloseDocument();
  1817. PDFViewer.InitDocument(currentPath);
  1818. if (!string.IsNullOrEmpty(openPassword))
  1819. {
  1820. PDFViewer.Document.UnlockWithPassword(openPassword);
  1821. }
  1822. if (!string.IsNullOrEmpty(permissionsPassword))
  1823. {
  1824. PDFViewer.Document.CheckOwnerPassword(permissionsPassword);
  1825. }
  1826. try
  1827. {
  1828. PDFViewer.Load();
  1829. }
  1830. catch
  1831. {
  1832. }
  1833. PDFViewer.UndoManager.CanSave = false;
  1834. SecurityInfo.IsPasswordChanged = false;
  1835. }
  1836. else
  1837. {
  1838. return false;
  1839. }
  1840. System.IO.File.Delete(tempPath);
  1841. }
  1842. else
  1843. {
  1844. return false;
  1845. }
  1846. return true;
  1847. }
  1848. catch
  1849. {
  1850. return false;
  1851. }
  1852. }
  1853. else if (SecurityInfo.IsPasswordRemoved)
  1854. {
  1855. try
  1856. {
  1857. string currentPath = PDFViewer.Document.FilePath;
  1858. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1859. PDFViewer.Document.Descrypt(tempPath);
  1860. PDFViewer.CloseDocument();
  1861. PDFViewer.InitDocument(tempPath);
  1862. PDFViewer.Document.WriteToFilePath(currentPath);
  1863. PDFViewer.CloseDocument();
  1864. PDFViewer.InitDocument(currentPath);
  1865. PDFViewer.Load();
  1866. System.IO.File.Delete(tempPath);
  1867. }
  1868. catch
  1869. {
  1870. return false;
  1871. }
  1872. }
  1873. bool result = PDFViewer.Document.WriteToLoadedPath();
  1874. if (result)
  1875. {
  1876. PDFViewer.UndoManager.CanSave = false;
  1877. App.Current.Dispatcher.Invoke(() =>
  1878. {
  1879. //保存时更新缩略图
  1880. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1881. try
  1882. {
  1883. if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PDFViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  1884. {
  1885. var size = PDFViewer.Document.GetPageSize(0);
  1886. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  1887. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  1888. if (File.Exists(folderPath))
  1889. File.Delete(folderPath);
  1890. DirectoryInfo folder = new DirectoryInfo(folderPath);
  1891. if (!folder.Exists)
  1892. folder.Create();
  1893. string imagePath = info.ThumbImgPath;
  1894. if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  1895. {
  1896. string imageName = Guid.NewGuid().ToString();
  1897. imagePath = System.IO.Path.Combine(folderPath, imageName);
  1898. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  1899. {
  1900. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1901. }
  1902. }
  1903. else
  1904. {
  1905. using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  1906. {
  1907. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1908. }
  1909. }
  1910. info.ThumbImgPath = imagePath;
  1911. SettingHelper.SetFileInfo(info);
  1912. }
  1913. }
  1914. catch
  1915. {
  1916. info.ThumbImgPath = null;
  1917. SettingHelper.SetFileInfo(info);
  1918. }
  1919. });
  1920. PDFViewer.ReloadDocument();
  1921. }
  1922. else
  1923. {
  1924. //文件被占用 保存失败时
  1925. AlertsMessage alertsMessage = new AlertsMessage();
  1926. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  1927. if (alertsMessage.result == ContentResult.Ok)
  1928. return saveAsFile();
  1929. else
  1930. return false;
  1931. }
  1932. return result;
  1933. }
  1934. catch { return false; }
  1935. finally
  1936. {
  1937. ShowLeftTip(false);
  1938. }
  1939. }
  1940. /// <summary>
  1941. /// 另存为或新文档保存逻辑
  1942. /// </summary>
  1943. public bool saveAsFile(Action RedactionAction = null)
  1944. {
  1945. var dlg = new Microsoft.Win32.SaveFileDialog();
  1946. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1947. dlg.FileName = PDFViewer.Document.FileName;
  1948. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1949. {
  1950. bool result = false;
  1951. if (RedactionAction != null)
  1952. {
  1953. //保存前进行标记密文处理应用或擦除
  1954. RedactionAction.Invoke();
  1955. }
  1956. if (App.OpenedFileList.Contains(dlg.FileName))
  1957. {
  1958. //提示文件已经被打开
  1959. }
  1960. else
  1961. {
  1962. string oldpath = PDFViewer.Document.FilePath;
  1963. string openPassword = string.Empty;
  1964. string permissionsPassword = string.Empty;
  1965. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1966. if (SecurityInfo.NeedOpenPassword)
  1967. {
  1968. openPassword = SecurityInfo.OpenPassword;
  1969. passwordInfo.OpenPassword = openPassword;
  1970. }
  1971. if (SecurityInfo.NeedPermissionsPassword)
  1972. {
  1973. permissionsPassword = SecurityInfo.PermissionsPassword;
  1974. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1975. passwordInfo.PermissionsPassword = permissionsPassword;
  1976. }
  1977. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1978. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  1979. if (result)
  1980. {
  1981. App.OpenedFileList.Remove(oldpath);
  1982. DoAfterSaveAs(dlg.FileName);
  1983. }
  1984. else
  1985. {
  1986. //提示文件被其他软件占用 无法保存
  1987. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") });
  1988. }
  1989. }
  1990. return result;
  1991. }
  1992. else
  1993. return false;
  1994. }
  1995. /// <summary>
  1996. /// 另存为Flatten
  1997. /// </summary>
  1998. private void saveAsFlatten()
  1999. {
  2000. var dlg = new SaveFileDialog();
  2001. dlg.Filter = Properties.Resources.OpenDialogFilter;
  2002. dlg.FileName = PDFViewer.Document.FileName.Substring(0, PDFViewer.Document.FileName.LastIndexOf(".")) + "_Flatten.pdf";
  2003. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  2004. {
  2005. saveFile();
  2006. CPDFDocument kmdoc = CPDFDocument.InitWithFilePath(PDFViewer.Document.FileName);
  2007. if (kmdoc == null)
  2008. return;
  2009. bool result = kmdoc.WriteFlattenToFilePath(dlg.FileName);
  2010. if (result)
  2011. System.Diagnostics.Process.Start("explorer", "/select,\"" + dlg.FileName + "\"");
  2012. kmdoc.Release();
  2013. }
  2014. }
  2015. /// <summary>
  2016. /// 另存为后进行的操作
  2017. /// 重新打开新文档
  2018. /// </summary>
  2019. /// <param name="targetPath"></param>
  2020. public void DoAfterSaveAs(string targetPath)
  2021. {
  2022. string oldFilePath = targetPath;
  2023. PDFViewer.UndoManager.CanSave = false;
  2024. mainViewModel.OpenFile(targetPath, passwordInfo);
  2025. //TODO:通知各模块更新PDFview对象
  2026. //var result = OpenFile(targetPath, true);
  2027. //if (result)
  2028. //{
  2029. // FileChanged.Invoke(this, null);
  2030. // Zoomer.PdfViewer = PdfViewer;
  2031. // PageSelector.PdfViewer = PdfViewer;
  2032. // ViewModeSelector.PdfViewer = PdfViewer;
  2033. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  2034. // if (fileInfo != null)
  2035. // {
  2036. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  2037. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  2038. // if (fileInfo.LastFitMode == FitMode.FitFree)
  2039. // PdfViewer.Zoom(fileInfo.LastZoom);
  2040. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  2041. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  2042. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  2043. // else
  2044. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  2045. // }
  2046. //}
  2047. }
  2048. /// <summary>
  2049. /// 显示前添加内容到Region
  2050. /// </summary>
  2051. /// <param name="isPageEdit"></param>
  2052. private void ShowContent(string currentBar, bool isToolMode = false)
  2053. {
  2054. GridVisibility = Visibility.Visible;
  2055. //显示页面编辑或其他工具
  2056. if (currentBar == "TabItemPageEdit" || isToolMode)
  2057. {
  2058. if (currentBar == "TabItemPageEdit")//进入页面编辑
  2059. {
  2060. if (GridToolRow != 1)
  2061. {
  2062. GridToolRow = 1;
  2063. }
  2064. if (GridToolRowSpan != 3)
  2065. {
  2066. GridToolRowSpan = 3;
  2067. }
  2068. }
  2069. else//进入水印等其他工具模式
  2070. {
  2071. GridVisibility = Visibility.Collapsed;
  2072. if (GridToolRow != 0)
  2073. {
  2074. GridToolRow = 0;
  2075. }
  2076. if (GridToolRowSpan != 4)
  2077. {
  2078. GridToolRowSpan = 4;
  2079. }
  2080. }
  2081. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  2082. UpdateShowContent(currentBar);
  2083. }
  2084. else
  2085. {
  2086. if (GridToolRow != 1)
  2087. {
  2088. GridToolRow = 1;
  2089. }
  2090. UpdateShowContent(currentBar);
  2091. }
  2092. }
  2093. /// <summary>
  2094. /// 从二级工具栏进入需要修改界面布局的场景
  2095. /// </summary>
  2096. /// <param name="EditToolName"></param>
  2097. private void EnterEditTools(StringWithUnicode EditToolName)
  2098. {
  2099. EnterSelectedEditTool(EditToolName);
  2100. }
  2101. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  2102. {
  2103. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  2104. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  2105. {
  2106. PDFViewer.Document.ReleasePages();
  2107. PDFViewer.ReloadDocument();
  2108. }
  2109. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  2110. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  2111. {
  2112. region.AddToRegion(ViwerRegionName, PDFViewer);
  2113. }
  2114. CurrentBar = "TabItemTool";
  2115. EnterSelectedBar(CurrentBar);
  2116. }
  2117. /// <summary>
  2118. /// 二级菜单指定目标处理
  2119. /// </summary>
  2120. /// <param name="e"></param>
  2121. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  2122. {
  2123. CurrentBar = EditToolName.EditToolsContentName;
  2124. EnterToolMode(CurrentBar);
  2125. }
  2126. /// <summary>
  2127. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  2128. /// </summary>
  2129. /// <param name="targetToolMode">要导航过去的控件名称</param>
  2130. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  2131. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  2132. {
  2133. IsLoading = Visibility.Visible;
  2134. await Task.Delay(3);
  2135. NavigationParameters param = new NavigationParameters();
  2136. if (valuePairs == null)
  2137. {
  2138. param.Add(ParameterNames.PDFViewer, PDFViewer);
  2139. param.Add(ParameterNames.ViewContentViewModel, this);
  2140. }
  2141. else//有传入其他内容的参数时
  2142. {
  2143. param = valuePairs;
  2144. }
  2145. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  2146. ShowContent(CurrentBar, true);
  2147. IsLoading = Visibility.Collapsed;
  2148. }
  2149. private void EnterSelectedBar(string currentBar)
  2150. {
  2151. NavigationParameters param = new NavigationParameters();
  2152. param.Add(ParameterNames.PDFViewer, PDFViewer);
  2153. param.Add(ParameterNames.ViewContentViewModel, this);
  2154. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  2155. if (currentBar == "TabItemScan")
  2156. {
  2157. region.RequestNavigate(RegionNames.OCRViewerRegionName, "ScanViwer", param);
  2158. }
  2159. ShowContent(currentBar);
  2160. }
  2161. /// <summary>
  2162. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  2163. /// </summary>
  2164. public void ExitToolMode()
  2165. {
  2166. ToolContentVisible = Visibility.Collapsed;
  2167. if (isInPageEdit)
  2168. {
  2169. TabSelectedIndex = 0;
  2170. isInPageEdit = false;
  2171. }
  2172. }
  2173. #endregion 方法
  2174. }
  2175. }