ViewContentViewModel.cs 84 KB

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