12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468 |
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.Model;
- using System.Collections.ObjectModel;
- using PDF_Master.Model.PageEdit;
- using Prism.Commands;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.Windows.Controls.Primitives;
- using System.Diagnostics;
- using System.Windows.Controls;
- using System.Windows.Threading;
- using Prism.Events;
- using PDF_Master.EventAggregators;
- using System.Windows.Input;
- using PDF_Master.Helper;
- using Microsoft.Win32;
- using ComPDFKit.PDFDocument;
- using PDF_Master.CustomControl;
- using PDF_Master.Model.HomePageToolsDialogs;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace PDF_Master.ViewModels.PageEdit
- {
- public class PageEditContentViewModel : BindableBase, INavigationAware
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private void InitString()
- {
- T_Title = App.MainPageLoader.GetString("Thumbnails_Title");
- }
- #endregion
- private CPDFViewer PDFViewer;
- private ViewContentViewModel viewContentViewModel;
- private IDialogService dialogs;
- private IEventAggregator eventor;
- /// <summary>
- /// 用于筛选多窗体时间的唯一识别码
- /// </summary>
- private string unicode = null;
- /// <summary>
- /// Undo操作记录栈
- /// 只有插入、移位、旋转,倒序需要记录
- /// </summary>
- private Stack<PageEditHistory> UndoStack = new Stack<PageEditHistory>();
- /// <summary>
- /// Redo操作记录栈
- /// </summary>
- private Stack<PageEditHistory> RedoStack = new Stack<PageEditHistory>();
- /// <summary>
- /// 项数据集合
- /// </summary>
- public ObservableCollection<PageEditItem> PageEditItems { get; set; }
- /// <summary>
- /// 页面范围的项集合
- /// </summary>
- public ObservableCollection<string> PageRangeItems { get; set; }
- #region 属性
- /// <summary>
- /// 是否是第一次加载
- /// </summary>
- private bool isFirstLoad = true;
- /// <summary>
- /// 记录多选项中最后一项的索引
- /// </summary>
- private int maxSelectedIndex = 0;
- private Visibility showTip = Visibility.Collapsed;
- /// <summary>
- /// 是否显示渐隐提示
- /// </summary>
- public Visibility ShowTip
- {
- get { return showTip; }
- set
- {
- SetProperty(ref showTip, value);
- }
- }
- private Visibility isLoading = Visibility.Collapsed;
- /// <summary>
- /// 是否正在处理中
- /// </summary>
- public Visibility IsLoading
- {
- get { return isLoading; }
- set
- {
- SetProperty(ref isLoading, value);
- }
- }
- private int listSelecedIndex;
- /// <summary>
- /// 缩略图选中项的索引
- /// </summary>
- public int ListSelectedIndex
- {
- get { return listSelecedIndex; }
- set
- {
- SetProperty(ref listSelecedIndex, value);
- //显示页面范围选择控件
- TbPageRangeVisibility = Visibility.Visible;
- TbPageRange = (ListSelectedIndex + 1).ToString();
- TxtPageInputVisibility = Visibility.Collapsed;
- if(IsBOTAThumb==Visibility.Visible&&PDFViewer!=null)
- {
- PDFViewer.GoToPage(listSelecedIndex);
- }
- }
- }
- private bool isMultiSelected;
- /// <summary>
- /// 是否有多选 用于判断倒序和替换功能
- /// </summary>
- public bool IsMultiSelected
- {
- get { return isMultiSelected; }
- set
- {
- SetProperty(ref isMultiSelected, value);
- }
- }
- private int comboxSelectIndex;
- /// <summary>
- /// 下拉框选项集合
- /// </summary>
- public int ComboxSelectIndex
- {
- get { return comboxSelectIndex; }
- set
- {
- SetProperty(ref comboxSelectIndex, value);
- //选中指定的页面范围
- SuitPageRange(comboxSelectIndex);
- //隐藏tb页面范围显示控件
- TbPageRangeVisibility = Visibility.Collapsed;
- }
- }
- private Visibility tbPageRangeVisibility = Visibility.Visible;
- /// <summary>
- /// 页面范围的文本显示
- /// </summary>
- public Visibility TbPageRangeVisibility
- {
- get { return tbPageRangeVisibility; }
- set
- {
- SetProperty(ref tbPageRangeVisibility, value);
- if (value == Visibility.Visible)
- {
- comboxSelectIndex = -1;
- }
- }
- }
- private string tbPageRange;
- /// <summary>
- /// 页面范围显示的内容
- /// </summary>
- public string TbPageRange
- {
- get { return tbPageRange; }
- set
- {
- SetProperty(ref tbPageRange, value);
- if(string.IsNullOrEmpty(TbPageRange))
- {
- TbPageRange = "请输入范围";
- }
- }
- }
- private Visibility txtPageInputVisibility = Visibility.Collapsed;
- /// <summary>
- /// 页面自定义输入框的显示
- /// </summary>
- public Visibility TxtPageInputVisibility
- {
- get { return txtPageInputVisibility; }
- set
- {
- SetProperty(ref txtPageInputVisibility, value);
- }
- }
- /// <summary>
- /// 页面缩放状态,-1 为最小值,1为最大值
- /// </summary>
- private int zoomIndex = 0;
- public int ZoomIndex
- {
- get { return zoomIndex; }
- set
- {
- SetProperty(ref zoomIndex, value);
- }
- }
- private string toastText = App.MainPageLoader.GetString("PageEdit_Failed");
- /// <summary>
- /// Toast提示
- /// </summary>
- public string ToastText
- {
- get { return toastText; }
- set
- {
- SetProperty(ref toastText, value);
- }
- }
- private Visibility isBOTAThumb = Visibility.Collapsed;
- /// <summary>
- /// 是否是BOTA侧边栏的缩略图
- /// </summary>
- public Visibility IsBOTAThumb
- {
- get { return isBOTAThumb; }
- set
- {
- SetProperty(ref isBOTAThumb, value);
- }
- }
- private Visibility hasShowPageSize = Visibility.Collapsed;
- /// <summary>
- /// 是否显示页面尺寸
- /// </summary>
- public Visibility HasShowPageSize
- {
- get { return hasShowPageSize; }
- set
- {
- SetProperty(ref hasShowPageSize, value);
- }
- }
- private bool canUndo;
- /// <summary>
- /// 是否可以进行Undo操作
- /// </summary>
- public bool CanUndo
- {
- get { return canUndo; }
- set
- {
- SetProperty(ref canUndo, value);
- if (viewContentViewModel != null)
- {
- viewContentViewModel.CanUndo = this.CanUndo;
- }
- }
- }
- private bool canRedo;
- /// <summary>
- /// 是否可以进行Redo操作
- /// </summary>
- public bool CanRedo
- {
- get { return canRedo; }
- set
- {
- SetProperty(ref canRedo, value);
- if(viewContentViewModel!=null)
- {
- viewContentViewModel.CanRedo = this.CanRedo;
- }
-
- }
- }
- private bool canPaste = false;
- /// <summary>
- /// 是否可以粘贴
- /// </summary>
- public bool CanPaste
- {
- get { return canPaste; }
- set
- {
- SetProperty(ref canPaste, value);
- }
- }
- /// <summary>
- /// 页面缩放的范围
- /// </summary>
- private List<Size> ItemSizes = new List<Size>() {
- new Size(104,142),
- new Size(208, 294),
- new Size(312,441)
- };
- //选中的页面集合
- private List<int> selectedPageList = new List<int>();
- #endregion
- #region 命令
- public DelegateCommand<object> SelectionChangedCommand { get; set; }
- /// <summary>
- /// 拦截键盘输入的事件
- /// </summary>
- public DelegateCommand<object> PreviewKeyDown { get; set; }
- /// <summary>
- /// 响应键盘回车事件
- /// </summary>
- public DelegateCommand<object> KeyDown { get; set; }
- /// <summary>
- ///文本框失去焦点事件
- /// </summary>
- public DelegateCommand<object> LostFocusCommand { get; set; }
- /// <summary>
- /// 粘贴
- /// </summary>
- public DelegateCommand PasteCommand { get; set; }
- /// <summary>
- /// 复制
- /// </summary>
- public DelegateCommand CopyCommand { get; set; }
- /// <summary>
- /// 剪切
- /// </summary>
- public DelegateCommand CutCommand { get; set; }
- /// <summary>
- /// 替换文件
- /// </summary>
- public DelegateCommand ReplaceCommand { get; set; }
- /// <summary>
- /// 插入文件命令
- /// </summary>
- public DelegateCommand<object> InsertCommand { get; set; }
- /// <summary>
- /// 拆分文件
- /// </summary>
- public DelegateCommand SplitCommand { get; set; }
- /// <summary>
- /// 提取文件
- /// </summary>
- public DelegateCommand ExtractCommand { get; set; }
- /// <summary>
- /// 逆时针旋转
- /// </summary>
- public DelegateCommand LeftRotateCommand { get; set; }
- /// <summary>
- /// 顺时针旋转
- /// </summary>
- public DelegateCommand RightRotateCommand { get; set; }
- /// <summary>
- /// 删除
- /// </summary>
- public DelegateCommand DeleteCommand { get; set; }
- /// <summary>
- /// 倒序
- /// </summary>
- public DelegateCommand ReverseCommand { get; set; }
- /// <summary>
- /// 打印
- /// </summary>
- public DelegateCommand PrintCommand { get; set; }
- /// <summary>
- /// 放大
- /// </summary>
- public DelegateCommand ZoomInCommand { get; set; }
- /// <summary>
- /// 缩小
- /// </summary>
- public DelegateCommand ZoomOutCommand { get; set; }
- /// <summary>
- /// 显示页面大小
- /// </summary>
- public DelegateCommand ShowPageSizeCommand { get; set; }
- public DelegateCommand ClearSelectedCommand { get; set; }
- #endregion
- public PageEditContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
- {
- //变量初始化
- dialogs = dialogService;
- eventor = eventAggregator;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- PageEditItems = new ObservableCollection<PageEditItem>();
- PageRangeItems = new ObservableCollection<string>();
- //绑定命令
- SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
- PreviewKeyDown = new DelegateCommand<object>(PreviewKeyDownEvent);
- KeyDown = new DelegateCommand<object>(KeyDownEvent);
- LostFocusCommand = new DelegateCommand<object>(lostfocus);
- SplitCommand = new DelegateCommand(SplitCommandEvent);
- InsertCommand = new DelegateCommand<object>(InsertCommandEvent);
- ReplaceCommand = new DelegateCommand(ReplaceCommandEvent, CanReplaceCommandExcute).ObservesProperty(() => IsMultiSelected);
- ExtractCommand = new DelegateCommand(ExtractCommandEvent, CanFileCommandExcute).ObservesProperty(() => ListSelectedIndex);
- DeleteCommand = new DelegateCommand(DeleteCommandEvent, CanFileCommandExcute).ObservesProperty(() => ListSelectedIndex);
- LeftRotateCommand = new DelegateCommand(LeftRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => ListSelectedIndex);
- RightRotateCommand = new DelegateCommand(RightRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => ListSelectedIndex);
- ReverseCommand = new DelegateCommand(ReverseCommandEvent, CanFileCommandExcute).ObservesProperty(() => ListSelectedIndex);
- ZoomInCommand = new DelegateCommand(ZoomInCommandEvent, CanZoomIn).ObservesProperty(() => ZoomIndex);
- ZoomOutCommand = new DelegateCommand(ZoomOutCommandEvent, CanZoomOut).ObservesProperty(() => ZoomIndex);
- ShowPageSizeCommand = new DelegateCommand(ShowPageSize);
- CopyCommand = new DelegateCommand(copy);
- PasteCommand = new DelegateCommand(paste,CanPasteExcute).ObservesProperty(() => CanPaste);
- CutCommand = new DelegateCommand(cut);
- PrintCommand = new DelegateCommand(print);
- ClearSelectedCommand = new DelegateCommand(clearSelected);
- //订阅页面刷新事件
- eventAggregator.GetEvent<PageEditRefreshEvent>().Subscribe(OnPageEditRefreshEvent, e => e.Unicode == unicode);
- CacheFilePath.Instance.PropertyChanged += Instance_PropertyChanged;
- RefreshPageRangeItem();
- InitString();
- }
- #region 事件
- private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if(e.PropertyName=="CopyDoc")
- {
- if(CacheFilePath.Instance.CopyDoc!=null)
- {
- CanPaste = true;
- }
- }
- }
- /// <summary>
- /// 替换功能的逻辑
- /// </summary>
- private async void ReplaceCommandEvent()
- {
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.Multiselect = false;
- dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
- if ((bool)dialog.ShowDialog())
- {
- IsLoading = Visibility.Visible;
- await Task.Delay(200);
- int pageCount = PDFViewer.Document.PageCount;
- int insertIndex = listSelecedIndex;
- //从文件插入到指定位置
- var result = await InsertFromFile(listSelecedIndex+1,dialog.FileName);
- if(result)
- {
- //插入成功后再删除原有页面
- DoRemoveSelectedPages(new List<int>() { listSelecedIndex });
- }
- ReloadAfterOption(true, true, Tuple.Create(insertIndex-1, insertIndex + pageCount));
- IsLoading = Visibility.Collapsed;
- }
- }
- /// <summary>
- /// 拆分功能的逻辑
- /// </summary>
- private void SplitCommandEvent()
- {
- DialogParameters valuePairs = new DialogParameters();
- valuePairs.Add(ParameterNames.PDFViewer, PDFViewer);
- dialogs.ShowDialog(DialogNames.SplitDialog, valuePairs,e=> {
- if(e.Result==ButtonResult.OK&&e.Parameters!=null)
- {
- var model = e.Parameters.GetValue<HomePageSplitDialogModel>(ParameterNames.DataModel);
- DoSplitPages(model);
- }
- });
- }
- /// <summary>
- /// 提取功能的逻辑
- /// </summary>
- private void ExtractCommandEvent()
- {
- dialogs.ShowDialog(DialogNames.ExtractDialog,null,e=> {
- if(e.Result==ButtonResult.OK&&e.Parameters!=null)
- {
- var model = e.Parameters.GetValue<ExtractModel>(ParameterNames.DataModel);
- DoExtractPages(model.IsEveryPageToFile,model.IsDeleteAfterExtract);
- }
- });
- }
- /// <summary>
- /// 插入功能的逻辑
- /// </summary>
- private async void InsertCommandEvent(object type)
- {
- if((string)type==null)
- {
- return;
- }
- //获取插入位置
- int insertIndex = 0;
- if(listSelecedIndex<0)
- {
- insertIndex = 0;
- }
- else
- {
- insertIndex = listSelecedIndex+1;
- if(isMultiSelected)
- {
- insertIndex = maxSelectedIndex+1;
- }
- }
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- PageEditHistory history = new PageEditHistory();
- history.InsertIndex = insertIndex;
- if((string)type==ParameterNames.InsertType_Blank)
- {
- history.Type = ActionType.InsertCustomPages;
- history.InsertFilePath = "";
- //插入空白页面
- bool result;
- if (insertIndex == 0)
- {
- result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex].Size.Width, PageEditItems[insertIndex].Size.Height, null);
- history.PageSize = PageEditItems[insertIndex].Size;
- }
- else
- {
- result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex-1].Size.Width, PageEditItems[insertIndex-1].Size.Height, null);
- history.PageSize = PageEditItems[insertIndex - 1].Size;
- }
- if(!result)
- {
- IsLoading = Visibility.Collapsed;
- ShowToast();
- return;
- }
- //UI层插入Item
- var item = GetNewPageEditItem(insertIndex);
- PageEditItems.Insert(insertIndex,item);
-
- ReloadAfterOption(true,true,Tuple.Create(insertIndex,insertIndex+1));
- await Task.Delay(10);
- //选中插入的页面
- ListSelectedIndex = insertIndex;
- }
- else if((string)type == ParameterNames.InsertType_File)
- {
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.Multiselect = false;
- dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
- if ((bool)dialog.ShowDialog())
- {
- //插入文件
- var result = await InsertFromFile(insertIndex,dialog.FileName);
- if(!result)
- {
- IsLoading = Visibility.Collapsed;
- ShowToast();
- return;
- }
- ReloadAfterOption(true,true,Tuple.Create(0,PDFViewer.Document.PageCount));
- }
- IsLoading = Visibility.Collapsed;
- //里面附带了Undo栈记录,需要提前返回,避免重复记录
- return;
- }
- else
- {
- DialogParameters keyValuePairs = new DialogParameters();
- Size size = new Size();
- size.Width =CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Width);
- size.Height = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Height);
- keyValuePairs.Add("CurrentPageSize", size);
- //插入自定义页面
- dialogs.ShowDialog(DialogNames.InsertDialog, keyValuePairs,e=> {
- if(e.Result == ButtonResult.OK&&e.Parameters!=null)
- {
- var model = e.Parameters.GetValue<CustomInsertModel>(ParameterNames.DataModel);
- var result = InsertFromCustomPage(insertIndex, model.width, model.height, model.filepath);
- if(!result)
- {
- return;
- }
- history.Type = ActionType.InsertCustomPages;
- history.InsertFilePath = model.filepath;
- history.PageSize = new Size(model.width,model.height);
- }
- });
- }
- IsLoading = Visibility.Collapsed;
- //添加记录到Undo栈,清除Redo栈
- AddToUndoStack(history);
- }
- /// <summary>
- /// 删除选中页面
- /// </summary>
- private async void DeleteCommandEvent()
- {
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- DoRemoveSelectedPages();
- ReloadAfterOption(true, true, Tuple.Create(0,PDFViewer.Document.PageCount));
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 左旋转页面的逻辑
- /// </summary>
- private void LeftRotateCommandEvent()
- {
- RotateSelectedPages(false);
- }
- /// <summary>
- /// 右旋转页面的逻辑
- /// </summary>
- private void RightRotateCommandEvent()
- {
- RotateSelectedPages();
- }
- /// <summary>
- /// 页面倒序的逻辑
- /// </summary>
- private async void ReverseCommandEvent()
- {
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- List<int> pages = new List<int>();
- for (int i = 0; i <PageEditItems.Count; i++)
- {
- if (PageEditItems[i].Selected)
- {
- pages.Add(i);
- }
- }
- DoReverse(pages);
-
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 将指定页面结合进行倒序
- /// </summary>
- private async void DoReverse(List<int> pages, bool isFromUndoRedo = false)
- {
- if (pages.Count <= 1)
- {
- ShowToast("请至少选择两个图片进行倒序操作。");
- return;
- }
- pages.Sort();
- //记录原位置和目标位置 用于undo,redo
- Dictionary<int, int> sourceTarget = new Dictionary<int, int>();
- for (int i = 0; i < pages.Count; i++)
- {
- int preindex = pages[i];
- int laterIndex = pages[pages.Count - i - 1];//尾部的索引
- if (laterIndex <= preindex)//交换完成
- {
- //用于记录选中集合为奇数时,最中间Item的还原
- sourceTarget.Add(preindex, preindex);
- break;
- }
- var result = ExchangeItem(preindex,laterIndex);
- if(!result)
- {
- ShowToast();
- IsLoading = Visibility.Collapsed;
- return;
- }
- sourceTarget.Add(preindex, laterIndex);
- }
- await Task.Delay(20);
- //转换成页码数据集合,传递给UI,选中目标页面
- //因虚拟化加载的问题,通过Selected来控制Item选中效果会有问题
- List<int> pageRange = new List<int>();
- for (int i = 0; i < pages.Count; i++)
- {
- pageRange.Add(pages[i] + 1);
- }
- NotifyUIToRefresh(pageRange);
- ReloadAfterOption(true, true, null);
- if(!isFromUndoRedo)
- {
- PageEditHistory history = new PageEditHistory();
- history.Type = ActionType.Reverse;
- history.SourceTargetValue = sourceTarget;
- AddToUndoStack(history);
- }
- }
- /// <summary>
- /// 交换两个Item顺序
- /// </summary>
- /// <param name="preiIndex"></param>
- /// <param name="laterIndex"></param>
- /// <returns></returns>
- private bool ExchangeItem(int preiIndex,int laterIndex)
- {
- if (preiIndex == laterIndex)
- {
- return true;
- }
- var sourceitem = PageEditItems[preiIndex];
- var targetitem = PageEditItems[laterIndex];
- var result = PDFViewer.Document.ExchangePage(preiIndex, laterIndex);
- if (!result)
- {
- return false;
- }
- PageEditItems.Remove(sourceitem);
- PageEditItems.Remove(targetitem);
- PageEditItems.Insert(preiIndex, targetitem);
- PageEditItems.Insert(laterIndex, sourceitem);
- //PageEditItems.Remove(targetitem);
- //PageEditItems.Insert(laterIndex, sourceitem);
- //PageEditItems.Remove(sourceitem);
- //PageEditItems.Insert(preiIndex, targetitem);
- return true;
- }
- /// <summary>
- /// 是否可以执行文件操作命令 除插入、拆分外
- /// </summary>
- /// <returns></returns>
- private bool CanFileCommandExcute()
- {
- if (ListSelectedIndex < 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /// <summary>
- /// 校验是否可以替换
- /// </summary>
- /// <returns></returns>
- private bool CanReplaceCommandExcute()
- {
- return !IsMultiSelected;
- }
- /// <summary>
- /// 放大页面
- /// </summary>
- private void ZoomInCommandEvent()
- {
- ZoomIndex++;
- ChangeItemSize(ItemSizes[ZoomIndex+1]);
- NotifyUIToRefresh();
- }
- /// <summary>
- /// 缩小页面
- /// </summary>
- private void ZoomOutCommandEvent()
- {
- ZoomIndex--;
- ChangeItemSize(ItemSizes[ZoomIndex+1]);
- NotifyUIToRefresh();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="newsize"></param>
- public void ChangeItemSize(Size newsize)
- {
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- PageEditItems[i].ItemSize = newsize;
- }
- //缩略图模式 调整item大小的时候,更新选中项
- NotifyUIToRefresh(new List<int>() { PDFViewer.CurrentIndex + 1 });
- }
- private bool CanZoomIn()
- {
- if (ZoomIndex >= 1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private bool CanZoomOut()
- {
- if (ZoomIndex <= -1)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /// <summary>
- /// 响应UI发送刷新图片的事件
- /// </summary>
- /// <param name="e"></param>
- private void OnPageEditRefreshEvent(PageEditRefreshEventArgs e)
- {
- //UI发送事件过来通知刷新图片时
- //调整逻辑,前后预加载5页
- RefreshItemImage(e.PageRange.Item1 - 2, e.PageRange.Item2 + 2);
- }
- /// <summary>
- /// 选项改变事件
- /// 虚拟化的时候不会触发选中事件
- /// </summary>
- /// <param name="e"></param>
- private void SelectionChangedEvent(object e)
- {
- var args = e as SelectionChangedEventArgs;
- //下下策 从UI控件获取所有选项的集合
- var listbox = args.OriginalSource as ListBox;
- if (TbPageRangeVisibility == Visibility.Visible && args != null)
- {
- selectedPageList = new List<int>();
- for (int i = 0; i < listbox.SelectedItems.Count; i++)
- {
- if (!selectedPageList.Contains((listbox.SelectedItems[i] as PageEditItem).PageNumber))
- {
- selectedPageList.Add((listbox.SelectedItems[i] as PageEditItem).PageNumber);
- }
- }
- ////根据选中的范围 适配全部页面范围、奇数页面范围等
- //if (pagelist.Count == PageEditItems.Count)
- //{
- // ComboxSelectIndex = 0;
- // return;
- //}
- //只选中单个页面是,listselectedIndex 更准确
- if (selectedPageList.Count == 1)
- {
- TbPageRange = (listSelecedIndex + 1).ToString();
- }
- else
- {
- string pageparm = CommonHelper.GetPageParmFromList(selectedPageList);
- TbPageRange = pageparm;
- }
-
- }
- //Ctrl+A时让所有item选中
- var isCtrlorShiftDown =( (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.A))|| (Keyboard.IsKeyDown(Key.RightCtrl) && Keyboard.IsKeyDown(Key.A)));
- if (isCtrlorShiftDown)
- {
- for (int i = 0; i < PageEditItems.Count; i++)
-
- {
- PageEditItems[i].Selected = true;
- }
- }
- //判断是否有选中多项
- if (listbox.SelectedItems.Count > 1)
- {
- IsMultiSelected = true;
- maxSelectedIndex = listbox.Items.IndexOf(listbox.SelectedItems[listbox.SelectedItems.Count - 1]);
- }
- else
- {
- IsMultiSelected = false;
- }
- }
- /// <summary>
- /// 限制文本框输入内容
- /// </summary>
- /// <param name="e"></param>
- private void PreviewKeyDownEvent(object e)
- {
- var args = e as KeyEventArgs;
- if (args == null)
- {
- return;
- }
- 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)))
- {
- args.Handled = true;
- }
- }
- /// <summary>
- /// 响应回车确认事件
- /// </summary>
- /// <param name="e"></param>
- private void KeyDownEvent(object e)
- {
- var args = e as KeyEventArgs;
- if (args == null)
- {
- return;
- }
- if (args.Key == Key.Enter)
- {
- List<int> TargetPageIndexs = new List<int>();
- var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
- if (!result&&ComboxSelectIndex==5)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("","请输入有效范围","OK");
- return;
- }
- //选中输入的页面范围
- List<int> pageRange = new List<int>();
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
- {
- pageRange.Add(i+1);
- }
- }
- NotifyUIToRefresh(pageRange);
- }
- }
- /// <summary>
- /// 失去焦点后选中页面
- /// </summary>
- /// <param name="obj"></param>
- private void lostfocus(object obj)
- {
- var args = obj as RoutedEventArgs;
- if (args == null||(args.OriginalSource as TextBox)==null)
- {
- return;
- }
- List<int> TargetPageIndexs = new List<int>();
- if(PageRangeItems.Contains((args.OriginalSource as TextBox).Text)&&ComboxSelectIndex!=5)
- {
- //选中其他项,失去焦点时不报错
- return;
- }
- var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
- if (!result)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", "请输入有效范围", "OK");
- return;
- }
- //选中输入的页面范围
- List<int> pageRange = new List<int>();
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
- {
- pageRange.Add(i + 1);
- }
- }
- NotifyUIToRefresh(pageRange);
- }
- #endregion
- #region 方法
- /// <summary>
- /// Esc退出多选
- /// </summary>
- private void clearSelected()
- {
- ListSelectedIndex = -1;
- }
- /// <summary>
- /// 调用打印机
- /// </summary>
- private void print()
- {
- PDFViewer.GoToPage(listSelecedIndex);
- DialogParameters printValue = new DialogParameters();
- printValue.Add(ParameterNames.PDFViewer, PDFViewer);
- printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
- printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
- printValue.Add(ParameterNames.PageList,selectedPageList);
- dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
- }
- /// <summary>
- /// 剪切
- /// </summary>
- private void cut()
- {
- CacheFilePath.Instance.CopyDoc = null;
- CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
- List<int> pages = new List<int>();
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (PageEditItems[i].Selected)
- {
- pages.Add(i + 1);
- }
- }
- bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
- if (!result)
- {
- ShowToast("复制失败");
- return;
- }
- DoRemoveSelectedPages();
- ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
- }
- /// <summary>
- /// 复制
- /// </summary>
- private void copy()
- {
- //付费锁
- if (!App.IsLogin)
- {
- dialogs.ShowDialog(DialogNames.IAPCompareDialog);
- return;
- }
- CacheFilePath.Instance.CopyDoc = null;
- CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
- List<int> pages = new List<int>();
- for(int i=0;i<PageEditItems.Count;i++)
- {
- if(PageEditItems[i].Selected)
- {
- pages.Add(i + 1);
- }
-
- }
- bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",",pages));
- if (!result)
- {
- ShowToast("复制失败");
- return;
- }
- }
- /// <summary>
- /// 是否可以粘贴
- /// </summary>
- /// <returns></returns>
- private bool CanPasteExcute()
- {
- return this.CanPaste;
- }
- /// <summary>
- /// 粘贴
- /// </summary>
- private async void paste()
- {
- //付费锁
- if (!App.IsLogin)
- {
- dialogs.ShowDialog(DialogNames.IAPCompareDialog);
- return;
- }
- IsLoading = Visibility.Visible;
- int insertindex = maxSelectedIndex > listSelecedIndex ? maxSelectedIndex : listSelecedIndex;
- insertindex++;
- int pagecount = CacheFilePath.Instance.CopyDoc.PageCount;
- bool result = PDFViewer.Document.ImportPagesAtIndex(CacheFilePath.Instance.CopyDoc, "1-" + pagecount, insertindex);
- if (!result)
- {
- IsLoading = Visibility.Collapsed;
- ShowToast("粘贴失败");
- return;
- }
- //UI层插入
- for (int i = 0; i < pagecount; i++)
- {
- var item = GetNewPageEditItem(i);
- PageEditItems.Insert(insertindex + i, item);
- }
- //加一点延时才能显示选中效果
- await Task.Delay(200);
- List<int> pageRange = new List<int>();
- for (int i = 0; i < pagecount; i++)
- {
- pageRange.Add(insertindex + i + 1);
- }
- NotifyUIToRefresh(pageRange);
- //刷新页码等
- ReloadAfterOption(true, true, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
- NotifyUIToRefresh(pageRange);
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 页面编辑执行Undo操作的方法
- /// </summary>
- private async void Undo()
- {
- var history = UndoStack.Pop();
- PageEditHistory redoHistory = new PageEditHistory();
- switch (history.Type)
- {
- case ActionType.RightRotate:
- var rightlist = history.SourceTargetValue.Keys.ToList<int>();
- RotateSelectedPages(false, rightlist,true);
- redoHistory.SourceTargetValue = history.SourceTargetValue;
- redoHistory.Type = ActionType.LeftRotate;
- break;
- case ActionType.LeftRotate:
- var leftlist = history.SourceTargetValue.Keys.ToList<int>();
- RotateSelectedPages(true, leftlist,true);
- redoHistory.SourceTargetValue = history.SourceTargetValue;
- redoHistory.Type = ActionType.RightRotate;
- break;
- case ActionType.InsertFromFile:
- List<int> pageList = new List<int>();
- for(int i=0;i<history.InsertPageCount;i++)
- {
- pageList.Add(history.InsertIndex+i);
- }
- DoRemoveSelectedPages(pageList, true);
- redoHistory = history;
- break;
- case ActionType.InsertCustomPages:
- DoRemoveSelectedPages(new List<int> {history.InsertIndex},true);
- redoHistory = history;
- break;
- case ActionType.DragSort:
- //将字典倒序后,将每个Source的值恢复到Key的位置
- var SourceTargets = history.SourceTargetValue;
- var keys = SourceTargets.Keys.ToArray();
- Array.Reverse(keys);
- foreach(var key in keys)
- {
- MoveItemToIndex(SourceTargets[key], key);
- }
- //刷新页码等
- ReloadAfterOption(true,true,new Tuple<int, int>(0,PDFViewer.Document.PageCount));
- List<int> selectedPages = new List<int>();
- //将索引集合转换成页码集合
- foreach(var key in SourceTargets.Keys)
- {
- selectedPages.Add(key+1);
- }
- //通知UI选中Item
- NotifyUIToRefresh(selectedPages);
- redoHistory = history;
- break;
- case ActionType.Reverse:
- //将字典倒序后,将每个Source的值恢复到Key的位置
- var SourceTarget = history.SourceTargetValue;
- var pres = SourceTarget.Keys.ToArray();
- foreach (var key in pres)
- {
- if (key != SourceTarget[key])
- {
- ExchangeItem(key, SourceTarget[key]);
- }
- }
- await Task.Delay(20);
- //刷新页码等
- ReloadAfterOption(true, true, null);
- List<int> Pages = new List<int>();
- //将索引集合转换成页码集合
- foreach (var key in SourceTarget.Keys)
- {
- Pages.Add(key + 1);
- }
- foreach(var key in SourceTarget.Values)
- {
- Pages.Add(key + 1);
- }
- //通知UI选中Item
- NotifyUIToRefresh(Pages);
- redoHistory = history;
- break;
- default:
- break;
- }
- RedoStack.Push(redoHistory);
- CanRedo = true;
- //检查是否可以再进行Undo
- if(UndoStack.Count<=0)
- {
- this.CanUndo = false;
- }
- }
- /// <summary>
- /// 页面编辑执行Redo操作的方法
- /// </summary>
- private async void Redo()
- {
- var history = RedoStack.Pop();
- PageEditHistory undoHistory = new PageEditHistory();
- switch (history.Type)
- {
- case ActionType.RightRotate:
- var rightlist = history.SourceTargetValue.Keys.ToList<int>();
- RotateSelectedPages(false, rightlist,true);
- undoHistory.SourceTargetValue = history.SourceTargetValue;
- undoHistory.Type = ActionType.LeftRotate;
- break;
- case ActionType.LeftRotate:
- var leftlist = history.SourceTargetValue.Keys.ToList<int>();
- RotateSelectedPages(true, leftlist,true);
- undoHistory.SourceTargetValue = history.SourceTargetValue;
- undoHistory.Type = ActionType.RightRotate;
- break;
- case ActionType.InsertFromFile:
- await InsertFromFile(history.InsertIndex,history.InsertFilePath,history.InsertFilePassWord,true);
- ReloadAfterOption(true, true, Tuple.Create(history.InsertIndex-1, history.InsertIndex+history.InsertPageCount));
- undoHistory = history;
- break;
- case ActionType.InsertCustomPages:
- InsertFromCustomPage(history.InsertIndex, history.PageSize.Width, history.PageSize.Height, history.InsertFilePath);
- ListSelectedIndex = history.InsertIndex;
- undoHistory = history;
- break;
- case ActionType.DragSort:
- DragToSort(history.InsertIndex,history.SourceTargetValue.Keys.ToList<int>(),true);
- undoHistory = history;
- break;
- case ActionType.Reverse:
- var list = history.SourceTargetValue.Keys.ToList<int>().Union(history.SourceTargetValue.Values.ToList<int>()).ToList<int>();
- DoReverse(list, true);
- undoHistory = history;
- break;
- default:
- break;
- }
- UndoStack.Push(undoHistory);
- CanUndo = true;
- if (RedoStack.Count <= 0)
- {
- this.CanRedo = false;
- }
- }
- /// <summary>
- /// 退出页面编辑面试,回到阅读页
- /// </summary>
- public void BackToPDFViewer(int PageIndex)
- {
- PDFViewer.GoToPage(PageIndex);
- viewContentViewModel.ExitToolMode();
- }
- /// <summary>
- /// 显示Toast提示
- /// </summary>
- private void ShowToast(string content="")
- {
- if (!string.IsNullOrEmpty(content))
- {
- ToastText = content;
- }
- else
- {
- ToastText = "操作失败";
- }
- ShowTip = Visibility.Visible;
- }
- /// <summary>
- /// 通知UI刷新页面范围
- /// 或通过xaml来设置选中项
- /// </summary>vb nmjvcx
- private void NotifyUIToRefresh(List<int> pageRange=null)
- {
- if (pageRange == null || pageRange.Count <= 0)
- {
- //通知UI刷新页面范围
- eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
- }
- else
- {
- //通知UI刷新页面范围
- eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode,NotifyType.SelectItems,pageRange));
- }
- }
- /// <summary>
- /// 获取已选中的页面集合
- /// </summary>
- /// <returns></returns>
- private List<int> getSelectedPage()
- {
- List<int> pages = new List<int>();
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (PageEditItems[i].Selected)
- {
- pages.Add(i);
- }
- }
- return pages;
- }
- /// <summary>
- /// 删除选中项的方法 不带Reload操作
- /// 方便其他逻辑中调用
- /// </summary>
- private void DoRemoveSelectedPages(List<int> pageList = null, bool isFromUndoRedo = false)
- {
- //付费锁
- if (!App.IsLogin)
- {
- dialogs.ShowDialog(DialogNames.IAPCompareDialog);
- return;
- }
- List<int> indexList = new List<int>();
- if (pageList == null || pageList.Count < 0)
- {
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (PageEditItems[i].Selected)
- {
-
- indexList.Add(i);
-
- }
- }
- }
- else
- {
- indexList = pageList;
- }
-
- if(indexList.Count>0&& indexList.Count==PDFViewer.Document.PageCount)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("","不能删除所有页面","OK");
- return;
- }
- var result = PDFViewer.Document.RemovePages(indexList.ToArray());
- if (!result)
- {
- ShowToast();
- return;
- }
- //文档层操作成功后再删除UI层
- for (int i = indexList.Count - 1; i >= 0; i--)
- {
- PageEditItems.RemoveAt(indexList[i]);
- }
- if (!isFromUndoRedo)
- {
- //清除页面编辑Undo,Redo栈
- UndoStack.Clear();
- RedoStack.Clear();
- this.CanUndo = false;
- this.CanRedo = false;
- }
- }
- /// <summary>
- /// 提取选中页面的方法
- /// </summary>
- /// <param name="isEveryPageToFile">是否每页生成一个文件</param>
- /// <param name="isDeleteAfterExtract">是否提取后删除页面</param>
- private async void DoExtractPages(bool isEveryPageToFile,bool isDeleteAfterExtract)
- {
- System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(dialog.SelectedPath))
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("","","OK");//补充提示文案
- return;
- }
- }
- else
- return;
- string selectedfile = "";
- IsLoading = Visibility.Visible;
- await Task.Delay(3);
- List<int> pagenums = new List<int>();
- for(int i=0;i<PageEditItems.Count;i++)
- {
- if(PageEditItems[i].Selected)
- {
- pagenums.Add(PageEditItems[i].PageNumber);
- }
- }
- if (isEveryPageToFile)
- {
- //每页生成的单独的文件
- for (int i = 0; i < pagenums.Count; i++)
- {
- string filename = PDFViewer.Document.FileName + " " + pagenums[i] + ".pdf";
- string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
- path = CommonHelper.CreateFilePath(path);
- selectedfile = path;
- CPDFDocument savedoc = CPDFDocument.CreateDocument();
- bool result = savedoc.ImportPages(PDFViewer.Document, pagenums[i].ToString());
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- result = savedoc.WriteToFilePath(path);
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- savedoc.Release();//释放内存
- }
- }
- else//全部生成一个文件
- {
- string filename = PDFViewer.Document.FileName + " " + CommonHelper.GetPageParmFromList(pagenums) + ".pdf";
- string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
- path = CommonHelper.CreateFilePath(path);
- selectedfile = path;
- CPDFDocument savedoc = CPDFDocument.CreateDocument();
- string range = String.Join(",", pagenums);
- bool result = savedoc.ImportPages(PDFViewer.Document, range);
- if (!result)
- {
- ShowToast();
- IsLoading = Visibility.Collapsed;
- return;
- }
- result = savedoc.WriteToFilePath(path);
- if (!result)
- {
- ShowToast();
- }
- savedoc.Release();//释放内存
- }
- if(isDeleteAfterExtract)
- {
- DoRemoveSelectedPages();
- ReloadAfterOption(true,true,Tuple.Create(0,PDFViewer.Document.PageCount));
- }
- CommonHelper.ShowFileBrowser(selectedfile);
- IsLoading = Visibility.Collapsed;
- }
- private void DoSplitPages(HomePageSplitDialogModel model)
- {
- try
- {
- var pdfViewer = PDFViewer;
- System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
- //dialog.Description = App.MainPageLoader.GetString("Main_OpenFolderNoteWarning"); ;
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(dialog.SelectedPath))
- {
- return;
- }
- }
- else
- return;
- var data = model;
- int PageCount = pdfViewer.Document.PageCount;
- string selectedfile = "";
- if (data.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
- {
- int perPages = Convert.ToInt32(data.GetModeCount);
- int SplitCount = (int)Math.Ceiling((double)PageCount / (double)perPages);
- //TODO:命名规则是否要调整
- selectedfile = data.FileName + " " + "1.pdf";
- for (int i = 0; i < SplitCount; i++)
- {
- int startNum = i * perPages + 1;
- int endNum = Math.Min(PageCount, (i + 1) * perPages);
- string range = startNum.ToString() + "-" + endNum.ToString();
- CPDFDocument savedoc = CPDFDocument.CreateDocument();
- bool result = savedoc.ImportPages(pdfViewer.Document, range);
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- string filename = data.FileName + " " + (i + 1) + ".pdf";
- string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
- path = CommonHelper.CreateFilePath(path);
- result = savedoc.WriteToFilePath(path);
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- savedoc.Release();
- selectedfile = path;
- }
- }
- else if (data.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
- {
- int docCount = Convert.ToInt32(data.GetModeCount);
- int avePages = (int)Math.Floor((double)PageCount / (double)docCount);
- int remain = PageCount % docCount;
- int startNum = 1;
- //TODO:命名规则是否要调整
- selectedfile = data.FileName + " " + "1.pdf";
- for (int i = 0; i < docCount; i++)
- {
- int endNum = startNum + (avePages - 1);
- if (i < remain) endNum++;
- endNum = Math.Min(endNum, PageCount);
- string range = startNum + "-" + endNum;
- startNum = endNum + 1;
- string fileName = data.FileName + " " + (i + 1) + ".pdf";
- string path = System.IO.Path.Combine(dialog.SelectedPath, fileName);
- path = CommonHelper.CreateFilePath(path);
- CPDFDocument savedoc = CPDFDocument.CreateDocument();
- bool result = savedoc.ImportPages(pdfViewer.Document, range);
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- result = savedoc.WriteToFilePath(path);
- if (!result)
- {
- savedoc.Release();
- continue;
- }
- savedoc.Release();
- selectedfile = path;
- }
- }
- else//自定义页码范围
- {
- string pageRange="";
- if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.OddPage)
- {
- int[] page = new int[(PageCount + 1) / 2];
- for (int i = 0; i < page.Length; i++)
- {
- page[i] = i * 2 + 1;
- }
- pageRange = string.Join(",", page);
- }
- else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.EvenPage)
- {
- int[] page = new int[PageCount / 2];
- for (int i = 0; i < page.Length; i++)
- {
- page[i] = i * 2 + 2;
- }
- pageRange = string.Join(",", page);
- }
- List<int> pageList1 = new List<int>();
- //如果是自定义输入页面范围的话,直接解析
- CommonHelper.GetPagesInRange(ref pageList1, pageRange, PageCount, new char[] { ',' }, new char[] { '-' });
- //part1
- selectedfile = "";
- CPDFDocument saveDoc1 = CPDFDocument.CreateDocument();
- string filepath = data.FileName + " " + "1.pdf";
- string path1 = Path.Combine(dialog.SelectedPath, filepath);
- path1 = CommonHelper.CreateFilePath(path1);
- bool result = saveDoc1.ImportPages(pdfViewer.Document, pageRange);
- if (!result)
- {
- saveDoc1.Release();
- ShowToast();
- return;
- }
- result = saveDoc1.WriteToFilePath(path1);
- if (!result)
- {
- saveDoc1.Release();
- ShowToast();
- return;
- }
- saveDoc1.Release();
- //Part2
- List<int> pageList2 = new List<int>();
- for (int i = 1; i <= pdfViewer.Document.PageCount; i++)
- {
- if (!pageList1.Contains(i - 1))//pagelist1 存放的是index
- pageList2.Add(i);
- }
- string pageRange2 = string.Join(",", pageList2);
- CPDFDocument saveDoc2 = CPDFDocument.CreateDocument();
- string filepath2 = data.FileName + " " + "2.pdf";
- string path2 = Path.Combine(dialog.SelectedPath, filepath2);
- path2 = CommonHelper.CreateFilePath(path2);
- result = saveDoc2.ImportPages(pdfViewer.Document, pageRange2);
- if (!result)
- {
- saveDoc2.Release();
- ShowToast();
- return;
- }
- result = saveDoc2.WriteToFilePath(path2);
- if (!result)
- {
- saveDoc2.Release();
- ShowToast();
- return;
- }
- selectedfile = path1;
- saveDoc2.Release();
- }
- //显示文件夹,并选中一个文件
- Process.Start("explorer", "/select,\"" + selectedfile + "\"");
- }
- catch (Exception ex)
- {
- }
- }
- /// <summary>
- /// 将文件插入到指定位置
- /// </summary>
- /// <param name="insertIndex"></param>
- public async Task<bool> InsertFromFile(int insertIndex, string filePath, string oldPassWord = null, bool isFromUndoRedo = false)
- {
- CPDFDocument doc = CPDFDocument.InitWithFilePath(filePath);
- if (doc == null)
- {
- ShowToast();
- return false;
- }
- string password = "";
- if (doc.IsLocked)
- {
- if(!string.IsNullOrEmpty(oldPassWord))
- {
- var result = doc.UnlockWithPassword(oldPassWord);
- if(result)
- {
- password = oldPassWord;
- }
- }
- //没有传入密码,或者用传入密码解密失败是,需要弹出界面弹窗
- if (doc.IsLocked)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFDocument, doc);
- bool result = true;
- dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
- {
- if (e.Result != ButtonResult.OK)
- {
- result = false;
- }
- password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
- });
- if (!result)
- {
- return false;
- }
- }
- }
- int pageCount = doc.PageCount;
- //插入整个新文档
- //文档层插入
- var insertresult = PDFViewer.Document.ImportPagesAtIndex(doc, "1-" + pageCount, insertIndex);
- if (!insertresult)
- {
- ShowToast();
- return false;
- }
- //插入成功后释放临时Document对象
- doc.Release();
- //UI层插入
- for (int i = 0; i < pageCount; i++)
- {
- var item = GetNewPageEditItem(i);
- PageEditItems.Insert(insertIndex + i, item);
- }
- //加一点延时才能显示选中效果
- await Task.Delay(200);
- List<int> pageRange = new List<int>();
- for (int i = 0; i < pageCount; i++)
- {
- pageRange.Add(insertIndex+i+1);
- }
- NotifyUIToRefresh(pageRange);
- //如果是undo redo里调用本方法,则不需要添加到Undo栈
- if (!isFromUndoRedo)
- {
- //添加记录到Undo栈,清空Redo栈
- PageEditHistory history = new PageEditHistory();
- history.Type = ActionType.InsertFromFile;
- history.InsertIndex = insertIndex;
- history.InsertFilePath = filePath;
- history.InsertFilePassWord = password;
- history.InsertPageCount = pageCount;
- AddToUndoStack(history);
- }
- return true;
- }
- /// <summary>
- /// 添加记录到Undo栈
- /// </summary>
- /// <param name="history"></param>
- private void AddToUndoStack(PageEditHistory history)
- {
- UndoStack.Push(history);
- CanUndo = true;
- RedoStack.Clear();
- CanRedo = false;
- }
- /// <summary>
- /// 插入自定义页面
- /// </summary>
- /// <param name="insertIndex"></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private bool InsertFromCustomPage(int insertIndex,double width,double height,string filePath)
- {
- try
- {
- bool result = false;
- //文档层插入
- if (!string.IsNullOrEmpty(filePath))
- {
- BitmapFrame frame = BitmapFrame.Create(new Uri(filePath));
- byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
- frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
- result = PDFViewer.Document.InsertPage(insertIndex, frame.Width, frame.Height, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
- }
- else
- {
- result = PDFViewer.Document.InsertPage(insertIndex, width, height, null);
- }
- if (!result)
- {
- ShowToast();
- return false;
- }
- PageEditItem item = new PageEditItem();
- item.Size = PDFViewer.Document.GetPageSize(insertIndex);
- PageEditItems.Insert(insertIndex, item);
- ReloadAfterOption(true, true, Tuple.Create(insertIndex - 1, insertIndex + 1));
- return true;
- }
- catch
- { return false; }
- }
- /// <summary>
- /// 根据索引创建新的Item
- /// </summary>
- /// <param name="index"></param>
- /// <returns></returns>
- private PageEditItem GetNewPageEditItem(int index)
- {
- PageEditItem item = new PageEditItem();
- if (PDFViewer.Document.PageAtIndex(index).Rotation % 2 != 0)
- {
- ///标记为横向页面
- item.isVertical = false;
- }
- item.PageNumber = index + 1;
- var pagesize = PDFViewer.Document.GetPageSize(index);
- item.Size = pagesize;
- //double width = CommonHelper.GetUnitsFromPageSize(pagesize.Width);
- //double height = CommonHelper.GetUnitsFromPageSize(pagesize.Height);
- //item.PageSize = $"{width.ToString("F0")}mm*{height.ToString("F0")} mm";
- return item;
- }
- /// <summary>
- /// 进行文件操作后刷新PDFView
- /// 清除undo 栈
- /// 重加载Document
- /// 设置保存状态
- /// </summary>
- /// <param name="needRefreshPageNum">是否需要刷新页码</param>
- /// <param name="needClearUndoHistory">是否需要清空注释Undo栈</param>
- /// <param name="pageRange">需要刷新的页面范围图片</param>
- public void ReloadAfterOption(bool needRefreshPageNum = false, bool needClearUndoHistory = true, Tuple<int, int> pageRange = null)
- {
- if (needRefreshPageNum)
- {
- RefreshPageNum();
- }
- if (needClearUndoHistory)
- {
- PDFViewer.UndoManager.ClearHistory();
- }
- if (pageRange != null)
- {
- if (pageRange.Item2 - pageRange.Item1 > 30)
- {
- //范围较大时,通知UI获取更准确的刷新范围
- NotifyUIToRefresh();
- }
- else
- {
- RefreshItemImage(pageRange.Item1, pageRange.Item2);
- }
- }
- PDFViewer.UndoManager.CanSave = true;
- PDFViewer.ReloadDocument();
- }
- /// <summary>
- /// 旋转所有选中页面
- /// </summary>
- /// <param name="rightRotate"></param>
- private async void RotateSelectedPages(bool rightRotate = true,List<int> pageitems=null,bool isFromUndoRedo = false)
- {
- //付费锁
- if (!App.IsLogin)
- {
- dialogs.ShowDialog(DialogNames.IAPCompareDialog);
- return;
- }
- IsLoading = Visibility.Visible;
- await Task.Delay(2);
- Dictionary<int, int> items = new Dictionary<int, int>();
- if (pageitems == null)
- {
- //旋转选中页面
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (PageEditItems[i].Selected)
- {
- //逆时针旋转传 - 1 顺时针旋转传1
- PDFViewer.Document.RotatePage(i, rightRotate ? 1 : -1);
- PDFViewer.Document.ReleasePages(i);
- items.Add(i, i);
- PageEditItems[i].Size = PDFViewer.Document.GetPageSize(i);
- //重新拿图
- PageEditItems[i].IsGetImage = false;
- }
- }
- }
- else
- {
- //旋转对应Items集合
- for (int i = 0; i < pageitems.Count; i++)
- {
- PDFViewer.Document.RotatePage(pageitems[i], rightRotate ? 1 : -1);
- PDFViewer.Document.ReleasePages(pageitems[i]);
- items.Add(pageitems[i], pageitems[i]);
- //重新拿图
- PageEditItems[pageitems[i]].IsGetImage = false;
- PageEditItems[i].Size = PDFViewer.Document.GetPageSize(i);
- }
- }
- //旋转页面后取消选中
- PDFViewer.ClearSelectPDFEdit(true);
- //如果是undo,Redo里调用的方法,则不记录到栈里
- if (!isFromUndoRedo)
- {
- //记录进Undo栈
- PageEditHistory history = new PageEditHistory();
- history.Type = rightRotate ? ActionType.RightRotate : ActionType.LeftRotate;
- history.SourceTargetValue = items;
- AddToUndoStack(history);
- }
- ReloadAfterOption(false, true, Tuple.Create(0, PageEditItems.Count));
- IsLoading = Visibility.Collapsed;
- }
- /// <summary>
- /// 刷新页码
- /// </summary>
- private void RefreshPageNum()
- {
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- PageEditItems[i].PageNumber = i + 1;
- PageEditItems[i].ShowPageSize = HasShowPageSize == Visibility.Visible ? true : false;
- }
-
- }
- /// <summary>
- /// 刷新页面范围下拉框
- /// </summary>
- private void RefreshPageRangeItem()
- {
- PageRangeItems.Clear();
- PageRangeItems.Add("所有页面");
- PageRangeItems.Add("奇数页");
- PageRangeItems.Add("偶数页");
- PageRangeItems.Add("横向页面");
- PageRangeItems.Add("纵向页面");
- PageRangeItems.Add("自定义");
- }
- /// <summary>
- /// 选中目标页面范围的页面
- /// </summary>
- /// <param name="index">页面范围选中项索引</param>
- private void SuitPageRange(int index)
- {
- List<int> pageRange = new List<int>();
- switch (index)
- {
- case 0:
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- pageRange.Add(i+1);
- }
- break;
- case 1:
- //奇数页
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (i % 2 == 0)
- {
- pageRange.Add(i + 1);
- }
- }
- break;
- case 2:
- //偶数页
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (i % 2 != 0)
- {
- pageRange.Add(i + 1);
- }
- }
- break;
- case 3://横向页面
- ListSelectedIndex = -1;
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (!PageEditItems[i].isVertical)
- {
- pageRange.Add(i + 1);
- }
- }
- break;
- case 4://纵向页面
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- if (PageEditItems[i].isVertical)
- {
- pageRange.Add(i + 1);
- }
- }
- break;
- case 5://自定义
- //await Task.Delay(20);
- //TbPageRange = "1,3";
- break;
- default:
- break;
- }
- if(pageRange.Count>0)
- {
- NotifyUIToRefresh(pageRange);
- }
- else
- {
- if (index != 5)
- {
- //没有符合条件时 清空选中项
- ListSelectedIndex = -1;
- }
- }
- }
- /// <summary>
- /// 是否显示页面尺寸
- /// </summary>
- /// <param name="show">是或否</param>
- private void ShowPageSize()
- {
- bool show = true;
- if(HasShowPageSize== Visibility.Visible)
- {
- show = false;
- HasShowPageSize = Visibility.Collapsed;
- }
- else
- {
- show = true;
- HasShowPageSize = Visibility.Visible;
- }
- for (int i = 0; i < PageEditItems.Count; i++)
- {
- PageEditItems[i].ShowPageSize = show;
- }
- }
- /// <summary>
- /// 从PDFView获取所有Items集合
- /// </summary>
- private void GetSourceItems(bool isFirstLoad)
- {
- if (isFirstLoad||IsBOTAThumb==Visibility.Visible)
- {
- PageEditItems.Clear();
- for (int i = 0; i < PDFViewer.Document.PageCount; i++)
- {
- var item = GetNewPageEditItem(i);
- PageEditItems.Add(item);
- }
- }
- //else
- //{
- // //后续进来仅刷新图片即可
- // for (int i = 0; i < PageEditItems.Count; i++)
- // {
- // PageEditItems[i].IsGetImage = false;
- // }
- //}
- }
- /// <summary>
- /// 刷新书签列表
- /// </summary>
- public void RefreshBookMarkList()
- {
- if (PDFViewer != null)
- {
- var booklist = PDFViewer.Document.GetBookmarkList();
- if (booklist == null)
- {
- return;
- }
- //获取所有书签的Index集合
- List<int> marks = new List<int>();
- for (int i = 0; i < booklist.Count; i++)
- {
- marks.Add(booklist[i].PageIndex);
- }
- //不能只遍历marks集合,考虑书签集合元素删除的情况
- for (int k = 0; k < PageEditItems.Count; k++)
- {
- if (marks.Contains(k))
- PageEditItems[k].HaveBookMark = true;
- else
- PageEditItems[k].HaveBookMark = false;
- }
- }
- }
- /// <summary>
- /// 刷新指定范围的图片
- /// </summary>
- /// <param name="startIndex"></param>
- /// <param name="endIndex"></param>
- private async void RefreshItemImage(int startIndex, int endIndex)
- {
- for (int i = startIndex; i <= endIndex; i++)
- {
- if (i <= PageEditItems.Count- 1 && i >= 0 && PageEditItems.Count > 0)
- {
- //宽高为样式中图片控件宽高
- if (PageEditItems[i].isVertical)
- {
- //等比例缩放
- if (PageEditItems[i].Size.Height > 300)
- {
- await PDFViewer.GetThumbnail(i, (int)(PageEditItems[i].Size.Width / (PageEditItems[i].Size.Height / 300)), 300);
- }
- else
- {
- await PDFViewer.GetThumbnail(i, (int)PageEditItems[i].Size.Width, (int)PageEditItems[i].Size.Height);
- }
- }
- else
- {
- if (PageEditItems[i].Size.Width > 300)
- {
- await PDFViewer.GetThumbnail(i, 300, (int)(PageEditItems[i].Size.Height / (PageEditItems[i].Size.Width / 300)));
- }
- else
- {
- //获取横图
- await PDFViewer.GetThumbnail(i, (int)PageEditItems[i].Size.Width, (int)PageEditItems[i].Size.Height);
- }
- }
- }
- }
- }
- /// <summary>
- /// 拖拽排序功能
- /// </summary>
- /// <param name="targetIndex"></param>
- /// <param name="pageIndexs"></param>
- public void DragToSort(int targetIndex,List<int> pageIndexs,bool IsFromUndoRedo =false)
- {
- //拖拽排序后需要重新选中项的集合
- List<int> selectedPages = new List<int>();
- //记录原位置和目标位置的字典,用于Undo,Redo恢复
- Dictionary<int, int> SourceTargetValue = new Dictionary<int, int>();
- bool result = false;
- //排序前,先对所有页码排序
- pageIndexs.Sort();
- //要考虑每一次交换都会导致局部页码发生改变
- //每次整体往后移动时,先移动大页码;整体往前移动时,先移动小页码;往中间移动时,再按上述两种情况分别移动
- if (targetIndex <= pageIndexs[0])
- {
- for (int i = 0; i < pageIndexs.Count; i++)
- {
- // list.Add(PageEditListBox.Items[pageIndexs[i]] as ListBoxItem);
- //sourceindex.Add(pageIndexs[i]);
- result = MoveItemToIndex(pageIndexs[i], targetIndex + i);
- if(!result)
- {
- return;
- }
- SourceTargetValue.Add(pageIndexs[i], targetIndex + i);
- selectedPages.Add(targetIndex + i+1);
- }
- }
- else if (targetIndex > pageIndexs[pageIndexs.Count - 1])
- {
- //目标位置在所有选中内容右边 整体后移
- for (int i = 0; i < pageIndexs.Count; i++)
- {
- result = MoveItemToIndex(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
- if (!result)
- {
- return;
- }
- SourceTargetValue.Add(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
- selectedPages.Add(targetIndex - i);
- }
- }
- else//目标位置在所有选中项中间
- {
- int i, j, k;
- //找出PageEditListBox.Items中页码等于destpage的下标
- for (k = 0; k < pageIndexs.Count - 1; k++)
- {
- //这里要算入K---即前面部分的页面个数
- if (pageIndexs[k] < targetIndex && pageIndexs[k + 1] >= targetIndex)
- {
- break;
- }
- }
- //局部往后移动
- for (i = 0; i <= k; i++)
- {
- result = MoveItemToIndex(pageIndexs[k - i], targetIndex - 1 - i);
- if (!result)
- {
- return;
- }
- SourceTargetValue.Add(pageIndexs[k - i], targetIndex - 1 - i);
- selectedPages.Add(targetIndex - i);
- }
- int newIndex = targetIndex;
- //局部往前移动
- for (j = i; j < pageIndexs.Count; j++)
- {
- result = MoveItemToIndex(pageIndexs[j], newIndex);
- if (!result)
- {
- return;
- }
- SourceTargetValue.Add(pageIndexs[j], newIndex);
- selectedPages.Add(newIndex + 1);
- newIndex++;
- }
- }
- //通知UI选中Items
- //NotifyUIToRefresh(selectedPages);
- //刷新页码和图片
- ReloadAfterOption(true,true,new Tuple<int, int>(0,PDFViewer.Document.PageCount));
- if (!IsFromUndoRedo)
- {
- //存入Undo栈
- PageEditHistory history = new PageEditHistory();
- history.Type = ActionType.DragSort;
- history.SourceTargetValue = SourceTargetValue;
- //用于Redo恢复
- history.InsertIndex = targetIndex;
- AddToUndoStack(history);
- }
- }
- /// <summary>
- /// 移动某个Item到指定位置
- /// </summary>
- private bool MoveItemToIndex(int sourceindex, int targetindex)
- {
- if (targetindex == sourceindex || targetindex < 0)
- {
- return false;
- }
- //文档层移动
- var result = PDFViewer.Document.MovePage(sourceindex, targetindex);
- if (!result)
- {
- return false;
- }
- //UI层移动
- var source = PageEditItems[sourceindex];
- PageEditItems.RemoveAt(sourceindex);
- PageEditItems.Insert(targetindex, source);
- return true;
- }
- /// <summary>
- /// 从底层库获取对应页面的图片
- /// </summary>
- /// <param name="pageIndex"></param>
- /// <param name="thumb"></param>
- /// <param name="w"></param>
- /// <param name="h"></param>
- private void PDFViewer_OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
- {
- try
- {
- if (PageEditItems.Count >= pageIndex + 1)
- {
- PixelFormat fmt = PixelFormats.Bgra32;
- BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
- PageEditItems[pageIndex].Image = bps;
- PageEditItems[pageIndex].IsGetImage = true;
- }
- }
- catch
- {
- }
- }
- #endregion
- #region Navigate
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- /// <summary>
- /// 从其他页面导航过来的时候会触发
- /// 考虑只有在页面编辑的时候会进行页面的删减,可以尝试逻辑优化:仅第一次进入页面编辑时重新刷新页面
- /// </summary>
- /// <param name="navigationContext"></param>
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- bool isBOTA = false;
- navigationContext.Parameters.TryGetValue<bool>(ParameterNames.BOTAThumb, out isBOTA);
- if (PDFViewer != null)
- {
- if (isFirstLoad)
- {
- PDFViewer.OnThumbnailGenerated -= PDFViewer_OnThumbnailGenerated;
- PDFViewer.OnThumbnailGenerated += PDFViewer_OnThumbnailGenerated;
- PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
- PDFViewer.InfoChanged += PDFViewer_InfoChanged;
- PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
- PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
- ZoomIndex = 0;
- if(viewContentViewModel!=null)
- {
- viewContentViewModel.PageEditUndo = Undo;
- viewContentViewModel.PageEditRedo = Redo;
- }
- }
- //BOTA缩略图时,显示不同的菜单栏
- if(isBOTA)
- {
- IsBOTAThumb = Visibility.Visible;
- }
- else
- {
- IsBOTAThumb = Visibility.Collapsed;
- }
- GetSourceItems(isFirstLoad);
- ListSelectedIndex = PDFViewer.CurrentIndex;
- viewContentViewModel.CanUndo = this.CanUndo;
- viewContentViewModel.CanRedo = this.CanRedo;
- RefreshBookMarkList();
- isFirstLoad = false;
- }
- }
- private void PDFViewer_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
- {
- if (e != null && e.Count > 0)
- {
- int pageIndex = e[0].PageIndex;
- RefreshItemImage(pageIndex, pageIndex);
- }
- }
- private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
- {
- if(e.Key== "PageNum")
- {
- NotifyUIToRefresh(new List<int> { (e.Value as ComPDFKitViewer.RenderData).PageIndex});
- }
- }
- #endregion
- }
- }
|