ViewContentViewModel.cs 92 KB

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