ViewContentViewModel.cs 87 KB

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