ViewContentViewModel.cs 92 KB

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