PageEditContentViewModel.cs 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit_Conversion.Converter;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Dropbox.Api.Team;
  5. using Microsoft.Win32;
  6. using PDF_Master.CustomControl;
  7. using PDF_Master.EventAggregators;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model;
  10. using PDF_Master.Model.HomePageToolsDialogs;
  11. using PDF_Master.Model.PageEdit;
  12. using PDF_Master.Properties;
  13. using PDFReader_WPF.Helper;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. using Prism.Regions;
  18. using Prism.Services.Dialogs;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Diagnostics;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Input;
  30. using System.Windows.Media;
  31. using System.Windows.Media.Imaging;
  32. using WIA;
  33. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  34. namespace PDF_Master.ViewModels.PageEdit
  35. {
  36. public class PageEditContentViewModel : BindableBase, INavigationAware
  37. {
  38. #region 文案
  39. private string T_title;
  40. public string T_Title
  41. {
  42. get { return T_title; }
  43. set
  44. {
  45. SetProperty(ref T_title, value);
  46. }
  47. }
  48. private string T_rotateLeftToolTip;
  49. public string T_RotateLeftToolTip
  50. {
  51. get { return T_rotateLeftToolTip; }
  52. set
  53. {
  54. SetProperty(ref T_rotateLeftToolTip, value);
  55. }
  56. }
  57. private string T_rotateRightToolTip;
  58. public string T_RotateRightToolTip
  59. {
  60. get { return T_rotateRightToolTip; }
  61. set
  62. {
  63. SetProperty(ref T_rotateRightToolTip, value);
  64. }
  65. }
  66. private string T_deleteToolTip;
  67. public string T_DeleteToolTip
  68. {
  69. get { return T_deleteToolTip; }
  70. set
  71. {
  72. SetProperty(ref T_deleteToolTip, value);
  73. }
  74. }
  75. private string T_deleteLook = "";
  76. public string T_DeleteLook
  77. {
  78. get { return T_deleteLook; }
  79. set
  80. {
  81. SetProperty(ref T_deleteLook, value);
  82. }
  83. }
  84. private string t_Insert;
  85. public string T_Insert
  86. {
  87. get { return t_Insert; }
  88. set
  89. {
  90. SetProperty(ref t_Insert, value);
  91. }
  92. }
  93. private string t_InsertFile;
  94. public string T_InsertFile
  95. {
  96. get { return t_InsertFile; }
  97. set
  98. {
  99. SetProperty(ref t_InsertFile, value);
  100. }
  101. }
  102. private string t_InsertaBlankPage;
  103. public string T_InsertaBlankPage
  104. {
  105. get { return t_InsertaBlankPage; }
  106. set
  107. {
  108. SetProperty(ref t_InsertaBlankPage, value);
  109. }
  110. }
  111. private string t_InsertaCustomPage;
  112. public string T_InsertaCustomPage
  113. {
  114. get { return t_InsertaCustomPage; }
  115. set
  116. {
  117. SetProperty(ref t_InsertaCustomPage, value);
  118. }
  119. }
  120. private string t_Extract;
  121. public string T_Extract
  122. {
  123. get { return t_Extract; }
  124. set
  125. {
  126. SetProperty(ref t_Extract, value);
  127. }
  128. }
  129. private string t_Replace;
  130. public string T_Replace
  131. {
  132. get { return t_Replace; }
  133. set
  134. {
  135. SetProperty(ref t_Replace, value);
  136. }
  137. }
  138. private string t_Split;
  139. public string T_Split
  140. {
  141. get { return t_Split; }
  142. set
  143. {
  144. SetProperty(ref t_Split, value);
  145. }
  146. }
  147. private string t_InsertPage;
  148. public string T_InsertPage
  149. {
  150. get { return t_InsertPage; }
  151. set
  152. {
  153. SetProperty(ref t_InsertPage, value);
  154. }
  155. }
  156. private string t_Reverse;
  157. public string T_Reverse
  158. {
  159. get { return t_Reverse; }
  160. set
  161. {
  162. SetProperty(ref t_Reverse, value);
  163. }
  164. }
  165. private string t_ExtractPage;
  166. public string T_ExtractPage
  167. {
  168. get { return t_ExtractPage; }
  169. set
  170. {
  171. SetProperty(ref t_ExtractPage, value);
  172. }
  173. }
  174. private string t_ReplacePage;
  175. public string T_ReplacePage
  176. {
  177. get { return t_ReplacePage; }
  178. set
  179. {
  180. SetProperty(ref t_ReplacePage, value);
  181. }
  182. }
  183. private void InitString()
  184. {
  185. T_InsertFile = App.MainPageLoader.GetString("PageEdit_Insert");
  186. T_Insert = App.MainPageLoader.GetString("PageEdit_InsertFile");
  187. T_InsertaBlankPage = App.MainPageLoader.GetString("PageEdit_InsertaBlankPage");
  188. T_InsertaCustomPage = App.MainPageLoader.GetString("PageEdit_InsertaCustomPage");
  189. T_Extract = App.MainPageLoader.GetString("PageEdit_Extract");
  190. T_Replace = App.MainPageLoader.GetString("PageEdit_Replace");
  191. T_Split = App.MainPageLoader.GetString("PageEdit_Split");
  192. T_InsertPage = App.MainPageLoader.GetString("PageEdit_InsertPage");
  193. T_Reverse = App.MainPageLoader.GetString("PageEdit_Reverse");
  194. T_ExtractPage = App.MainPageLoader.GetString("PageEdit_ExtractPage");
  195. T_ReplacePage = App.MainPageLoader.GetString("PageEdit_ReplacePage");
  196. T_Title = App.MainPageLoader.GetString("Thumbnails_Title");
  197. T_DeleteToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_Delete");
  198. T_DeleteLook = App.MainPageLoader.GetString("T_DeleteLook");
  199. T_RotateRightToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_RotateRight");
  200. T_RotateLeftToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_RotateLeft");
  201. }
  202. #endregion 文案
  203. public CPDFViewer PDFViewer;
  204. public PageEditItem CurrentPageEditItem = null;
  205. public ViewContentViewModel viewContentViewModel;
  206. private IDialogService dialogs;
  207. private IEventAggregator eventor;
  208. /// <summary>
  209. /// 用于筛选多窗体时间的唯一识别码
  210. /// </summary>
  211. private string unicode = null;
  212. /// <summary>
  213. /// 项数据集合
  214. /// </summary>
  215. public ObservableCollection<PageEditItem> PageEditItems { get; set; }
  216. /// <summary>
  217. /// 页面范围的项集合
  218. /// </summary>
  219. public ObservableCollection<string> PageRangeItems { get; set; }
  220. #region 属性
  221. /// <summary>
  222. /// 判断是否是添加页面
  223. /// </summary>
  224. public bool IsAddPages { get; set; } = false;
  225. /// <summary>
  226. /// 增加两个变量,用于记录当前控件内已经刷新图片的索引值,用于显示页面尺寸大小时,先显示当前范围可见Item的页面尺寸
  227. /// </summary>
  228. private int startRefreshedIndex = 0;
  229. private int endRefreshedIndex = 0;
  230. /// <summary>
  231. /// 正在初始化(正在初始化过程中 不发布通知BOTA刷新的事件)
  232. /// </summary>
  233. public bool Initializing = false;
  234. /// <summary>
  235. /// 是否是第一次加载
  236. /// </summary>
  237. private bool isFirstLoad = true;
  238. /// <summary>
  239. /// 记录多选项中最后一项的索引
  240. /// </summary>
  241. public int maxSelectedIndex = -1;
  242. private Visibility showTip = Visibility.Collapsed;
  243. /// <summary>
  244. /// 是否显示渐隐提示
  245. /// </summary>
  246. public Visibility ShowTip
  247. {
  248. get { return showTip; }
  249. set
  250. {
  251. SetProperty(ref showTip, value);
  252. }
  253. }
  254. private Visibility isLoading = Visibility.Collapsed;
  255. /// <summary>
  256. /// 是否正在处理中
  257. /// </summary>
  258. public Visibility IsLoading
  259. {
  260. get { return isLoading; }
  261. set
  262. {
  263. SetProperty(ref isLoading, value);
  264. }
  265. }
  266. private int listSelecedIndex;
  267. /// <summary>
  268. /// 缩略图选中项的索引
  269. /// </summary>
  270. public int ListSelectedIndex
  271. {
  272. get { return listSelecedIndex; }
  273. set
  274. {
  275. SetProperty(ref listSelecedIndex, value);
  276. if (!IsSelectFormSelf && value >= 0)
  277. {
  278. //显示页面范围选择控件
  279. TxtPageInputVisibility = Visibility.Visible;
  280. TbPageRange = (ListSelectedIndex + 1).ToString();
  281. }
  282. }
  283. }
  284. /// <summary>
  285. ///右键不会选中页面, 不要跳转
  286. /// </summary>
  287. /// 判断是否是右键选择
  288. private bool isMouseRightButtonDown = false;
  289. public bool IsMouseRightButtonDown
  290. {
  291. get { return isMouseRightButtonDown; }
  292. set { SetProperty(ref isMouseRightButtonDown, value); }
  293. }
  294. /// <summary>
  295. /// 右键选择索引
  296. /// </summary>
  297. public int RightSelectedIndex = 0;
  298. /// <summary>
  299. /// 右键选择 集合
  300. /// </summary>
  301. public List<int> RightSelectedIndexs = new List<int>();
  302. /// <summary>
  303. /// 多选
  304. /// </summary>
  305. private bool isMultiSelected;
  306. /// <summary>
  307. /// 是否有多选 用于判断倒序和替换功能
  308. /// </summary>
  309. public bool IsMultiSelected
  310. {
  311. get { return isMultiSelected; }
  312. set
  313. {
  314. SetProperty(ref isMultiSelected, value);
  315. }
  316. }
  317. private int comboxSelectIndex;
  318. /// <summary>
  319. /// 下拉框选项集合
  320. /// </summary>
  321. public int ComboxSelectIndex
  322. {
  323. get { return comboxSelectIndex; }
  324. set
  325. {
  326. SetProperty(ref comboxSelectIndex, value);
  327. //隐藏tb页面范围显示控件
  328. TxtPageInputVisibility = Visibility.Collapsed;
  329. //选中指定的页面范围
  330. SuitPageRange(comboxSelectIndex);
  331. }
  332. }
  333. private Visibility tbPageRangeVisibility = Visibility.Visible;
  334. /// <summary>
  335. /// 页面范围的文本显示
  336. /// </summary>
  337. public Visibility TbPageRangeVisibility
  338. {
  339. get { return tbPageRangeVisibility; }
  340. set
  341. {
  342. SetProperty(ref tbPageRangeVisibility, value);
  343. if (value == Visibility.Visible)
  344. {
  345. comboxSelectIndex = -1;
  346. }
  347. }
  348. }
  349. private string tbPageRange;
  350. /// <summary>
  351. /// 页面范围显示的内容
  352. /// </summary>
  353. public string TbPageRange
  354. {
  355. get { return tbPageRange; }
  356. set
  357. {
  358. SetProperty(ref tbPageRange, value);
  359. }
  360. }
  361. private Visibility txtPageInputVisibility = Visibility.Collapsed;
  362. /// <summary>
  363. /// 页面自定义输入框的显示
  364. /// </summary>
  365. public Visibility TxtPageInputVisibility
  366. {
  367. get { return txtPageInputVisibility; }
  368. set
  369. {
  370. SetProperty(ref txtPageInputVisibility, value);
  371. }
  372. }
  373. /// <summary>
  374. /// 页面缩放状态,-1 为最小值,1为最大值
  375. /// </summary>
  376. private int zoomIndex = 0;
  377. public int ZoomIndex
  378. {
  379. get { return zoomIndex; }
  380. set
  381. {
  382. SetProperty(ref zoomIndex, value);
  383. }
  384. }
  385. private string toastText = App.MainPageLoader.GetString("PageEdit_Failed");
  386. /// <summary>
  387. /// Toast提示
  388. /// </summary>
  389. public string ToastText
  390. {
  391. get { return toastText; }
  392. set
  393. {
  394. SetProperty(ref toastText, value);
  395. }
  396. }
  397. private Visibility isBOTAThumb = Visibility.Collapsed;
  398. /// <summary>
  399. /// 是否是BOTA侧边栏的缩略图
  400. /// </summary>
  401. public Visibility IsBOTAThumb
  402. {
  403. get { return isBOTAThumb; }
  404. set
  405. {
  406. SetProperty(ref isBOTAThumb, value);
  407. }
  408. }
  409. private Visibility showedPageSize = Visibility.Collapsed;
  410. /// <summary>
  411. /// 显示页面尺寸
  412. /// </summary>
  413. public Visibility ShowedPageSize
  414. {
  415. get { return showedPageSize; }
  416. set
  417. {
  418. SetProperty(ref showedPageSize, value);
  419. //if (IsBOTAThumb != Visibility.Visible)
  420. //{
  421. // if (value == Visibility.Visible)
  422. // {
  423. // HidePageSize = Visibility.Collapsed;
  424. // }
  425. // else
  426. // {
  427. // HidePageSize = Visibility.Visible;
  428. // }
  429. //}
  430. }
  431. }
  432. private Visibility hidePageSize = Visibility.Collapsed;
  433. /// <summary>
  434. /// 隐藏页面尺寸
  435. /// </summary>
  436. public Visibility HidePageSize
  437. {
  438. get { return hidePageSize; }
  439. set
  440. {
  441. SetProperty(ref hidePageSize, value);
  442. }
  443. }
  444. private bool canPaste = false;
  445. /// <summary>
  446. /// 是否可以粘贴
  447. /// </summary>
  448. public bool CanPaste
  449. {
  450. get { return canPaste; }
  451. set
  452. {
  453. SetProperty(ref canPaste, value);
  454. }
  455. }
  456. /// <summary>
  457. /// 页面缩放的范围
  458. /// </summary>
  459. private List<Size> ItemSizes = new List<Size>() {
  460. new Size(104,142),
  461. new Size(208, 294),
  462. new Size(312,441)
  463. };
  464. //选中的页面集合
  465. private List<int> selectedPageList = new List<int>();
  466. /// <summary>
  467. /// 是否是下拉框选项改变引起的选项变更事件
  468. /// </summary>
  469. private bool IsSelectFormSelf = false;
  470. public bool IsShowPageSize = false;
  471. public string Txt = Properties.Resources.txtex;
  472. public string Word = Properties.Resources.wordex;
  473. public string Ppt = Properties.Resources.pptex;
  474. public string Excel = Properties.Resources.excelex;
  475. public string Html = Properties.Resources.htmlex;
  476. public string Image = Properties.Resources.imageex;
  477. public string Pdf = Properties.Resources.pdf;
  478. #endregion 属性
  479. #region 命令
  480. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  481. /// <summary>
  482. /// 拦截键盘输入的事件
  483. /// </summary>
  484. public DelegateCommand<object> PreviewKeyDown { get; set; }
  485. /// <summary>
  486. /// 粘贴
  487. /// </summary>
  488. public DelegateCommand PasteCommand { get; set; }
  489. /// <summary>
  490. /// 复制
  491. /// </summary>
  492. public DelegateCommand CopyCommand { get; set; }
  493. /// <summary>
  494. /// 剪切
  495. /// </summary>
  496. public DelegateCommand CutCommand { get; set; }
  497. /// <summary>
  498. /// 替换文件
  499. /// </summary>
  500. public DelegateCommand ReplaceCommand { get; set; }
  501. /// <summary>
  502. /// 插入文件命令
  503. /// </summary>
  504. public DelegateCommand<object> InsertCommand { get; set; }
  505. /// <summary>
  506. /// 拆分文件
  507. /// </summary>
  508. public DelegateCommand SplitCommand { get; set; }
  509. /// <summary>
  510. /// 提取文件
  511. /// </summary>
  512. public DelegateCommand ExtractCommand { get; set; }
  513. /// <summary>
  514. /// 逆时针旋转
  515. /// </summary>
  516. public DelegateCommand LeftRotateCommand { get; set; }
  517. /// <summary>
  518. /// 顺时针旋转
  519. /// </summary>
  520. public DelegateCommand RightRotateCommand { get; set; }
  521. /// <summary>
  522. /// 删除
  523. /// </summary>
  524. public DelegateCommand DeleteCommand { get; set; }
  525. /// <summary>
  526. /// 倒序
  527. /// </summary>
  528. public DelegateCommand ReverseCommand { get; set; }
  529. /// <summary>
  530. /// 放大
  531. /// </summary>
  532. public DelegateCommand ZoomInCommand { get; set; }
  533. /// <summary>
  534. /// 缩小
  535. /// </summary>
  536. public DelegateCommand ZoomOutCommand { get; set; }
  537. /// <summary>
  538. /// 显示页面大小
  539. /// </summary>
  540. public DelegateCommand ShowPageSizeCommand { get; set; }
  541. public DelegateCommand ClearSelectedCommand { get; set; }
  542. /// <summary>
  543. /// 进入页面编辑
  544. /// </summary>
  545. public DelegateCommand EnterPageEditCommand { get; set; }
  546. #endregion 命令
  547. public PageEditContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  548. {
  549. //变量初始化
  550. dialogs = dialogService;
  551. eventor = eventAggregator;
  552. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  553. PageEditItems = new ObservableCollection<PageEditItem>();
  554. PageRangeItems = new ObservableCollection<string>();
  555. //绑定命令
  556. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  557. PreviewKeyDown = new DelegateCommand<object>(PreviewKeyDownEvent);
  558. SplitCommand = new DelegateCommand(SplitCommandEvent);
  559. InsertCommand = new DelegateCommand<object>(InsertCommandEvent);
  560. ReplaceCommand = new DelegateCommand(ReplaceCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  561. ExtractCommand = new DelegateCommand(ExtractCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  562. DeleteCommand = new DelegateCommand(DeleteCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  563. LeftRotateCommand = new DelegateCommand(LeftRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  564. RightRotateCommand = new DelegateCommand(RightRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  565. ReverseCommand = new DelegateCommand(ReverseCommandEvent, CanReverseFileCommandExcute).ObservesProperty(() => IsMultiSelected);
  566. ZoomInCommand = new DelegateCommand(ZoomInCommandEvent, CanZoomIn).ObservesProperty(() => ZoomIndex);
  567. ZoomOutCommand = new DelegateCommand(ZoomOutCommandEvent, CanZoomOut).ObservesProperty(() => ZoomIndex);
  568. ShowPageSizeCommand = new DelegateCommand(ShowPageSize);
  569. CopyCommand = new DelegateCommand(copy);
  570. PasteCommand = new DelegateCommand(() => { Paste(); }, CanPasteExcute).ObservesProperty(() => CanPaste);
  571. CutCommand = new DelegateCommand(cut);
  572. ClearSelectedCommand = new DelegateCommand(clearSelected);
  573. EnterPageEditCommand = new DelegateCommand(enterpageeditr);
  574. //订阅页面刷新事件
  575. eventAggregator.GetEvent<PageEditRefreshEvent>().Subscribe(OnPageEditRefreshEvent, e => e.Unicode == unicode);
  576. eventAggregator.GetEvent<ViewContentEvent>().Subscribe(ViewContentChangedEvent, e => e.Unicode == unicode);
  577. CacheFilePath.Instance.PropertyChanged += Instance_PropertyChanged;
  578. RefreshPageRangeItem();
  579. InitString();
  580. }
  581. #region 事件
  582. private void ViewContentChangedEvent(ViewContentEventArgs obj)
  583. {
  584. //只有在缩略图模式下响应刷新,进入页面编辑时 会刷新可见范围内图片,不需要响应这个事件
  585. if (IsBOTAThumb == Visibility.Visible)
  586. {
  587. PageEditItems[PDFViewer.CurrentIndex].IsGetImage = false;
  588. PageEditItems[PDFViewer.CurrentIndex].Size = PDFViewer.Document.GetPageSize(PDFViewer.CurrentIndex);
  589. RefreshItemImage(PDFViewer.CurrentIndex, PDFViewer.CurrentIndex);
  590. }
  591. }
  592. /// <summary>
  593. /// 进入页面编辑
  594. /// </summary>
  595. private void enterpageeditr()
  596. {
  597. viewContentViewModel.EnterPageEdit();
  598. }
  599. private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  600. {
  601. if (e.PropertyName == "CopyDoc")
  602. {
  603. if (CacheFilePath.Instance.CopyDoc != null)
  604. {
  605. CanPaste = true;
  606. }
  607. }
  608. }
  609. /// <summary>
  610. /// 替换功能的逻辑
  611. /// 实现效果是,先删除后插入
  612. /// 跨页多选后替换的位置是多选的第一页位置
  613. /// </summary>
  614. private async void ReplaceCommandEvent()
  615. {
  616. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  617. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Replace);
  618. DataTrackingHelper.IsClearEntryPath = true;
  619. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Replace");
  620. //支持插入格式:PDF、图片、Office格式文件
  621. string allfiles = Pdf + Txt + Word + Excel + Ppt + Image + Html;
  622. OpenFileDialog dialog = new OpenFileDialog();
  623. //dialog.Multiselect = false;
  624. //dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
  625. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  626. $"PDF({Pdf})|{Pdf}|" +
  627. $"Microsoft Office Word({Word})|{Word}|" +
  628. $"Microsoft Office Excel({Excel})|{Excel}|" +
  629. $"Microsoft Office PowerPoint({Ppt})|{Ppt}|" +
  630. $"Txt({Txt})|{Txt}|" +
  631. $"Picture({Image})|{Image}|" +
  632. $"Html({Html})|{Html}"
  633. );
  634. List<int> indexList = new List<int>();
  635. for (int i = 0; i < PageEditItems.Count; i++)
  636. {
  637. if (PageEditItems[i].Selected)
  638. {
  639. indexList.Add(i);
  640. }
  641. }
  642. //因为虚拟化 可能获取不到所有选中项的页码 需要结合选中项的集合判断一下
  643. if (selectedPageList.Count > 0 && selectedPageList.Count > indexList.Count)
  644. {
  645. indexList = new List<int>();
  646. for (int j = 0; j < selectedPageList.Count; j++)
  647. {
  648. indexList.Add(selectedPageList[j] - 1);
  649. }
  650. }
  651. indexList.Sort();
  652. //全选页面 提示不能全选,230711不要提示
  653. //if (indexList.Count > 0 && indexList.Count == PDFViewer.Document.PageCount)
  654. //{
  655. // AlertsMessage alertsMessage = new AlertsMessage();
  656. // alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageEdit_DeleteAllPage"), App.ServiceLoader.GetString("Text_ok"));
  657. // return;
  658. //}
  659. if ((bool)dialog.ShowDialog())
  660. {
  661. //付费锁
  662. if (!ServiceHelper.IAPBeforeFunction())
  663. {
  664. return;
  665. }
  666. IsLoading = Visibility.Visible;
  667. await Task.Delay(200);
  668. int pageCount = PDFViewer.Document.PageCount;
  669. int insertIndex = listSelecedIndex;
  670. string fileName = GetInsertFromFile(dialog.FileName);
  671. #region 历史版本
  672. //从文件插入到指定位置
  673. //var result = await InsertFromFile(listSelecedIndex + 1, fileName);
  674. //if (result)
  675. //{
  676. // //插入成功后再删除原有页面
  677. // //2023-06-05 索引需要-1,因为上面的listSelecedIndex + 1,需要还原
  678. // //DoRemoveSelectedPages(new List<int>() { ListSelectedIndex - 1 });
  679. // DoRemoveSelectedPages(indexList);
  680. //}
  681. #endregion 历史版本
  682. //先记录索引(第一个选定页面)
  683. int index = ListSelectedIndex;
  684. if (IsMouseRightButtonDown)
  685. {
  686. index = RightSelectedIndex;
  687. if (IsMultiSelected)
  688. {
  689. indexList = RightSelectedIndexs;
  690. }
  691. else
  692. {
  693. indexList = new List<int> { index };
  694. }
  695. }
  696. //替换页面 先看文件是否加密,如果加密,先解密后,再替换
  697. //将文档验证是否为空,是否解密,置前,若为空直接退出
  698. bool isDiscryptied = GetDocumentIsDiscryptied(fileName, null, out CPDFDocument doc, out string password);
  699. if (isDiscryptied)
  700. {
  701. //先删除
  702. int flag = DoRemoveSelectedPages(indexList, false, true);
  703. if (flag == -1)
  704. {
  705. return;
  706. }
  707. if (PDFViewer.Document.PageCount == 0)
  708. {
  709. index = 0;
  710. }
  711. //再新增,避免索引串号的情况
  712. var result = await InsertFromFile(index, fileName, doc, password);
  713. if (result)
  714. {
  715. ReloadAfterOption(true, false, Tuple.Create(insertIndex - 1, insertIndex + pageCount));
  716. }
  717. }
  718. IsLoading = Visibility.Collapsed;
  719. }
  720. }
  721. /// <summary>
  722. /// 拆分功能的逻辑
  723. /// </summary>
  724. private void SplitCommandEvent()
  725. {
  726. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  727. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Split);
  728. DataTrackingHelper.IsClearEntryPath = true;
  729. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Split");
  730. DialogParameters valuePairs = new DialogParameters();
  731. valuePairs.Add(ParameterNames.PDFViewer, PDFViewer);
  732. valuePairs.Add(ParameterNames.PageEditSelectedIndex, ListSelectedIndex);
  733. valuePairs.Add(ParameterNames.PageEditSelectedItems, TbPageRange);
  734. dialogs.ShowDialog(DialogNames.SplitDialog, valuePairs, e =>
  735. {
  736. if (e.Result == ButtonResult.OK && e.Parameters != null)
  737. {
  738. //付费锁
  739. if (!ServiceHelper.IAPBeforeFunction())
  740. {
  741. return;
  742. }
  743. //保存并继续,使用了高级功能则弹窗权益弹窗
  744. if (!viewContentViewModel.saveFile(true))
  745. {
  746. return;
  747. }
  748. var model = e.Parameters.GetValue<HomePageSplitDialogModel>(ParameterNames.DataModel);
  749. DoSplitPages(model);
  750. }
  751. });
  752. }
  753. /// <summary>
  754. /// 提取功能的逻辑
  755. /// </summary>
  756. private void ExtractCommandEvent()
  757. {
  758. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  759. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Extract);
  760. DataTrackingHelper.IsClearEntryPath = true;
  761. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Extract");
  762. DialogParameters keyValuePairs = new DialogParameters();
  763. //因无法删除所有页面,若选中所有页面提取,则需要将「提取后删除页面」复选项置灰
  764. if (selectedPageList.Count == PageEditItems.Count)
  765. {
  766. keyValuePairs.Add(ParameterNames.IsDeleteAfterExtract, false);
  767. }
  768. else
  769. {
  770. keyValuePairs.Add(ParameterNames.IsDeleteAfterExtract, true);
  771. }
  772. dialogs.ShowDialog(DialogNames.ExtractDialog, keyValuePairs, e =>
  773. {
  774. if (e.Result == ButtonResult.OK && e.Parameters != null)
  775. {
  776. //付费锁
  777. if (!ServiceHelper.IAPBeforeFunction())
  778. {
  779. return;
  780. }
  781. //保存并继续,使用了高级功能则弹窗权益弹窗
  782. if (!viewContentViewModel.saveFile(true))
  783. {
  784. return;
  785. }
  786. var model = e.Parameters.GetValue<ExtractModel>(ParameterNames.DataModel);
  787. DoExtractPages(model.IsEveryPageToFile, model.IsDeleteAfterExtract);
  788. }
  789. });
  790. }
  791. /// <summary>
  792. /// 插入功能的逻辑
  793. /// </summary>
  794. private async void InsertCommandEvent(object type)
  795. {
  796. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  797. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Insert);
  798. DataTrackingHelper.IsClearEntryPath = true;
  799. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Insert");
  800. if ((string)type == null)
  801. {
  802. return;
  803. }
  804. int pageIndex = 0;
  805. if (IsMouseRightButtonDown)
  806. {
  807. pageIndex = RightSelectedIndex;
  808. }
  809. else
  810. {
  811. pageIndex = ListSelectedIndex;
  812. }
  813. //获取插入位置
  814. int insertIndex = 0;
  815. if (pageIndex < 0)
  816. {
  817. insertIndex = PageEditItems.Count;
  818. }
  819. else
  820. {
  821. insertIndex = pageIndex + 1;
  822. if (insertIndex >= PDFViewer.Document.PageCount)
  823. {
  824. insertIndex = PDFViewer.Document.PageCount;
  825. }
  826. if (isMultiSelected && maxSelectedIndex != -1)
  827. {
  828. insertIndex = maxSelectedIndex + 1;
  829. }
  830. }
  831. IsLoading = Visibility.Visible;
  832. await System.Threading.Tasks.Task.Delay(3);
  833. PageEditHistory history = new PageEditHistory();
  834. history.InsertIndex = insertIndex;
  835. #region 插入空白页面
  836. if ((string)type == ParameterNames.InsertType_Blank)
  837. {
  838. //付费锁
  839. if (!ServiceHelper.IAPBeforeFunction())
  840. {
  841. return;
  842. }
  843. history.Type = ActionType.InsertCustomPages;
  844. history.InsertFilePath = "";
  845. //插入空白页面
  846. bool result;
  847. if (insertIndex == 0)
  848. {
  849. result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex].Size.Width, PageEditItems[insertIndex].Size.Height, null);
  850. history.PageSize = PageEditItems[insertIndex].Size;
  851. }
  852. else
  853. {
  854. result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex - 1].Size.Width, PageEditItems[insertIndex - 1].Size.Height, null);
  855. history.PageSize = PageEditItems[insertIndex - 1].Size;
  856. }
  857. if (!result)
  858. {
  859. IsLoading = Visibility.Collapsed;
  860. ShowToast();
  861. return;
  862. }
  863. //UI层插入Item
  864. var item = GetNewPageEditItem(insertIndex);
  865. PageEditItems.Insert(insertIndex, item);
  866. //插入后 滚动到 选中的页面
  867. ReloadAfterOption(true, false, Tuple.Create(0, insertIndex + 1), true);
  868. await Task.Delay(10);
  869. //选中插入的页面
  870. ListSelectedIndex = insertIndex;
  871. }
  872. #endregion 插入空白页面
  873. #region 插入文件
  874. else if ((string)type == ParameterNames.InsertType_File)
  875. {
  876. //支持插入格式:PDF、图片、Office格式文件
  877. string allfiles = Pdf + Txt + Word + Excel + Ppt + Image + Html;
  878. OpenFileDialog dialog = new OpenFileDialog();
  879. dialog.Multiselect = false;
  880. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  881. $"PDF({Pdf})|{Pdf}|" +
  882. $"Microsoft Office Word({Word})|{Word}|" +
  883. $"Microsoft Office Excel({Excel})|{Excel}|" +
  884. $"Microsoft Office PowerPoint({Ppt})|{Ppt}|" +
  885. $"Txt({Txt})|{Txt}|" +
  886. $"Picture({Image})|{Image}|" +
  887. $"Html({Html})|{Html}"
  888. );
  889. //OpenFileDialog dialog = new OpenFileDialog();
  890. //dialog.Multiselect = false;
  891. //dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
  892. if ((bool)dialog.ShowDialog())
  893. {
  894. //付费锁
  895. if (!ServiceHelper.IAPBeforeFunction())
  896. {
  897. return;
  898. }
  899. string fileName = GetInsertFromFile(dialog.FileName);
  900. //将文档验证是否为空,是否解密,置前,若为空直接退出
  901. bool IsLocked = GetDocumentIsDiscryptied(fileName, null, out CPDFDocument doc, out string password);
  902. if (IsLocked == false)
  903. {
  904. return;
  905. }
  906. //ToolMethod.SetFileThumbImg(fileName);
  907. //插入文件
  908. var result = await InsertFromFile(insertIndex, fileName, doc, password);
  909. if (!result)
  910. {
  911. IsLoading = Visibility.Collapsed;
  912. ShowToast();
  913. return;
  914. }
  915. ReloadAfterOption(true, false, Tuple.Create(0, PDFViewer.Document.PageCount));
  916. }
  917. IsLoading = Visibility.Collapsed;
  918. //里面附带了Undo栈记录,需要提前返回,避免重复记录
  919. return;
  920. }
  921. #endregion 插入文件
  922. #region 插入自定义页面
  923. else
  924. {
  925. DialogParameters keyValuePairs = new DialogParameters();
  926. Size size = new Size();
  927. size.Width = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Width);
  928. size.Height = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Height);
  929. keyValuePairs.Add(ParameterNames.CurrentPageSize, size);
  930. keyValuePairs.Add(ParameterNames.PageEditSelectedIndex, pageIndex);
  931. //int o = ListSelectedIndex;
  932. //插入自定义页面
  933. dialogs.ShowDialog(DialogNames.InsertDialog, keyValuePairs, e =>
  934. {
  935. if (e.Result == ButtonResult.OK && e.Parameters != null)
  936. {
  937. //付费锁
  938. if (!ServiceHelper.IAPBeforeFunction())
  939. {
  940. return;
  941. }
  942. var model = e.Parameters.GetValue<CustomInsertModel>(ParameterNames.DataModel);
  943. var result = InsertFromCustomPage(insertIndex, model.width, model.height, model.filepath);
  944. if (!result)
  945. {
  946. return;
  947. }
  948. history.Type = ActionType.InsertCustomPages;
  949. history.InsertFilePath = model.filepath;
  950. history.PageSize = new Size(model.width, model.height);
  951. }
  952. });
  953. }
  954. #endregion 插入自定义页面
  955. IsLoading = Visibility.Collapsed;
  956. //添加记录到Undo栈,清除Redo栈
  957. history.viewmodel = this;
  958. PDFViewer.UndoManager.AddHistory(history);
  959. }
  960. public string GetInsertFromFile(string file)
  961. {
  962. var fileType = System.IO.Path.GetExtension(file).ToLower();
  963. string fileName = string.Empty;
  964. if (Pdf.Contains(fileType))
  965. {
  966. fileName = file;
  967. }
  968. else if (Image.Contains(fileType))
  969. {
  970. fileName = GetImageFile(file);
  971. }
  972. else
  973. {
  974. fileName = GetOfficeFile(file);
  975. }
  976. if (string.IsNullOrEmpty(fileName))
  977. {
  978. AlertsMessage alertsMessage = new AlertsMessage();
  979. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  980. IsLoading = Visibility.Collapsed;
  981. return fileName;
  982. }
  983. if (File.Exists(fileName) == false)
  984. {
  985. AlertsMessage alertsMessage = new AlertsMessage();
  986. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  987. IsLoading = Visibility.Collapsed;
  988. return fileName;
  989. }
  990. return fileName;
  991. }
  992. private string GetOfficeFile(string sourcepath, Microsoft.Office.Interop.Word.WdPaperSize paperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4, double margin = 0)
  993. {
  994. try
  995. {
  996. string fileName = null;
  997. CPDFViewer pDFViewer = new CPDFViewer();
  998. pDFViewer.CreateDocument();
  999. string targetPath = "";
  1000. targetPath = ConverterHelper.ConvertOfficeToPDF(sourcepath, paperSize, margin);
  1001. if (pDFViewer.Document == null)
  1002. {
  1003. AlertsMessage alertsMessage = new AlertsMessage();
  1004. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1005. return null;
  1006. }
  1007. string file1 = sourcepath.Substring(sourcepath.LastIndexOf("\\") + 1, sourcepath.LastIndexOf(".") - sourcepath.LastIndexOf("\\") - 1) + ".pdf";
  1008. var tempdoc = CPDFDocument.InitWithFilePath(targetPath);
  1009. if (tempdoc == null)
  1010. {
  1011. AlertsMessage alertsMessage = new AlertsMessage();
  1012. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1013. return null;
  1014. }
  1015. pDFViewer.Document.ImportPages(tempdoc, "");
  1016. string tempfile = CacheFilePath.Instance.CreatedFilePath;
  1017. fileName = CommonHelper.CreateFilePath(Path.Combine(tempfile, App.UntitledFileNmae));
  1018. pDFViewer.Document.WriteToFilePath(fileName);
  1019. pDFViewer.CloseDocument();
  1020. pDFViewer.InitDocument(fileName);
  1021. return fileName;
  1022. }
  1023. catch
  1024. {
  1025. string str = "没有安装";
  1026. //判断是否有安装office软件
  1027. var officeType = Type.GetTypeFromProgID("Word.Application");
  1028. if (officeType == null)
  1029. {
  1030. str += "Word;";
  1031. }
  1032. officeType = Type.GetTypeFromProgID("Excel.Application");
  1033. if (officeType == null)
  1034. {
  1035. str += "Excel;";
  1036. }
  1037. officeType = Type.GetTypeFromProgID("Powerpoint.Application");
  1038. if (officeType == null)
  1039. {
  1040. str += "Excel;";
  1041. }
  1042. AlertsMessage alertsMessage = new AlertsMessage();
  1043. if (str != "没有安装")
  1044. {
  1045. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoOffice_Warning"), App.ServiceLoader.GetString("Text_ok"));
  1046. }
  1047. else
  1048. {
  1049. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("CreateFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  1050. }
  1051. return null;
  1052. }
  1053. }
  1054. private string GetImageFile(string fileName)
  1055. {
  1056. CPDFViewer pDFViewer = new CPDFViewer();
  1057. pDFViewer.CreateDocument();
  1058. string path = fileName.Substring(fileName.LastIndexOf("\\") + 1, fileName.LastIndexOf(".") - fileName.LastIndexOf("\\") - 1) + ".pdf";
  1059. System.Drawing.Bitmap pic = new System.Drawing.Bitmap(fileName);
  1060. int width = pic.Size.Width;
  1061. int height = pic.Size.Height;
  1062. string ex = System.IO.Path.GetExtension(fileName);
  1063. //其他格式或者名称中含空格的图片 在本地先转存一下
  1064. if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(fileName, UriKind.Absolute))
  1065. {
  1066. //将其他格式图片转换成jpg
  1067. string folderPath = Path.GetTempPath();
  1068. if (File.Exists(folderPath))
  1069. {
  1070. File.Delete(folderPath);
  1071. }
  1072. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  1073. if (!tempfolder.Exists)
  1074. {
  1075. tempfolder.Create();
  1076. }
  1077. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
  1078. fileName = targetPath;
  1079. pic.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  1080. }
  1081. pic.Dispose();
  1082. var result = pDFViewer.Document.InsertPage(0, width, height, fileName);
  1083. if (!result)
  1084. {
  1085. AlertsMessage alertsMessage = new AlertsMessage();
  1086. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1087. return null;
  1088. }
  1089. string tempfile = CacheFilePath.Instance.CreatedFilePath;
  1090. string file = CommonHelper.CreateFilePath(Path.Combine(tempfile, App.UntitledFileNmae));
  1091. pDFViewer.Document.WriteToFilePath(file);
  1092. pDFViewer.CloseDocument();
  1093. pDFViewer.InitDocument(file);
  1094. return file;
  1095. }
  1096. /// <summary>
  1097. /// 删除选中页面
  1098. /// </summary>
  1099. private async void DeleteCommandEvent()
  1100. {
  1101. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1102. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Delete);
  1103. DataTrackingHelper.IsClearEntryPath = true;
  1104. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Delete");
  1105. AlertsMessage alertsMessage = new AlertsMessage();
  1106. alertsMessage.ShowDialog("", T_DeleteLook, App.ServiceLoader.GetString("Text_no"), App.ServiceLoader.GetString("Text_ok"));
  1107. if (alertsMessage.result == ContentResult.Ok)
  1108. {
  1109. IsLoading = Visibility.Visible;
  1110. await Task.Delay(3);
  1111. int pageIndex = 0;
  1112. int index = 0;
  1113. if (IsMouseRightButtonDown)
  1114. {
  1115. int count = RightSelectedIndexs.Count;
  1116. pageIndex = RightSelectedIndex;
  1117. if (IsMultiSelected)
  1118. {
  1119. index = DoRemoveSelectedPages(RightSelectedIndexs);
  1120. }
  1121. else
  1122. {
  1123. index = DoRemoveSelectedPages(new List<int> { pageIndex });
  1124. }
  1125. }
  1126. else
  1127. {
  1128. index = DoRemoveSelectedPages();
  1129. }
  1130. //删除后 选中项
  1131. if (index != -1)
  1132. {
  1133. if (index <= PDFViewer.Document.PageCount)
  1134. {
  1135. index = index + 1;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. index = 0;
  1141. }
  1142. if (IsBOTAThumb == Visibility.Visible)
  1143. {
  1144. GoToPage(new List<int>() { index });
  1145. }
  1146. ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
  1147. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  1148. NotifyUIToRefresh(new List<int>() { index });
  1149. IsLoading = Visibility.Collapsed;
  1150. }
  1151. }
  1152. /// <summary>
  1153. /// 左旋转页面的逻辑
  1154. /// </summary>
  1155. private void LeftRotateCommandEvent()
  1156. {
  1157. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1158. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Rotate);
  1159. DataTrackingHelper.IsClearEntryPath = true;
  1160. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Rotate");
  1161. List<int> pageitems = new List<int>();
  1162. if (IsMouseRightButtonDown)
  1163. {
  1164. if (IsMultiSelected)
  1165. {
  1166. pageitems.AddRange(RightSelectedIndexs);
  1167. }
  1168. else
  1169. {
  1170. pageitems.Add(RightSelectedIndex);
  1171. }
  1172. RotateSelectedPages(false, pageitems);
  1173. }
  1174. else
  1175. {
  1176. RotateSelectedPages(false);
  1177. }
  1178. }
  1179. /// <summary>
  1180. /// 右旋转页面的逻辑
  1181. /// </summary>
  1182. private void RightRotateCommandEvent()
  1183. {
  1184. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1185. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Rotate);
  1186. DataTrackingHelper.IsClearEntryPath = true;
  1187. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Rotate");
  1188. List<int> pageitems = new List<int>();
  1189. if (IsMouseRightButtonDown)
  1190. {
  1191. if (IsMultiSelected)
  1192. {
  1193. pageitems.AddRange(RightSelectedIndexs);
  1194. }
  1195. else
  1196. {
  1197. pageitems.Add(RightSelectedIndex);
  1198. }
  1199. RotateSelectedPages(true, pageitems);
  1200. }
  1201. else
  1202. {
  1203. RotateSelectedPages();
  1204. }
  1205. }
  1206. /// <summary>
  1207. /// 页面倒序的逻辑
  1208. /// </summary>
  1209. private async void ReverseCommandEvent()
  1210. {
  1211. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1212. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Reverse);
  1213. DataTrackingHelper.IsClearEntryPath = true;
  1214. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Reverse");
  1215. IsLoading = Visibility.Visible;
  1216. await Task.Delay(3);
  1217. List<int> pages = new List<int>();
  1218. for (int i = 0; i < PageEditItems.Count; i++)
  1219. {
  1220. if (PageEditItems[i].Selected)
  1221. {
  1222. pages.Add(i);
  1223. }
  1224. }
  1225. DoReverse(pages);
  1226. IsLoading = Visibility.Collapsed;
  1227. }
  1228. /// <summary>
  1229. /// 将指定页面结合进行倒序
  1230. /// </summary>
  1231. private async void DoReverse(List<int> pages, bool isFromUndoRedo = false)
  1232. {
  1233. if (pages.Count <= 1)
  1234. {
  1235. ShowToast("请至少选择两个图片进行倒序操作。");
  1236. return;
  1237. }
  1238. //付费锁
  1239. if (!ServiceHelper.IAPBeforeFunction())
  1240. {
  1241. return;
  1242. }
  1243. pages.Sort();
  1244. //记录原位置和目标位置 用于undo,redo
  1245. Dictionary<int, int> sourceTarget = new Dictionary<int, int>();
  1246. for (int i = 0; i < pages.Count; i++)
  1247. {
  1248. int preindex = pages[i];
  1249. int laterIndex = pages[pages.Count - i - 1];//尾部的索引
  1250. if (laterIndex <= preindex)//交换完成
  1251. {
  1252. //用于记录选中集合为奇数时,最中间Item的还原
  1253. sourceTarget.Add(preindex, preindex);
  1254. break;
  1255. }
  1256. var result = ExchangeItem(preindex, laterIndex);
  1257. if (!result)
  1258. {
  1259. ShowToast();
  1260. IsLoading = Visibility.Collapsed;
  1261. return;
  1262. }
  1263. sourceTarget.Add(preindex, laterIndex);
  1264. }
  1265. await Task.Delay(20);
  1266. //转换成页码数据集合,传递给UI,选中目标页面
  1267. //因虚拟化加载的问题,通过Selected来控制Item选中效果会有问题
  1268. List<int> pageRange = new List<int>();
  1269. for (int i = 0; i < pages.Count; i++)
  1270. {
  1271. pageRange.Add(pages[i] + 1);
  1272. }
  1273. NotifyUIToRefresh(pageRange);
  1274. ReloadAfterOption(true, false, null);
  1275. if (!isFromUndoRedo)
  1276. {
  1277. PageEditHistory history = new PageEditHistory();
  1278. history.Type = ActionType.Reverse;
  1279. history.SourceTargetValue = sourceTarget;
  1280. history.viewmodel = this;
  1281. PDFViewer.UndoManager.AddHistory(history);
  1282. }
  1283. }
  1284. /// <summary>
  1285. /// 交换两个Item顺序
  1286. /// </summary>
  1287. /// <param name="preiIndex"></param>
  1288. /// <param name="laterIndex"></param>
  1289. /// <returns></returns>
  1290. private bool ExchangeItem(int preiIndex, int laterIndex)
  1291. {
  1292. if (preiIndex == laterIndex)
  1293. {
  1294. return true;
  1295. }
  1296. var sourceitem = PageEditItems[preiIndex];
  1297. var targetitem = PageEditItems[laterIndex];
  1298. var result = PDFViewer.Document.ExchangePage(preiIndex, laterIndex);
  1299. if (!result)
  1300. {
  1301. return false;
  1302. }
  1303. PageEditItems.Remove(sourceitem);
  1304. PageEditItems.Remove(targetitem);
  1305. PageEditItems.Insert(preiIndex, targetitem);
  1306. PageEditItems.Insert(laterIndex, sourceitem);
  1307. return true;
  1308. }
  1309. /// <summary>
  1310. /// 是否可以执行文件操作命令 除插入、拆分外
  1311. /// </summary>
  1312. /// <returns></returns>
  1313. private bool CanFileCommandExcute()
  1314. {
  1315. //增加是否有选中多页的判断
  1316. //修复问题:全选页面后,ctrl取消第一页的选中时,其余操作会因为ListSelectedIndex为-1被禁用的问题
  1317. if (IsMouseRightButtonDown)
  1318. {
  1319. if (RightSelectedIndex < 0 && !IsMultiSelected)
  1320. {
  1321. return false;
  1322. }
  1323. else
  1324. {
  1325. return true;
  1326. }
  1327. }
  1328. else
  1329. {
  1330. if (ListSelectedIndex < 0 && !IsMultiSelected)
  1331. {
  1332. return false;
  1333. }
  1334. else
  1335. {
  1336. return true;
  1337. }
  1338. }
  1339. }
  1340. private bool CanReverseFileCommandExcute()
  1341. {
  1342. if (IsMouseRightButtonDown)
  1343. {
  1344. if (RightSelectedIndex >= 0 && IsMultiSelected)
  1345. {
  1346. return true;
  1347. }
  1348. else
  1349. {
  1350. return false;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if (ListSelectedIndex >= 0 && IsMultiSelected)
  1356. {
  1357. return true;
  1358. }
  1359. else
  1360. {
  1361. return false;
  1362. }
  1363. }
  1364. }
  1365. /// <summary>
  1366. /// 校验是否可以替换
  1367. /// </summary>
  1368. /// <returns></returns>
  1369. private bool CanReplaceCommandExcute()
  1370. {
  1371. //return !IsMultiSelected;
  1372. return true;
  1373. }
  1374. /// <summary>
  1375. /// 放大页面
  1376. /// </summary>
  1377. private void ZoomInCommandEvent()
  1378. {
  1379. ZoomIndex++;
  1380. ChangeItemSize(ItemSizes[ZoomIndex + 1]);
  1381. NotifyUIToRefresh();
  1382. }
  1383. /// <summary>
  1384. /// 缩小页面
  1385. /// </summary>
  1386. private async void ZoomOutCommandEvent()
  1387. {
  1388. ZoomIndex--;
  1389. ChangeItemSize(ItemSizes[ZoomIndex + 1]);
  1390. //增加一定延时,计算页面显示范围时,用新的尺寸进行计算
  1391. await Task.Delay(3);
  1392. NotifyUIToRefresh();
  1393. }
  1394. /// <summary>
  1395. ///
  1396. /// </summary>
  1397. /// <param name="newsize"></param>
  1398. public void ChangeItemSize(Size newsize)
  1399. {
  1400. for (int i = 0; i < PageEditItems.Count; i++)
  1401. {
  1402. PageEditItems[i].ItemSize = newsize;
  1403. }
  1404. RefreshPageNum();
  1405. List<int> indexList = GetSelectedItemPageNumbers();
  1406. if (indexList.Count > 0)
  1407. {
  1408. //选中项集合 也需要 同步更新
  1409. selectedPageList.Clear();
  1410. selectedPageList.AddRange(indexList);
  1411. }
  1412. if (selectedPageList.Count > 0)
  1413. {
  1414. //缩略图模式 调整item大小的时候,更新选中项
  1415. //如果注释掉,调整BOTA宽度后,缩略图视图中不会显示选中项
  1416. if (selectedPageList.Count < 1)
  1417. {
  1418. NotifyUIToRefresh(new List<int>() { PDFViewer.CurrentIndex + 1 });
  1419. }
  1420. else
  1421. {
  1422. NotifyUIToRefresh(selectedPageList);
  1423. }
  1424. }
  1425. }
  1426. private bool CanZoomIn()
  1427. {
  1428. if (ZoomIndex >= 1)
  1429. {
  1430. return false;
  1431. }
  1432. else
  1433. {
  1434. return true;
  1435. }
  1436. }
  1437. private bool CanZoomOut()
  1438. {
  1439. if (ZoomIndex <= -1)
  1440. {
  1441. return false;
  1442. }
  1443. else
  1444. {
  1445. return true;
  1446. }
  1447. }
  1448. /// <summary>
  1449. /// 响应UI发送刷新图片的事件
  1450. /// </summary>
  1451. /// <param name="e"></param>
  1452. private void OnPageEditRefreshEvent(PageEditRefreshEventArgs e)
  1453. {
  1454. //UI发送事件过来通知刷新图片时
  1455. //调整逻辑,前后预加载5页
  1456. RefreshItemImage(e.PageRange.Item1 - 2, e.PageRange.Item2 + 2);
  1457. }
  1458. /// <summary>
  1459. /// 选项改变事件
  1460. /// 虚拟化的时候不会触发选中事件
  1461. /// </summary>
  1462. /// <param name="e"></param>
  1463. private void SelectionChangedEvent(object e)
  1464. {
  1465. if (IsSelectFormSelf)
  1466. {
  1467. //避免频繁的触发该事件
  1468. return;
  1469. }
  1470. var args = e as SelectionChangedEventArgs;
  1471. //下下策 从UI控件获取所有选项的集合
  1472. var listbox = args.OriginalSource as ListBox;
  1473. if (args != null)
  1474. {
  1475. selectedPageList = new List<int>();
  1476. for (int i = 0; i < listbox.SelectedItems.Count; i++)
  1477. {
  1478. if (!selectedPageList.Contains((listbox.SelectedItems[i] as PageEditItem).PageNumber))
  1479. {
  1480. selectedPageList.Add((listbox.SelectedItems[i] as PageEditItem).PageNumber);
  1481. }
  1482. }
  1483. ////根据选中的范围 适配全部页面范围、奇数页面范围等
  1484. //if (pagelist.Count == PageEditItems.Count)
  1485. //{
  1486. // ComboxSelectIndex = 0;
  1487. // return;
  1488. //}
  1489. //只选中单个页面是,listselectedIndex 更准确
  1490. if (selectedPageList.Count == 1)
  1491. {
  1492. TbPageRange = (listSelecedIndex + 1).ToString();
  1493. //缩放 平移移动页面,页面大小出现不全的问题
  1494. listbox.ScrollIntoView(listbox.SelectedItems[0]);
  1495. //需要刷新 startRefreshIndex 、endRefreshedIndex
  1496. //这个方法 会有串联地方,然后不起作用
  1497. //NotifyUIToRefresh();
  1498. //保险起见,暂时只能用这个方法
  1499. if (listbox != null)
  1500. {
  1501. var itemSize = new Size((listbox.Items[0] as PageEditItem).ItemSize.Width + 32, (listbox.Items[0] as PageEditItem).ItemSize.Height + 30);
  1502. Tuple<int, int, int> range = (Tuple<int, int, int>)GetRoughFromView(listbox, itemSize, new Thickness(5, 10, 5, 10));
  1503. //RefreshItemImage(range.Item1, range.Item2);
  1504. startRefreshedIndex = range.Item1;
  1505. endRefreshedIndex = range.Item2;
  1506. }
  1507. }
  1508. else
  1509. {
  1510. //根据选中的页面转换成1,3-10类似的字符串
  1511. string pageparm = CommonHelper.GetPageParmFromList(selectedPageList);
  1512. TbPageRange = pageparm;
  1513. }
  1514. }
  1515. //Ctrl+A时让所有item选中
  1516. var isCtrlorShiftDown = ((Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.A)) || (Keyboard.IsKeyDown(Key.RightCtrl) && Keyboard.IsKeyDown(Key.A)));
  1517. if (isCtrlorShiftDown)
  1518. {
  1519. for (int i = 0; i < PageEditItems.Count; i++)
  1520. {
  1521. PageEditItems[i].Selected = true;
  1522. }
  1523. }
  1524. //判断是否有选中多项
  1525. if (listbox.SelectedItems.Count > 1)
  1526. {
  1527. IsMultiSelected = true;
  1528. maxSelectedIndex = listbox.Items.IndexOf(listbox.SelectedItems[listbox.SelectedItems.Count - 1]);
  1529. }
  1530. else
  1531. {
  1532. IsMultiSelected = false;
  1533. //非多选的时候需要重置多选页码的标志符
  1534. //否则会有多选页面复制后,粘贴位置不准确的问题
  1535. maxSelectedIndex = -1;
  1536. }
  1537. }
  1538. private object GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  1539. {
  1540. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  1541. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  1542. return new Tuple<int, int, int>(0, 0, 0);
  1543. try
  1544. {
  1545. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  1546. var currentWidth = scrollViewer.ActualWidth;
  1547. //计算当前窗口大小能显示的行数和列数
  1548. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  1549. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  1550. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  1551. preItemCount = preItemCount * columnCount;
  1552. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  1553. preEnd = preEnd * columnCount + columnCount - 1;
  1554. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  1555. return new Tuple<int, int, int>(
  1556. Math.Max(preItemCount, 0),
  1557. Math.Min(view.Items.Count, preEnd),
  1558. middle);
  1559. }
  1560. catch { }
  1561. return new Tuple<int, int, int>(0, 0, 0);
  1562. }
  1563. /// <summary>
  1564. /// 限制文本框输入内容
  1565. /// </summary>
  1566. /// <param name="e"></param>
  1567. private void PreviewKeyDownEvent(object e)
  1568. {
  1569. var args = e as KeyEventArgs;
  1570. if (args == null)
  1571. {
  1572. return;
  1573. }
  1574. ////枚举 过滤所有不合理的输入
  1575. //if (args.Key != Key.Enter && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
  1576. //{
  1577. // args.Handled = true;
  1578. //}
  1579. if (args.Key == Key.Enter)
  1580. {
  1581. if (PageRangeItems.Contains((args.OriginalSource as TextBox).Text))
  1582. {
  1583. return;
  1584. }
  1585. List<int> TargetPageIndexs = new List<int>();
  1586. var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
  1587. if (!result)
  1588. {
  1589. ListSelectedIndex = -1;
  1590. TbPageRange = string.Empty;
  1591. AlertsMessage alertsMessage = new AlertsMessage();
  1592. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  1593. return;
  1594. }
  1595. //选中输入的页面范围
  1596. List<int> pageRange = new List<int>();
  1597. for (int i = 0; i < PageEditItems.Count; i++)
  1598. {
  1599. if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
  1600. {
  1601. pageRange.Add(i + 1);
  1602. }
  1603. }
  1604. NotifyUIToRefresh(pageRange);
  1605. }
  1606. }
  1607. /// <summary>
  1608. /// 失去焦点后选中页面
  1609. /// </summary>
  1610. /// <param name="obj"></param>
  1611. public void lostfocus(object obj)
  1612. {
  1613. var args = obj as RoutedEventArgs;
  1614. if (args == null || (args.OriginalSource as TextBox) == null)
  1615. {
  1616. return;
  1617. }
  1618. List<int> TargetPageIndexs = new List<int>();
  1619. if (PageRangeItems.Contains((args.OriginalSource as TextBox).Text) && ComboxSelectIndex != 5)
  1620. {
  1621. //选中其他项,失去焦点时不报错
  1622. return;
  1623. }
  1624. var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
  1625. if (!result)
  1626. {
  1627. ListSelectedIndex = -1;
  1628. TbPageRange = string.Empty;
  1629. AlertsMessage alertsMessage = new AlertsMessage();
  1630. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  1631. return;
  1632. }
  1633. //选中输入的页面范围
  1634. List<int> pageRange = new List<int>();
  1635. for (int i = 0; i < PageEditItems.Count; i++)
  1636. {
  1637. if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
  1638. {
  1639. pageRange.Add(i + 1);
  1640. }
  1641. }
  1642. NotifyUIToRefresh(pageRange);
  1643. }
  1644. #endregion 事件
  1645. #region 方法
  1646. /// <summary>
  1647. /// Esc退出多选
  1648. /// </summary>
  1649. private void clearSelected()
  1650. {
  1651. ListSelectedIndex = -1;
  1652. }
  1653. /// <summary>
  1654. /// 调用打印机
  1655. /// </summary>
  1656. public async void print(List<int> SelectedPages)
  1657. {
  1658. //保存并继续,使用了高级功能则弹窗权益弹窗
  1659. if (!viewContentViewModel.saveFile(true))
  1660. {
  1661. return;
  1662. }
  1663. if (ListSelectedIndex != -1)
  1664. {
  1665. await App.Current.Dispatcher.InvokeAsync(() =>
  1666. {
  1667. PDFViewer.GoToPage(ListSelectedIndex);
  1668. });
  1669. }
  1670. DialogParameters printValue = new DialogParameters();
  1671. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  1672. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  1673. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  1674. printValue.Add(ParameterNames.PageList, SelectedPages);
  1675. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  1676. }
  1677. /// <summary>
  1678. /// 剪切
  1679. /// </summary>
  1680. private void cut()
  1681. {
  1682. //没有权限时,拦截剪切操作
  1683. if (!viewContentViewModel.CheckPermissionBeforeOption())
  1684. {
  1685. return;
  1686. }
  1687. CacheFilePath.Instance.CopyDoc = null;
  1688. CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
  1689. List<int> pages = new List<int>();
  1690. for (int i = 0; i < PageEditItems.Count; i++)
  1691. {
  1692. if (PageEditItems[i].Selected)
  1693. {
  1694. pages.Add(i + 1);
  1695. }
  1696. }
  1697. bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
  1698. if (!result)
  1699. {
  1700. ShowToast();
  1701. return;
  1702. }
  1703. List<int> pageitems = new List<int>();
  1704. if (IsMouseRightButtonDown)
  1705. {
  1706. if (IsMultiSelected)
  1707. {
  1708. pageitems.AddRange(RightSelectedIndexs);
  1709. }
  1710. else
  1711. {
  1712. pageitems.Add(RightSelectedIndex);
  1713. }
  1714. DoRemoveSelectedPages(pageitems);
  1715. }
  1716. else
  1717. {
  1718. DoRemoveSelectedPages();
  1719. }
  1720. ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
  1721. }
  1722. /// <summary>
  1723. /// 复制
  1724. /// </summary>
  1725. private void copy()
  1726. {
  1727. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1728. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Copy);
  1729. DataTrackingHelper.IsClearEntryPath = true;
  1730. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Copy");
  1731. if (!ServiceHelper.IAPBeforeFunction())
  1732. {
  1733. return;
  1734. }
  1735. if (viewContentViewModel.CheckPermissionBeforeOption())
  1736. {
  1737. int pageIndex = 0;
  1738. if (IsMouseRightButtonDown)
  1739. {
  1740. pageIndex = RightSelectedIndex;
  1741. }
  1742. else
  1743. {
  1744. pageIndex = ListSelectedIndex;
  1745. }
  1746. CacheFilePath.Instance.CopyDoc?.Release();
  1747. CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
  1748. List<int> pages = new List<int>();
  1749. if (isMultiSelected)
  1750. {
  1751. //确认是多选了页面时,通过页面的选中状态获取选中页面
  1752. for (int i = 0; i < PageEditItems.Count; i++)
  1753. {
  1754. if (PageEditItems[i].Selected)
  1755. {
  1756. pages.Add(i + 1);
  1757. }
  1758. }
  1759. }
  1760. else
  1761. {
  1762. //没有多选时,根据listbox的选中索引获取选中页面
  1763. pages.Add(pageIndex + 1);
  1764. }
  1765. bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
  1766. if (!result)
  1767. {
  1768. ShowToast();
  1769. return;
  1770. }
  1771. }
  1772. }
  1773. /// <summary>
  1774. /// 是否可以粘贴
  1775. /// </summary>
  1776. /// <returns></returns>
  1777. private bool CanPasteExcute()
  1778. {
  1779. return this.CanPaste;
  1780. }
  1781. /// <summary>
  1782. /// 粘贴
  1783. /// </summary>
  1784. private void Paste(int Index = -1)
  1785. {
  1786. if (!ServiceHelper.IAPBeforeFunction())
  1787. {
  1788. return;
  1789. }
  1790. int pageIndex = 0;
  1791. if (IsMouseRightButtonDown)
  1792. {
  1793. pageIndex = RightSelectedIndex;
  1794. }
  1795. else
  1796. {
  1797. pageIndex = ListSelectedIndex;
  1798. }
  1799. IsLoading = Visibility.Visible;
  1800. int insertindex = maxSelectedIndex > pageIndex ? maxSelectedIndex : pageIndex;
  1801. insertindex++;
  1802. if (Index >= 0)
  1803. {
  1804. insertindex = Index;
  1805. }
  1806. else if (pageIndex == -1)
  1807. {
  1808. insertindex = PageEditItems.Count;
  1809. }
  1810. if (CacheFilePath.Instance.CopyDoc == null)
  1811. {
  1812. return;
  1813. }
  1814. int pagecount = CacheFilePath.Instance.CopyDoc.PageCount;
  1815. bool result = PDFViewer.Document.ImportPagesAtIndex(CacheFilePath.Instance.CopyDoc, "1-" + pagecount, insertindex);
  1816. if (!result)
  1817. {
  1818. IsLoading = Visibility.Collapsed;
  1819. ShowToast();
  1820. return;
  1821. }
  1822. //UI层插入
  1823. for (int i = 0; i < pagecount; i++)
  1824. {
  1825. var item = GetNewPageEditItem(i);
  1826. PageEditItems.Insert(insertindex + i, item);
  1827. }
  1828. //加一点延时才能显示选中效果-2023.4.12,为解决卡顿问题,注释掉,注释掉未出现该选中问题
  1829. //await Task.Delay(200);
  1830. List<int> pageRange = new List<int>();
  1831. for (int i = 0; i < pagecount; i++)
  1832. {
  1833. pageRange.Add(insertindex + i + 1);
  1834. }
  1835. PageEditHistory history = new PageEditHistory();
  1836. history.InsertIndex = insertindex;
  1837. history.InsertPageCount = pagecount;
  1838. history.Type = ActionType.Paste;
  1839. history.viewmodel = this;
  1840. PDFViewer.UndoManager.AddHistory(history);
  1841. if (IsBOTAThumb == Visibility.Visible)
  1842. {
  1843. GoToPage(pageRange);
  1844. }
  1845. //刷新页码等
  1846. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1847. //通知Xaml选中新增的页面,从VM设置选中项会有问题
  1848. NotifyUIToRefresh(pageRange);
  1849. IsLoading = Visibility.Collapsed;
  1850. IsAddPages = true;
  1851. }
  1852. public void GoToPage(List<int> pageRange)
  1853. {
  1854. int index = pageRange.Last() - 1;
  1855. //跳转页面 需要写在ReloadAfterOption()之前,不然跳转失败的概率大
  1856. //插入页面需要重新加载 ,不然会有bug(bug:选中最后 一页,粘贴,跳转失败)
  1857. PDFViewer.ReloadDocument();
  1858. //await Task.Delay(500);
  1859. PDFViewer.GoToPage(index);
  1860. }
  1861. /// <summary>
  1862. /// 页面编辑执行Undo操作的方法
  1863. /// </summary>
  1864. public async void Undo(PageEditHistory history)
  1865. {
  1866. switch (history.Type)
  1867. {
  1868. case ActionType.RightRotate:
  1869. var rightlist = history.SourceTargetValue.Keys.ToList<int>();
  1870. RotateSelectedPages(false, rightlist, true);
  1871. break;
  1872. case ActionType.LeftRotate:
  1873. var leftlist = history.SourceTargetValue.Keys.ToList<int>();
  1874. RotateSelectedPages(true, leftlist, true);
  1875. break;
  1876. case ActionType.InsertFromFile:
  1877. case ActionType.Paste:
  1878. List<int> pageList = new List<int>();
  1879. for (int i = 0; i < history.InsertPageCount; i++)
  1880. {
  1881. pageList.Add(history.InsertIndex + i);
  1882. }
  1883. int index = DoRemoveSelectedPages(pageList, true);
  1884. UndoPageOperations(index);
  1885. break;
  1886. case ActionType.InsertCustomPages:
  1887. index = DoRemoveSelectedPages(new List<int> { history.InsertIndex }, true);
  1888. UndoPageOperations(index);
  1889. break;
  1890. case ActionType.DragSort:
  1891. //将字典倒序后,将每个Source的值恢复到Key的位置
  1892. var SourceTargets = history.SourceTargetValue;
  1893. var keys = SourceTargets.Keys.ToArray();
  1894. Array.Reverse(keys);
  1895. foreach (var key in keys)
  1896. {
  1897. MoveItemToIndex(SourceTargets[key], key);
  1898. }
  1899. //刷新页码等
  1900. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1901. List<int> selectedPages = new List<int>();
  1902. //将索引集合转换成页码集合
  1903. foreach (var key in SourceTargets.Keys)
  1904. {
  1905. selectedPages.Add(key + 1);
  1906. }
  1907. //通知UI选中Item
  1908. NotifyUIToRefresh(selectedPages);
  1909. break;
  1910. case ActionType.Reverse:
  1911. //将字典倒序后,将每个Source的值恢复到Key的位置
  1912. var SourceTarget = history.SourceTargetValue;
  1913. var pres = SourceTarget.Keys.ToArray();
  1914. foreach (var key in pres)
  1915. {
  1916. if (key != SourceTarget[key])
  1917. {
  1918. ExchangeItem(key, SourceTarget[key]);
  1919. }
  1920. }
  1921. await Task.Delay(20);
  1922. //刷新页码等
  1923. ReloadAfterOption(true, false, null);
  1924. List<int> Pages = new List<int>();
  1925. //将索引集合转换成页码集合
  1926. foreach (var key in SourceTarget.Keys)
  1927. {
  1928. Pages.Add(key + 1);
  1929. }
  1930. foreach (var key in SourceTarget.Values)
  1931. {
  1932. Pages.Add(key + 1);
  1933. }
  1934. //通知UI选中Item
  1935. NotifyUIToRefresh(Pages);
  1936. break;
  1937. default:
  1938. break;
  1939. }
  1940. }
  1941. private void UndoPageOperations(int index)
  1942. {
  1943. //【页面编辑】插入页面后,undo,页码更新
  1944. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1945. //删除后 选中项
  1946. if (index != -1)
  1947. {
  1948. if (index <= PDFViewer.Document.PageCount)
  1949. {
  1950. index = index + 1;
  1951. }
  1952. }
  1953. else
  1954. {
  1955. index = 0;
  1956. }
  1957. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  1958. NotifyUIToRefresh(new List<int>() { index });
  1959. }
  1960. /// <summary>
  1961. /// 页面编辑执行Redo操作的方法
  1962. /// </summary>
  1963. public async void Redo(PageEditHistory history)
  1964. {
  1965. switch (history.Type)
  1966. {
  1967. case ActionType.RightRotate:
  1968. var rightlist = history.SourceTargetValue.Keys.ToList<int>();
  1969. RotateSelectedPages(true, rightlist, true);
  1970. break;
  1971. case ActionType.LeftRotate:
  1972. var leftlist = history.SourceTargetValue.Keys.ToList<int>();
  1973. RotateSelectedPages(false, leftlist, true);
  1974. break;
  1975. case ActionType.InsertFromFile:
  1976. //将文档验证是否为空,是否解密,置前,若为空直接退出
  1977. bool IsLocked = GetDocumentIsDiscryptied(history.InsertFilePath, history.InsertFilePassWord, out CPDFDocument doc, out string password);
  1978. if (IsLocked == false)
  1979. {
  1980. return;
  1981. }
  1982. await InsertFromFile(history.InsertIndex, history.InsertFilePath, doc, history.InsertFilePassWord, true);
  1983. ReloadAfterOption(true, false, Tuple.Create(history.InsertIndex - 1, history.InsertIndex + history.InsertPageCount));
  1984. break;
  1985. case ActionType.InsertCustomPages:
  1986. InsertFromCustomPage(history.InsertIndex, history.PageSize.Width, history.PageSize.Height, history.InsertFilePath);
  1987. ListSelectedIndex = history.InsertIndex;
  1988. break;
  1989. case ActionType.DragSort:
  1990. DragToSort(history.InsertIndex, history.SourceTargetValue.Keys.ToList<int>(), true);
  1991. break;
  1992. case ActionType.Reverse:
  1993. var list = history.SourceTargetValue.Keys.ToList<int>().Union(history.SourceTargetValue.Values.ToList<int>()).ToList<int>();
  1994. DoReverse(list, true);
  1995. break;
  1996. case ActionType.Paste:
  1997. Paste(history.InsertIndex);
  1998. break;
  1999. default:
  2000. break;
  2001. }
  2002. }
  2003. /// <summary>
  2004. /// 退出页面编辑面试,回到阅读页
  2005. /// </summary>
  2006. public void BackToPDFViewer(int PageIndex)
  2007. {
  2008. PDFViewer.GoToPage(PageIndex);
  2009. viewContentViewModel.ExitToolMode();
  2010. }
  2011. /// <summary>
  2012. /// 显示Toast提示
  2013. /// </summary>
  2014. public void ShowToast(string content = "")
  2015. {
  2016. if (!string.IsNullOrEmpty(content))
  2017. {
  2018. ToastText = content;
  2019. }
  2020. else
  2021. {
  2022. ToastText = App.MainPageLoader.GetString("OpentonFail_Warning");
  2023. }
  2024. ShowTip = Visibility.Visible;
  2025. }
  2026. /// <summary>
  2027. /// 通知UI刷新页面范围
  2028. /// 或通过xaml来设置选中项
  2029. /// </summary>vb nmjvcx
  2030. private void NotifyUIToRefresh(List<int> pageRange = null)
  2031. {
  2032. if (pageRange == null || pageRange.Count <= 0)
  2033. {
  2034. //通知UI刷新页面范围
  2035. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  2036. }
  2037. else
  2038. {
  2039. //通知UI刷新页面范围
  2040. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode, NotifyType.SelectItems, pageRange));
  2041. }
  2042. }
  2043. /// <summary>
  2044. /// 获取已选中的页面集合
  2045. /// </summary>
  2046. /// <returns></returns>
  2047. private List<int> getSelectedPage()
  2048. {
  2049. List<int> pages = new List<int>();
  2050. for (int i = 0; i < PageEditItems.Count; i++)
  2051. {
  2052. if (PageEditItems[i].Selected)
  2053. {
  2054. pages.Add(i);
  2055. }
  2056. }
  2057. return pages;
  2058. }
  2059. /// <summary>
  2060. /// 删除选中项的方法 不带Reload操作
  2061. /// 方便其他逻辑中调用
  2062. /// </summary>
  2063. private int DoRemoveSelectedPages(List<int> pageList = null, bool isFromUndoRedo = false, bool isReplace = false)
  2064. {
  2065. PageEditItem pageEditItem = null;
  2066. int index = -1;
  2067. if (!ServiceHelper.IAPBeforeFunction())
  2068. {
  2069. return index;
  2070. }
  2071. //删除后 的选中项
  2072. index = maxSelectedIndex;
  2073. List<int> indexList = new List<int>();
  2074. if (pageList == null || pageList.Count == 0)
  2075. {
  2076. for (int i = 0; i < PageEditItems.Count; i++)
  2077. {
  2078. if (PageEditItems[i].Selected)
  2079. {
  2080. indexList.Add(i);
  2081. }
  2082. }
  2083. //因为虚拟化 可能获取不到所有选中项的页码 需要结合选中项的集合判断一下
  2084. if (selectedPageList.Count > 0 && selectedPageList.Count > indexList.Count)
  2085. {
  2086. indexList = new List<int>();
  2087. for (int j = 0; j < selectedPageList.Count; j++)
  2088. {
  2089. indexList.Add(selectedPageList[j] - 1);
  2090. }
  2091. }
  2092. }
  2093. else
  2094. {
  2095. indexList = pageList;
  2096. }
  2097. if (indexList.Count > 0 && indexList.Count == PDFViewer.Document.PageCount)
  2098. {
  2099. // 全部替换 不显示 不能删除所有的弹窗
  2100. if (isReplace == false)
  2101. {
  2102. AlertsMessage alertsMessage = new AlertsMessage();
  2103. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageEdit_DeleteAllPage"), App.ServiceLoader.GetString("Text_ok"));
  2104. return -1;
  2105. }
  2106. }
  2107. bool isLast = false;
  2108. if (index == -1 && indexList.Count > 0)
  2109. {
  2110. if (isMultiSelected)
  2111. {
  2112. indexList.Sort();
  2113. index = indexList.First();
  2114. }
  2115. else
  2116. {
  2117. index = indexList.Last();
  2118. }
  2119. }
  2120. if (index == PageEditItems.Count)
  2121. {
  2122. pageEditItem = PageEditItems[index - 1];
  2123. }
  2124. else if (index < PageEditItems.Count)
  2125. {
  2126. if ((index + 1) >= PageEditItems.Count)
  2127. {
  2128. pageEditItem = PageEditItems[PageEditItems.Count - 1];
  2129. isLast = true;
  2130. }
  2131. else
  2132. {
  2133. pageEditItem = PageEditItems[index + 1];
  2134. }
  2135. }
  2136. var result = PDFViewer.Document.RemovePages(indexList.ToArray());
  2137. if (!result)
  2138. {
  2139. ShowToast();
  2140. return -1;
  2141. }
  2142. //文档层操作成功后再删除UI层
  2143. for (int i = indexList.Count - 1; i >= 0; i--)
  2144. {
  2145. PageEditItems.RemoveAt(indexList[i]);
  2146. }
  2147. if (!isFromUndoRedo)
  2148. {
  2149. ////清除页面编辑Undo,Redo栈
  2150. //UndoStack.Clear();
  2151. //RedoStack.Clear();
  2152. //this.CanUndo = false;
  2153. //this.CanRedo = false;
  2154. }
  2155. index = PageEditItems.IndexOf(pageEditItem);
  2156. if (index == -1 && indexList.Count > 0)
  2157. {
  2158. if (isLast)
  2159. {
  2160. index = PageEditItems.Count - 1;
  2161. }
  2162. else
  2163. {
  2164. index = indexList.Last();
  2165. }
  2166. }
  2167. return index;
  2168. }
  2169. /// <summary>
  2170. /// 提取选中页面的方法
  2171. /// </summary>
  2172. /// <param name="isEveryPageToFile">是否每页生成一个文件</param>
  2173. /// <param name="isDeleteAfterExtract">是否提取后删除页面</param>
  2174. private async void DoExtractPages(bool isEveryPageToFile, bool isDeleteAfterExtract)
  2175. {
  2176. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  2177. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  2178. {
  2179. if (string.IsNullOrEmpty(dialog.SelectedPath))
  2180. {
  2181. AlertsMessage alertsMessage = new AlertsMessage();
  2182. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("OperationFailWarning"), App.ServiceLoader.GetString("Text_ok"));//补充提示文案
  2183. return;
  2184. }
  2185. }
  2186. else
  2187. return;
  2188. string selectedfile = "";
  2189. IsLoading = Visibility.Visible;
  2190. await Task.Delay(3);
  2191. List<int> pagenums = new List<int>();
  2192. for (int i = 0; i < PageEditItems.Count; i++)
  2193. {
  2194. if (PageEditItems[i].Selected)
  2195. {
  2196. pagenums.Add(PageEditItems[i].PageNumber);
  2197. }
  2198. }
  2199. if (IsMouseRightButtonDown)
  2200. {
  2201. if (IsMultiSelected)
  2202. {
  2203. for (int i = 0; i < RightSelectedIndexs.Count; i++)
  2204. {
  2205. RightSelectedIndexs[i] = RightSelectedIndexs[i] + 1;
  2206. }
  2207. pagenums = RightSelectedIndexs;
  2208. }
  2209. else
  2210. {
  2211. pagenums = new List<int>() { RightSelectedIndex + 1 };
  2212. }
  2213. }
  2214. if (isEveryPageToFile)
  2215. {
  2216. //每页生成的单独的文件
  2217. for (int i = 0; i < pagenums.Count; i++)
  2218. {
  2219. string filename = PDFViewer.Document.FileName + " " + "pages " + pagenums[i] + ".pdf";
  2220. string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
  2221. path = CommonHelper.CreateFilePath(path);
  2222. selectedfile = path;
  2223. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2224. bool result = savedoc.ImportPages(PDFViewer.Document, pagenums[i].ToString());
  2225. if (!result)
  2226. {
  2227. savedoc.Release();
  2228. continue;
  2229. }
  2230. result = savedoc.WriteToFilePath(path);
  2231. if (!result)
  2232. {
  2233. savedoc.Release();
  2234. continue;
  2235. }
  2236. savedoc.Release();//释放内存
  2237. }
  2238. }
  2239. else//全部生成一个文件
  2240. {
  2241. string filename = PDFViewer.Document.FileName + " " + "pages " + CommonHelper.GetPageParmFromList(pagenums) + ".pdf";
  2242. string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
  2243. path = CommonHelper.CreateFilePath(path);
  2244. selectedfile = path;
  2245. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2246. string range = String.Join(",", pagenums);
  2247. bool result = savedoc.ImportPages(PDFViewer.Document, range);
  2248. if (!result)
  2249. {
  2250. ShowToast();
  2251. IsLoading = Visibility.Collapsed;
  2252. return;
  2253. }
  2254. result = savedoc.WriteToFilePath(path);
  2255. if (!result)
  2256. {
  2257. ShowToast();
  2258. }
  2259. savedoc.Release();//释放内存
  2260. }
  2261. if (isDeleteAfterExtract)
  2262. {
  2263. //先提取后删除,index和其他地方设置不一样
  2264. int index = DoRemoveSelectedPages() + 1;
  2265. if (index > PDFViewer.Document.PageCount)
  2266. {
  2267. index = PDFViewer.Document.PageCount;
  2268. }
  2269. ReloadAfterOption(true, false, Tuple.Create(0, PDFViewer.Document.PageCount));
  2270. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  2271. NotifyUIToRefresh(new List<int>() { index });
  2272. }
  2273. CommonHelper.ShowFileBrowser(selectedfile);
  2274. IsLoading = Visibility.Collapsed;
  2275. }
  2276. private async void DoSplitPages(HomePageSplitDialogModel model)
  2277. {
  2278. try
  2279. {
  2280. var pdfViewer = PDFViewer;
  2281. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  2282. //dialog.Description = App.MainPageLoader.GetString("Main_OpenFolderNoteWarning"); ;
  2283. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  2284. {
  2285. if (string.IsNullOrEmpty(dialog.SelectedPath))
  2286. {
  2287. return;
  2288. }
  2289. }
  2290. else
  2291. return;
  2292. var data = model;
  2293. int PageCount = pdfViewer.Document.PageCount;
  2294. string selectedfile = "";
  2295. string directory = System.IO.Path.Combine(dialog.SelectedPath, pdfViewer.Document.FileName);
  2296. if (Directory.Exists(directory) == false)
  2297. {
  2298. Directory.CreateDirectory(directory);
  2299. }
  2300. App.mainWindowViewModel.ProcessCloseAction = PageSplitClear;
  2301. string filename = "";
  2302. int index = 0;
  2303. if (data.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
  2304. {
  2305. int perPages = Convert.ToInt32(data.GetModeCount);
  2306. int splitCount = (int)Math.Ceiling((double)PageCount / (double)perPages);
  2307. index = splitCount;
  2308. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2309. //TODO:命名规则是否要调整
  2310. selectedfile = data.FileName + " " + "1.pdf";
  2311. for (int i = 0; i < splitCount; i++)
  2312. {
  2313. App.mainWindowViewModel.Value = i;
  2314. await Task.Delay(50); // 模拟长时间操作
  2315. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2316. {
  2317. return;
  2318. }
  2319. int startNum = i * perPages + 1;
  2320. int endNum = Math.Min(PageCount, (i + 1) * perPages);
  2321. string range = startNum.ToString() + "-" + endNum.ToString();
  2322. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2323. bool result = savedoc.ImportPages(pdfViewer.Document, range);
  2324. if (!result)
  2325. {
  2326. savedoc.Release();
  2327. continue;
  2328. }
  2329. filename = GetFileName(data, i);
  2330. string path = System.IO.Path.Combine(directory, filename);
  2331. path = CommonHelper.CreateFilePath(path);
  2332. result = savedoc.WriteToFilePath(path);
  2333. if (!result)
  2334. {
  2335. savedoc.Release();
  2336. continue;
  2337. }
  2338. savedoc.Release();
  2339. selectedfile = path;
  2340. }
  2341. }
  2342. else if (data.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
  2343. {
  2344. int docCount = Convert.ToInt32(data.GetModeCount);
  2345. int avePages = (int)Math.Floor((double)PageCount / (double)docCount);
  2346. index = avePages;
  2347. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2348. int remain = PageCount % docCount;
  2349. int startNum = 1;
  2350. //TODO:命名规则是否要调整
  2351. selectedfile = data.FileName + " " + "1.pdf";
  2352. for (int i = 0; i < docCount; i++)
  2353. {
  2354. App.mainWindowViewModel.Value = i;
  2355. await Task.Delay(50); // 模拟长时间操作
  2356. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2357. {
  2358. return;
  2359. }
  2360. int endNum = startNum + (avePages - 1);
  2361. if (i < remain) endNum++;
  2362. endNum = Math.Min(endNum, PageCount);
  2363. string range = startNum + "-" + endNum;
  2364. startNum = endNum + 1;
  2365. filename = GetFileName(data, i);
  2366. string path = System.IO.Path.Combine(directory, filename);
  2367. path = CommonHelper.CreateFilePath(path);
  2368. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2369. bool result = savedoc.ImportPages(pdfViewer.Document, range);
  2370. if (!result)
  2371. {
  2372. savedoc.Release();
  2373. continue;
  2374. }
  2375. result = savedoc.WriteToFilePath(path);
  2376. if (!result)
  2377. {
  2378. savedoc.Release();
  2379. continue;
  2380. }
  2381. savedoc.Release();
  2382. selectedfile = path;
  2383. }
  2384. }
  2385. else//自定义页码范围
  2386. {
  2387. string pageRange = "";
  2388. List<int> pageList1 = new List<int>();
  2389. if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.OddPage)
  2390. {
  2391. int[] page = new int[(PageCount + 1) / 2];
  2392. index = page.Length;
  2393. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2394. for (int i = 0; i < page.Length; i++)
  2395. {
  2396. App.mainWindowViewModel.Value = i;
  2397. await Task.Delay(50); // 模拟长时间操作
  2398. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2399. {
  2400. return;
  2401. }
  2402. page[i] = i * 2 + 1;
  2403. }
  2404. pageRange = string.Join(",", page);
  2405. }
  2406. else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.EvenPage)
  2407. {
  2408. int[] page = new int[PageCount / 2];
  2409. index = page.Length;
  2410. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2411. for (int i = 0; i < page.Length; i++)
  2412. {
  2413. App.mainWindowViewModel.Value = i;
  2414. await Task.Delay(50); // 模拟长时间操作
  2415. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2416. {
  2417. return;
  2418. }
  2419. page[i] = i * 2 + 2;
  2420. }
  2421. pageRange = string.Join(",", page);
  2422. }
  2423. else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.CustomPage)
  2424. {
  2425. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", PageCount, System.Windows.Visibility.Visible);
  2426. pageRange = data.PageRange;
  2427. }
  2428. //如果是自定义输入页面范围的话,直接解析
  2429. CommonHelper.GetPagesInRange(ref pageList1, pageRange, PageCount, new char[] { ',' }, new char[] { '-' });
  2430. index = pageList1.Count;
  2431. #region //part1
  2432. selectedfile = "";
  2433. CPDFDocument saveDoc1 = CPDFDocument.CreateDocument();
  2434. //string filepath = data.FileName + " " + "1.pdf";
  2435. filename = GetFileName(data, 0);
  2436. string path1 = Path.Combine(directory, filename);
  2437. path1 = CommonHelper.CreateFilePath(path1);
  2438. bool result = saveDoc1.ImportPages(pdfViewer.Document, pageRange);
  2439. if (!result)
  2440. {
  2441. saveDoc1.Release();
  2442. ShowToast();
  2443. ChatGTPAIHelper.clossProcess(index);
  2444. return;
  2445. }
  2446. result = saveDoc1.WriteToFilePath(path1);
  2447. if (!result)
  2448. {
  2449. saveDoc1.Release();
  2450. ShowToast();
  2451. ChatGTPAIHelper.clossProcess(index);
  2452. return;
  2453. }
  2454. saveDoc1.Release();
  2455. selectedfile = path1;
  2456. #endregion //part1
  2457. #region 方案二 //Part2 除了选定之外的页面
  2458. List<int> pageList2 = new List<int>();
  2459. for (int i = 1; i <= pdfViewer.Document.PageCount; i++)
  2460. {
  2461. if (!pageList1.Contains(i - 1))//pagelist1 存放的是index
  2462. pageList2.Add(i);
  2463. }
  2464. string pageRange2 = string.Join(",", pageList2);
  2465. CPDFDocument saveDoc2 = CPDFDocument.CreateDocument();
  2466. string filepath2 = /*data.FileName + " " + "2.pdf";*/ GetFileName(data, 1);
  2467. string path2 = /*Path.Combine(dialog.SelectedPath, filepath2);*/Path.Combine(directory, filepath2);
  2468. path2 = CommonHelper.CreateFilePath(path2);
  2469. bool result1 = saveDoc2.ImportPages(pdfViewer.Document, pageRange2);
  2470. if (!result1)
  2471. {
  2472. saveDoc2.Release();
  2473. ShowToast();
  2474. return;
  2475. }
  2476. result1 = saveDoc2.WriteToFilePath(path2);
  2477. if (!result1)
  2478. {
  2479. saveDoc2.Release();
  2480. ShowToast();
  2481. return;
  2482. }
  2483. saveDoc2.Release();
  2484. //selectedfile = path2;
  2485. }
  2486. #endregion 方案二 //Part2 除了选定之外的页面
  2487. //显示文件夹,并选中一个文件
  2488. Process.Start("explorer", "/select,\"" + selectedfile + "\"");
  2489. ChatGTPAIHelper.clossProcess(index);
  2490. }
  2491. catch
  2492. {
  2493. }
  2494. }
  2495. private void PageSplitClear()
  2496. {
  2497. App.mainWindowViewModel.ProgressTitle = "Canceling";
  2498. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  2499. }
  2500. private string GetFileName(HomePageSplitDialogModel model, int index)
  2501. {
  2502. string filename = "";
  2503. string label = model.HasLabel ? model.FileNameLabel : "";
  2504. string deimiter = model.HasDeimiter ? model.FileNameDeimiter : "";
  2505. if (model.IsSourceNameFront)
  2506. {
  2507. filename = model.SourceFileName + deimiter + label + (index + 1) + ".pdf";
  2508. }
  2509. else
  2510. {
  2511. filename = label + (index + 1) + deimiter + model.SourceFileName + ".pdf";
  2512. }
  2513. return filename;
  2514. }
  2515. /// <summary>
  2516. /// 将文件插入到指定位置
  2517. /// </summary>
  2518. /// <param name="insertIndex"></param>
  2519. public async Task<bool> InsertFromFile(int insertIndex, string filePath, CPDFDocument doc = null, string oldPassWord = null, bool isFromUndoRedo = false)
  2520. {
  2521. if (doc == null)
  2522. {
  2523. return false;
  2524. }
  2525. int pageCount = doc.PageCount;
  2526. //插入整个新文档
  2527. //文档层插入
  2528. var insertresult = PDFViewer.Document.ImportPagesAtIndex(doc, "1-" + pageCount, insertIndex);
  2529. if (!insertresult)
  2530. {
  2531. ShowToast();
  2532. return false;
  2533. }
  2534. //插入成功后释放临时Document对象
  2535. doc.Release();
  2536. //UI层插入
  2537. for (int i = 0; i < pageCount; i++)
  2538. {
  2539. var item = GetNewPageEditItem(i);
  2540. PageEditItems.Insert(insertIndex + i, item);
  2541. }
  2542. //加一点延时才能显示选中效果
  2543. await Task.Delay(200);
  2544. List<int> pageRange = new List<int>();
  2545. for (int i = 0; i < pageCount; i++)
  2546. {
  2547. pageRange.Add(insertIndex + i + 1);
  2548. }
  2549. if (IsBOTAThumb == Visibility.Visible)
  2550. {
  2551. GoToPage(pageRange);
  2552. }
  2553. NotifyUIToRefresh(pageRange);
  2554. //如果是undo redo里调用本方法,则不需要添加到Undo栈
  2555. if (!isFromUndoRedo)
  2556. {
  2557. //添加记录到Undo栈,清空Redo栈
  2558. PageEditHistory history = new PageEditHistory();
  2559. history.Type = ActionType.InsertFromFile;
  2560. history.InsertIndex = insertIndex;
  2561. history.InsertFilePath = filePath;
  2562. history.InsertFilePassWord = oldPassWord;
  2563. history.InsertPageCount = pageCount;
  2564. history.viewmodel = this;
  2565. PDFViewer.UndoManager.AddHistory(history);
  2566. }
  2567. return true;
  2568. }
  2569. public bool GetDocumentIsDiscryptied(string filePath, string oldPassWord, out CPDFDocument doc, out string password)
  2570. {
  2571. doc = CPDFDocument.InitWithFilePath(filePath);
  2572. password = "";
  2573. if (doc == null)
  2574. {
  2575. //文件损坏
  2576. AlertsMessage alertsMessage = new AlertsMessage();
  2577. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Main_OpenFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  2578. return false;
  2579. }
  2580. if (doc.IsLocked)
  2581. {
  2582. if (!string.IsNullOrEmpty(oldPassWord))
  2583. {
  2584. var result = doc.UnlockWithPassword(oldPassWord);
  2585. if (result)
  2586. {
  2587. password = oldPassWord;
  2588. }
  2589. }
  2590. //没有传入密码,或者用传入密码解密失败是,需要弹出界面弹窗
  2591. if (doc.IsLocked)
  2592. {
  2593. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  2594. if (result.IsDiscryptied)
  2595. {
  2596. if (result.Password != null)
  2597. {
  2598. string filePath1 = doc.FilePath;
  2599. if (doc.IsLocked)
  2600. {
  2601. doc.UnlockWithPassword(result.Password);
  2602. }
  2603. else
  2604. {
  2605. doc.CheckOwnerPassword(result.Password);
  2606. }
  2607. password = result.Password;
  2608. }
  2609. }
  2610. else
  2611. {
  2612. return false;
  2613. }
  2614. }
  2615. }
  2616. return true;
  2617. }
  2618. /// <summary>
  2619. /// 插入自定义页面
  2620. /// </summary>
  2621. /// <param name="insertIndex"></param>
  2622. /// <param name="width"></param>
  2623. /// <param name="height"></param>
  2624. /// <param name="filePath"></param>
  2625. /// <returns></returns>
  2626. private bool InsertFromCustomPage(int insertIndex, double width, double height, string filePath)
  2627. {
  2628. try
  2629. {
  2630. bool result = false;
  2631. //文档层插入
  2632. if (!string.IsNullOrEmpty(filePath))
  2633. {
  2634. BitmapFrame frame = BitmapFrame.Create(new Uri(filePath));
  2635. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  2636. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  2637. result = PDFViewer.Document.InsertPage(insertIndex, frame.Width, frame.Height, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  2638. }
  2639. else
  2640. {
  2641. result = PDFViewer.Document.InsertPage(insertIndex, width, height, null);
  2642. }
  2643. if (!result)
  2644. {
  2645. ShowToast();
  2646. return false;
  2647. }
  2648. //UI层插入Item ,需要处理一下Item
  2649. var item = GetNewPageEditItem(insertIndex);
  2650. PageEditItems.Insert(insertIndex, item);
  2651. ReloadAfterOption(true, false, Tuple.Create(insertIndex - 1, insertIndex + 1), true);
  2652. //选中插入的页面
  2653. ListSelectedIndex = insertIndex;
  2654. return true;
  2655. }
  2656. catch
  2657. { return false; }
  2658. }
  2659. /// <summary>
  2660. /// 根据索引创建新的Item
  2661. /// </summary>
  2662. /// <param name="index"></param>
  2663. /// <returns></returns>
  2664. private PageEditItem GetNewPageEditItem(int index)
  2665. {
  2666. PageEditItem item = new PageEditItem();
  2667. if (PDFViewer.Document.PageAtIndex(index).Rotation % 2 != 0)
  2668. {
  2669. ///标记为横向页面
  2670. item.isVertical = false;
  2671. }
  2672. item.PageNumber = index + 1;
  2673. var pagesize = PDFViewer.Document.GetPageSize(index);
  2674. item.Size = pagesize;
  2675. //修复 缩放后粘贴的页面大小不一致的问题
  2676. item.ItemSize = ItemSizes[ZoomIndex + 1];
  2677. return item;
  2678. }
  2679. /// <summary>
  2680. /// 进行文件操作后刷新PDFView
  2681. /// 清除undo 栈
  2682. /// 重加载Document
  2683. /// 设置保存状态
  2684. /// </summary>
  2685. /// <param name="needRefreshPageNum">是否需要刷新页码</param>
  2686. /// <param name="needClearUndoHistory">是否需要清空注释Undo栈</param>
  2687. /// <param name="pageRange">需要刷新的页面范围图片</param>
  2688. public void ReloadAfterOption(bool needRefreshPageNum = false, bool needClearUndoHistory = false, Tuple<int, int> pageRange = null, bool isScrollIntoView = false)
  2689. {
  2690. if (needRefreshPageNum)
  2691. {
  2692. RefreshPageNum();
  2693. //从页面编辑退出的值,根据内容走,进页面编辑是这一页内容,退出来还是
  2694. if (CurrentPageEditItem != null && IsBOTAThumb != Visibility.Visible)
  2695. {
  2696. int pageIndex = CurrentPageEditItem.PageNumber - 1;
  2697. if (pageIndex != PDFViewer.CurrentIndex)
  2698. {
  2699. PDFViewer.GoToPage(pageIndex);
  2700. }
  2701. }
  2702. //插入、替换等更新页码,页码输入框的值未更新
  2703. RefreshTbPageRange();
  2704. }
  2705. if (needClearUndoHistory)
  2706. {
  2707. //只有删除操作清空undo栈
  2708. //可能会引起注释模块联动问题 需要留意
  2709. PDFViewer.UndoManager.ClearHistory();
  2710. }
  2711. if (pageRange != null)
  2712. {
  2713. if (pageRange.Item2 - pageRange.Item1 > 30)
  2714. {
  2715. if (isScrollIntoView)
  2716. {
  2717. //插入后 滚动到 选中的页面
  2718. List<int> pageRanges = new List<int>() { pageRange.Item2 };
  2719. NotifyUIToRefresh(pageRanges);
  2720. }
  2721. else
  2722. {
  2723. //范围较大时,通知UI获取更准确的刷新范围
  2724. NotifyUIToRefresh();
  2725. }
  2726. }
  2727. else
  2728. {
  2729. RefreshItemImage(pageRange.Item1, pageRange.Item2);
  2730. if (isScrollIntoView)
  2731. {
  2732. if (ListSelectedIndex == -1)
  2733. {
  2734. //插入后 滚动到 选中的页面
  2735. List<int> pageRanges = new List<int>() { pageRange.Item2 };
  2736. NotifyUIToRefresh(pageRanges);
  2737. }
  2738. }
  2739. }
  2740. }
  2741. PDFViewer.UndoManager.CanSave = true;
  2742. PDFViewer.ReloadDocument();
  2743. //解决:阅读页放大到只能看到最后一页时,从缩略图删除最后一页,阅读页面没有联动刷新
  2744. if (PDFViewer.GetVerticalOffset() >= PDFViewer.GetExtendHeight())
  2745. {
  2746. PDFViewer.ScrollToVerticalOffset(PDFViewer.GetExtendHeight());
  2747. }
  2748. }
  2749. /// <summary>
  2750. /// 重新获取选中页码
  2751. /// /插入、替换等更新页码,页码输入框的值未更新
  2752. /// </summary>
  2753. private void RefreshTbPageRange()
  2754. {
  2755. List<int> indexList = GetSelectedItemPageNumbers();
  2756. if (indexList.Count > 0)
  2757. {
  2758. //选中项集合 也需要 同步更新
  2759. selectedPageList.Clear();
  2760. selectedPageList.AddRange(indexList);
  2761. //只选中单个页面是,listselectedIndex 更准确
  2762. if (indexList.Count == 1)
  2763. {
  2764. TbPageRange = (listSelecedIndex + 1).ToString();
  2765. }
  2766. else
  2767. {
  2768. //根据选中的页面转换成1,3-10类似的字符串
  2769. string pageparm = CommonHelper.GetPageParmFromList(indexList);
  2770. TbPageRange = pageparm;
  2771. }
  2772. }
  2773. }
  2774. private List<int> GetSelectedItemPageNumbers()
  2775. {
  2776. List<int> indexList = new List<int>();
  2777. for (int i = 0; i < PageEditItems.Count; i++)
  2778. {
  2779. if (PageEditItems[i].Selected)
  2780. {
  2781. indexList.Add(PageEditItems[i].PageNumber);
  2782. }
  2783. }
  2784. return indexList;
  2785. }
  2786. /// <summary>
  2787. /// 旋转所有选中页面
  2788. /// </summary>
  2789. /// <param name="rightRotate"></param>
  2790. private async void RotateSelectedPages(bool rightRotate = true, List<int> pageitems = null, bool isFromUndoRedo = false)
  2791. {
  2792. if (!ServiceHelper.IAPBeforeFunction())
  2793. {
  2794. return;
  2795. }
  2796. IsLoading = Visibility.Visible;
  2797. await Task.Delay(2);
  2798. Dictionary<int, int> items = new Dictionary<int, int>();
  2799. if (pageitems == null)
  2800. {
  2801. //旋转选中页面
  2802. items = SetRotateSelectedPagesEvent(rightRotate);
  2803. }
  2804. else
  2805. {
  2806. if (pageitems.Count == 0)
  2807. {
  2808. items = SetRotateSelectedPagesEvent(rightRotate);
  2809. }
  2810. else
  2811. {
  2812. //旋转对应Items集合
  2813. for (int i = 0; i < pageitems.Count; i++)
  2814. {
  2815. PDFViewer.RotatePage(ComPDFKitViewer.PageRotate.Rotate90, rightRotate, pageitems[i]);
  2816. items.Add(pageitems[i], pageitems[i]);
  2817. //重新拿图
  2818. if (PageEditItems.Count > pageitems[i])
  2819. {
  2820. PageEditItems[pageitems[i]].IsGetImage = false;
  2821. }
  2822. }
  2823. }
  2824. }
  2825. //旋转页面后取消选中
  2826. PDFViewer.ClearSelectPDFEdit(true);
  2827. //如果是undo,Redo里调用的方法,则不记录到栈里
  2828. if (!isFromUndoRedo)
  2829. {
  2830. //记录进Undo栈
  2831. PageEditHistory history = new PageEditHistory();
  2832. history.Type = rightRotate ? ActionType.RightRotate : ActionType.LeftRotate;
  2833. history.SourceTargetValue = items;
  2834. history.viewmodel = this;
  2835. PDFViewer.UndoManager.AddHistory(history);
  2836. }
  2837. //页面范围-横向页面,旋转90°后,输入框应当变成具体的页码,因为旋转后不是横向页面了(纵向页面同理)
  2838. if (items.Count > 0)
  2839. {
  2840. ReloadAfterOption(true, false, Tuple.Create(items.Keys.First(), items.Keys.Last()));
  2841. }
  2842. IsLoading = Visibility.Collapsed;
  2843. }
  2844. private Dictionary<int, int> SetRotateSelectedPagesEvent(bool rightRotate)
  2845. {
  2846. Dictionary<int, int> items = new Dictionary<int, int>();
  2847. for (int i = 0; i < PageEditItems.Count; i++)
  2848. {
  2849. if (PageEditItems[i].Selected)
  2850. {
  2851. //逆时针旋转传 - 1 顺时针旋转传1
  2852. //直接用PDFViewer的旋转接口,需要留意好,中间层不能有自动清空undo栈
  2853. PDFViewer.RotatePage(ComPDFKitViewer.PageRotate.Rotate90, rightRotate, i);
  2854. items.Add(i, i);
  2855. //重新拿图
  2856. PageEditItems[i].IsGetImage = false;
  2857. }
  2858. }
  2859. return items;
  2860. }
  2861. /// <summary>
  2862. /// 刷新页码
  2863. /// </summary>
  2864. private void RefreshPageNum()
  2865. {
  2866. for (int i = 0; i < PageEditItems.Count; i++)
  2867. {
  2868. PageEditItems[i].PageNumber = i + 1;
  2869. if (IsBOTAThumb == Visibility.Visible)
  2870. {
  2871. PageEditItems[i].FontSize = 12;
  2872. }
  2873. else
  2874. {
  2875. PageEditItems[i].FontSize = 14;
  2876. }
  2877. }
  2878. }
  2879. /// <summary>
  2880. /// 刷新页面范围下拉框
  2881. /// </summary>
  2882. private void RefreshPageRangeItem()
  2883. {
  2884. PageRangeItems.Clear();
  2885. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_AllPage"));
  2886. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_OddPage"));
  2887. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_EvenPage"));
  2888. PageRangeItems.Add("Horizontal Page");
  2889. PageRangeItems.Add("Vertical Page");
  2890. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_CustomRangeEge"));
  2891. }
  2892. /// <summary>
  2893. /// 选中目标页面范围的页面
  2894. /// </summary>
  2895. /// <param name="index">页面范围选中项索引</param>
  2896. private async void SuitPageRange(int index)
  2897. {
  2898. List<int> pageRange = new List<int>();
  2899. switch (index)
  2900. {
  2901. case 0:
  2902. for (int i = 0; i < PageEditItems.Count; i++)
  2903. {
  2904. pageRange.Add(i + 1);
  2905. }
  2906. break;
  2907. case 1:
  2908. //奇数页
  2909. //先清空所有选项
  2910. ListSelectedIndex = -1;
  2911. for (int i = 0; i < PageEditItems.Count; i++)
  2912. {
  2913. if (i % 2 == 0)
  2914. {
  2915. pageRange.Add(i + 1);
  2916. }
  2917. }
  2918. break;
  2919. case 2:
  2920. //偶数页
  2921. //先清空所有选项
  2922. ListSelectedIndex = -1;
  2923. for (int i = 0; i < PageEditItems.Count; i++)
  2924. {
  2925. if (i % 2 != 0)
  2926. {
  2927. pageRange.Add(i + 1);
  2928. }
  2929. }
  2930. break;
  2931. case 3://横向页面
  2932. //先清空所有选项
  2933. ListSelectedIndex = -1;
  2934. for (int i = 0; i < PageEditItems.Count; i++)
  2935. {
  2936. if (!PageEditItems[i].isVertical)
  2937. {
  2938. pageRange.Add(i + 1);
  2939. }
  2940. }
  2941. break;
  2942. case 4://纵向页面
  2943. for (int i = 0; i < PageEditItems.Count; i++)
  2944. {
  2945. if (PageEditItems[i].isVertical)
  2946. {
  2947. pageRange.Add(i + 1);
  2948. }
  2949. }
  2950. break;
  2951. case 5://自定义
  2952. TxtPageInputVisibility = Visibility.Visible;
  2953. await Task.Delay(30);
  2954. TbPageRange = "";
  2955. break;
  2956. default:
  2957. break;
  2958. }
  2959. if (pageRange.Count > 0)
  2960. {
  2961. //避免在下面的过程中频繁触发SelectionChanged事件
  2962. IsSelectFormSelf = true;
  2963. NotifyUIToRefresh(pageRange);
  2964. //为了避免重复选中,条件也符合而文本缺失的情况
  2965. TbPageRange = PageRangeItems[index];
  2966. RightSelectedIndexs.Clear();
  2967. foreach (var item in pageRange)
  2968. {
  2969. RightSelectedIndexs.Add(item - 1);
  2970. }
  2971. IsSelectFormSelf = false;
  2972. }
  2973. else
  2974. {
  2975. //选中了下拉框的选项
  2976. //增加条件,修复全选页面,但是ctrl点选取消后,选中状态不准确问题
  2977. if (index != -1)
  2978. {
  2979. //没有符合条件时 清空选中项
  2980. ListSelectedIndex = -1;
  2981. //20230714 没有符合条件的页面时候,控件内容不清空
  2982. //没有符合条件的页面时,空间内容清空
  2983. //await Task.Delay(30);
  2984. //TbPageRange = "";
  2985. }
  2986. }
  2987. }
  2988. /// <summary>
  2989. /// 是否显示页面尺寸
  2990. /// </summary>
  2991. /// <param name="show">是或否</param>
  2992. private void ShowPageSize()
  2993. {
  2994. if (ShowedPageSize == Visibility.Visible)
  2995. {
  2996. IsShowPageSize = true;
  2997. ShowedPageSize = Visibility.Collapsed;
  2998. HidePageSize = Visibility.Visible;
  2999. }
  3000. else
  3001. {
  3002. IsShowPageSize = false;
  3003. HidePageSize = Visibility.Collapsed;
  3004. ShowedPageSize = Visibility.Visible;
  3005. }
  3006. ListSelectedIndex = -1;
  3007. selectedPageList.Clear();
  3008. //显示显示当前范围的item的页面尺寸,其他的Item在刷新图片时,再显示尺寸
  3009. if (startRefreshedIndex <= endRefreshedIndex)
  3010. {
  3011. for (int i = startRefreshedIndex; i < endRefreshedIndex; i++)
  3012. {
  3013. if (i >= 0 && i <= PageEditItems.Count - 1)
  3014. {
  3015. PageEditItems[i].ShowPageSize = IsShowPageSize;
  3016. }
  3017. }
  3018. }
  3019. }
  3020. /// <summary>
  3021. /// 从PDFView获取所有Items集合
  3022. /// </summary>
  3023. public void GetSourceItems(bool isFirstLoad)
  3024. {
  3025. if (isFirstLoad || IsBOTAThumb == Visibility.Visible)
  3026. {
  3027. PageEditItems.Clear();
  3028. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  3029. {
  3030. var item = GetNewPageEditItem(i);
  3031. PageEditItems.Add(item);
  3032. }
  3033. }
  3034. //else
  3035. //{
  3036. // //后续进来仅刷新图片即可
  3037. // for (int i = 0; i < PageEditItems.Count; i++)
  3038. // {
  3039. // PageEditItems[i].IsGetImage = false;
  3040. // }
  3041. //}
  3042. }
  3043. /// <summary>
  3044. /// 刷新书签列表
  3045. /// </summary>
  3046. public void RefreshBookMarkList()
  3047. {
  3048. if (PDFViewer != null)
  3049. {
  3050. var booklist = PDFViewer.Document.GetBookmarkList();
  3051. if (booklist == null)
  3052. {
  3053. return;
  3054. }
  3055. //获取所有书签的Index集合
  3056. List<int> marks = new List<int>();
  3057. for (int i = 0; i < booklist.Count; i++)
  3058. {
  3059. marks.Add(booklist[i].PageIndex);
  3060. }
  3061. //不能只遍历marks集合,考虑书签集合元素删除的情况
  3062. for (int k = 0; k < PageEditItems.Count; k++)
  3063. {
  3064. if (marks.Contains(k))
  3065. PageEditItems[k].HaveBookMark = true;
  3066. else
  3067. PageEditItems[k].HaveBookMark = false;
  3068. }
  3069. }
  3070. }
  3071. /// <summary>
  3072. /// 刷新指定范围的图片
  3073. /// </summary>
  3074. /// <param name="startIndex"></param>
  3075. /// <param name="endIndex"></param>
  3076. private void RefreshItemImage(int startIndex, int endIndex)
  3077. {
  3078. startRefreshedIndex = startIndex;
  3079. endRefreshedIndex = endIndex;
  3080. if (PDFViewer == null || PDFViewer.Document == null)
  3081. {
  3082. return;
  3083. }
  3084. //一个拿图比例系数,减少拿图耗时,尽量确保缩放后图片清晰度不会很差
  3085. int rate = (int)ItemSizes[2].Height;
  3086. //230628 缩略图需要根据当前 的缩略值
  3087. //int rate = (int)ItemSizes[ZoomIndex + 1].Height;
  3088. for (int i = startIndex; i <= endIndex; i++)
  3089. {
  3090. if (i <= PageEditItems.Count - 1 && i >= 0 && PageEditItems.Count > 0 && PDFViewer.Document != null && rate > 0)
  3091. {
  3092. //先更新一次每个页面的尺寸 确保图的方向正确
  3093. PageEditItems[i].Size = PDFViewer.Document.GetPageSize(i);
  3094. if (PageEditItems[i].Size.Width <= 0 || PageEditItems[i].Size.Height <= 0)
  3095. {
  3096. //补充判空逻辑,避免崩溃
  3097. continue;
  3098. }
  3099. //不能一次性显示页面大小,页数较多时会导致界面卡死
  3100. //每次刷新图片时,根据右键菜单状态(是否显示页面大小)来控制每个Item的页面尺寸显示
  3101. PageEditItems[i].ShowPageSize = IsShowPageSize;
  3102. //宽高为样式中图片控件宽高
  3103. if (PageEditItems[i].isVertical)
  3104. {
  3105. //等比例缩小
  3106. if (PageEditItems[i].Size.Height > rate)
  3107. {
  3108. int index = Convert.ToInt32(PageEditItems[i].Size.Width / (PageEditItems[i].Size.Height / rate));
  3109. SetThumbnailImage(i, index, rate, true);
  3110. }
  3111. else
  3112. {
  3113. int index = Convert.ToInt32(PageEditItems[i].Size.Width * (rate / (int)PageEditItems[i].Size.Height));
  3114. //尺寸大小的图片 等比例放大 避免页面放大后内容模糊
  3115. SetThumbnailImage(i, index, rate, true);
  3116. }
  3117. }
  3118. else
  3119. {
  3120. if (PageEditItems[i].Size.Width > rate)
  3121. {
  3122. int index = Convert.ToInt32(PageEditItems[i].Size.Height / (PageEditItems[i].Size.Width / rate));
  3123. SetThumbnailImage(i, index, rate, false);
  3124. }
  3125. else
  3126. {
  3127. int index = Convert.ToInt32(PageEditItems[i].Size.Height * (rate / (int)(PageEditItems[i].Size.Width)));
  3128. SetThumbnailImage(i, index, rate, false);
  3129. }
  3130. }
  3131. }
  3132. }
  3133. }
  3134. private async void SetThumbnailImage(int i, int index, int rate, bool isVertical)
  3135. {
  3136. if (viewContentViewModel.IsHiddenAnnot)
  3137. {
  3138. if (isVertical)
  3139. {
  3140. //显示=1, 隐藏=0注释
  3141. await PDFViewer.GetThumbnail(i, index, rate, 0);
  3142. }
  3143. else
  3144. {
  3145. await PDFViewer.GetThumbnail(i, rate, index, 0);
  3146. }
  3147. }
  3148. else
  3149. {
  3150. if (isVertical)
  3151. {
  3152. await PDFViewer.GetThumbnail(i, index, rate, 1);
  3153. }
  3154. else
  3155. {
  3156. await PDFViewer.GetThumbnail(i, rate, index, 1);
  3157. }
  3158. }
  3159. }
  3160. /// <summary>
  3161. /// 拖拽排序功能
  3162. /// </summary>
  3163. /// <param name="targetIndex"></param>
  3164. /// <param name="pageIndexs"></param>
  3165. public void DragToSort(int targetIndex, List<int> pageIndexs, bool IsFromUndoRedo = false)
  3166. {
  3167. //拖拽排序后需要重新选中项的集合
  3168. List<int> selectedPages = new List<int>();
  3169. //记录原位置和目标位置的字典,用于Undo,Redo恢复
  3170. Dictionary<int, int> SourceTargetValue = new Dictionary<int, int>();
  3171. bool result = false;
  3172. //排序前,先对所有页码排序
  3173. pageIndexs.Sort();
  3174. //要考虑每一次交换都会导致局部页码发生改变
  3175. //每次整体往后移动时,先移动大页码;整体往前移动时,先移动小页码;往中间移动时,再按上述两种情况分别移动
  3176. if (targetIndex <= pageIndexs[0])
  3177. {
  3178. for (int i = 0; i < pageIndexs.Count; i++)
  3179. {
  3180. // list.Add(PageEditListBox.Items[pageIndexs[i]] as ListBoxItem);
  3181. //sourceindex.Add(pageIndexs[i]);
  3182. result = MoveItemToIndex(pageIndexs[i], targetIndex + i);
  3183. if (!result)
  3184. {
  3185. return;
  3186. }
  3187. SourceTargetValue.Add(pageIndexs[i], targetIndex + i);
  3188. selectedPages.Add(targetIndex + i + 1);
  3189. }
  3190. }
  3191. else if (targetIndex > pageIndexs[pageIndexs.Count - 1])
  3192. {
  3193. //目标位置在所有选中内容右边 整体后移
  3194. for (int i = 0; i < pageIndexs.Count; i++)
  3195. {
  3196. result = MoveItemToIndex(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
  3197. if (!result)
  3198. {
  3199. return;
  3200. }
  3201. SourceTargetValue.Add(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
  3202. selectedPages.Add(targetIndex - i);
  3203. }
  3204. }
  3205. else//目标位置在所有选中项中间
  3206. {
  3207. int i, j, k;
  3208. //找出PageEditListBox.Items中页码等于destpage的下标
  3209. for (k = 0; k < pageIndexs.Count - 1; k++)
  3210. {
  3211. //这里要算入K---即前面部分的页面个数
  3212. if (pageIndexs[k] < targetIndex && pageIndexs[k + 1] >= targetIndex)
  3213. {
  3214. break;
  3215. }
  3216. }
  3217. //局部往后移动
  3218. for (i = 0; i <= k; i++)
  3219. {
  3220. result = MoveItemToIndex(pageIndexs[k - i], targetIndex - 1 - i);
  3221. if (!result)
  3222. {
  3223. return;
  3224. }
  3225. SourceTargetValue.Add(pageIndexs[k - i], targetIndex - 1 - i);
  3226. selectedPages.Add(targetIndex - i);
  3227. }
  3228. int newIndex = targetIndex;
  3229. //局部往前移动
  3230. for (j = i; j < pageIndexs.Count; j++)
  3231. {
  3232. result = MoveItemToIndex(pageIndexs[j], newIndex);
  3233. if (!result)
  3234. {
  3235. return;
  3236. }
  3237. SourceTargetValue.Add(pageIndexs[j], newIndex);
  3238. selectedPages.Add(newIndex + 1);
  3239. newIndex++;
  3240. }
  3241. }
  3242. //刷新页码和图片
  3243. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  3244. //先刷新页码再选中item,顶部的选中页面更准确
  3245. //通知UI选中Items
  3246. NotifyUIToRefresh(selectedPages);
  3247. if (!IsFromUndoRedo)
  3248. {
  3249. //存入Undo栈
  3250. PageEditHistory history = new PageEditHistory();
  3251. history.Type = ActionType.DragSort;
  3252. history.SourceTargetValue = SourceTargetValue;
  3253. //用于Redo恢复
  3254. history.InsertIndex = targetIndex;
  3255. history.viewmodel = this;
  3256. PDFViewer.UndoManager.AddHistory(history);
  3257. }
  3258. }
  3259. /// <summary>
  3260. /// 移动某个Item到指定位置
  3261. /// </summary>
  3262. private bool MoveItemToIndex(int sourceindex, int targetindex)
  3263. {
  3264. if (targetindex == sourceindex || targetindex < 0)
  3265. {
  3266. return false;
  3267. }
  3268. //文档层移动
  3269. var result = PDFViewer.Document.MovePage(sourceindex, targetindex);
  3270. if (!result)
  3271. {
  3272. return false;
  3273. }
  3274. //UI层移动
  3275. var source = PageEditItems[sourceindex];
  3276. PageEditItems.RemoveAt(sourceindex);
  3277. PageEditItems.Insert(targetindex, source);
  3278. if (PDFViewer.CurrentIndex == sourceindex)
  3279. {
  3280. PDFViewer.GoToPage(targetindex);
  3281. }
  3282. return true;
  3283. }
  3284. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  3285. {
  3286. if (PDFViewer.Parent is ContentControl ui)
  3287. {
  3288. //判断鼠标焦点 是否在控件上
  3289. if (ui.IsMouseOver)
  3290. {
  3291. //缩略图 复制粘贴后会选择第一项的问题
  3292. if (e.Key == "PageNum" && !viewContentViewModel.isInPageEdit)
  3293. {
  3294. int index = (e.Value as ComPDFKitViewer.RenderData).PageIndex - 1;
  3295. //注释掉 该方法有许多联动,避免联动影响
  3296. //NotifyUIToRefresh(new List<int> { (e.Value as ComPDFKitViewer.RenderData).PageIndex });
  3297. eventor.GetEvent<PageEditSelectedItemEvent>().Publish(new PageEditSelectedItemEventArgs() { Unicode = unicode, SelectedItemIndex = index, IsSingle = true });
  3298. }
  3299. }
  3300. }
  3301. //不准确 有时候会触发两次,就无法判断
  3302. //if (IsAddPages == false)
  3303. //{
  3304. // //缩略图 复制粘贴后会选择第一项的问题
  3305. // if (e.Key == "PageNum" && !viewContentViewModel.isInPageEdit)
  3306. // {
  3307. // int index = (e.Value as ComPDFKitViewer.RenderData).PageIndex - 1;
  3308. // //注释掉 该方法有许多联动,避免联动影响
  3309. // //NotifyUIToRefresh(new List<int> { (e.Value as ComPDFKitViewer.RenderData).PageIndex });
  3310. // eventor.GetEvent<PageEditSelectedItemEvent>().Publish(new PageEditSelectedItemEventArgs() { Unicode = unicode, SelectedItemIndex = index, IsSingle = true });
  3311. // }
  3312. //}
  3313. //else
  3314. //{
  3315. // IsAddPages = false;
  3316. // return;
  3317. //}
  3318. }
  3319. /// <summary>
  3320. /// 从底层库获取对应页面的图片
  3321. /// </summary>
  3322. /// <param name="pageIndex"></param>
  3323. /// <param name="thumb"></param>
  3324. /// <param name="w"></param>
  3325. /// <param name="h"></param>
  3326. private void PDFViewer_OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  3327. {
  3328. try
  3329. {
  3330. if (PageEditItems.Count >= pageIndex + 1)
  3331. {
  3332. PixelFormat fmt = PixelFormats.Bgra32;
  3333. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  3334. PageEditItems[pageIndex].Image = bps;
  3335. PageEditItems[pageIndex].IsGetImage = true;
  3336. }
  3337. }
  3338. catch
  3339. {
  3340. }
  3341. }
  3342. #endregion 方法
  3343. #region Navigate
  3344. public bool IsNavigationTarget(NavigationContext navigationContext)
  3345. {
  3346. return true;
  3347. }
  3348. public void OnNavigatedFrom(NavigationContext navigationContext)
  3349. {
  3350. }
  3351. /// <summary>
  3352. /// 从其他页面导航过来的时候会触发
  3353. /// 考虑只有在页面编辑的时候会进行页面的删减,可以尝试逻辑优化:仅第一次进入页面编辑时重新刷新页面
  3354. /// </summary>
  3355. /// <param name="navigationContext"></param>
  3356. public void OnNavigatedTo(NavigationContext navigationContext)
  3357. {
  3358. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  3359. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  3360. bool isBOTA = false;
  3361. navigationContext.Parameters.TryGetValue<bool>(ParameterNames.BOTAThumb, out isBOTA);
  3362. if (PDFViewer != null)
  3363. {
  3364. Initializing = true;
  3365. if (isFirstLoad)
  3366. {
  3367. PDFViewer.OnThumbnailGenerated -= PDFViewer_OnThumbnailGenerated;
  3368. PDFViewer.OnThumbnailGenerated += PDFViewer_OnThumbnailGenerated;
  3369. PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
  3370. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  3371. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  3372. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  3373. ZoomIndex = 0;
  3374. }
  3375. //BOTA缩略图时,显示不同的菜单栏
  3376. if (isBOTA)
  3377. {
  3378. IsBOTAThumb = Visibility.Visible;
  3379. //ShowedPageSize = Visibility.Collapsed;
  3380. //HidePageSize = Visibility.Collapsed;
  3381. }
  3382. else
  3383. {
  3384. IsBOTAThumb = Visibility.Collapsed;
  3385. //进入页面编辑 滚动到选中页面
  3386. //NotifyUIToRefresh();
  3387. }
  3388. //每次进入页面编辑都刷新item个数,缩略图删减时,可以更新item
  3389. //需要留意大文档性能
  3390. GetSourceItems(true);
  3391. // 暂时 bota 和页面编辑不联动,切换后,是重新获取数据,不会获得之前的状态
  3392. IsShowPageSize = false;
  3393. ShowedPageSize = Visibility.Visible;
  3394. HidePageSize = Visibility.Collapsed;
  3395. //清空一下选中项,修复进入页面编辑后仍然选中多页的问题
  3396. ListSelectedIndex = -1;
  3397. ListSelectedIndex = PDFViewer.CurrentIndex;
  3398. //页面编辑取消显示书签Icon
  3399. //RefreshBookMarkList();
  3400. isFirstLoad = false;
  3401. Initializing = false;
  3402. if (PageEditItems.Count > 0)
  3403. {
  3404. if (PageEditItems.IndexOf(PageEditItems[PDFViewer.CurrentIndex]) > -1)
  3405. {
  3406. //记录 进页面编辑是这一页内容
  3407. CurrentPageEditItem = PageEditItems[PDFViewer.CurrentIndex];
  3408. }
  3409. }
  3410. }
  3411. }
  3412. private void PDFViewer_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
  3413. {
  3414. if (e != null && e.Count > 0 && IsBOTAThumb == Visibility.Visible)
  3415. {
  3416. //缩略图模式下 刷新页面
  3417. int pageIndex = e[0].PageIndex;
  3418. RefreshItemImage(pageIndex, pageIndex);
  3419. }
  3420. }
  3421. #endregion Navigate
  3422. }
  3423. }