using ComPDFKit.PDFDocument; using ComPDFKit.PDFDocument.Action; using ComPDFKit.PDFPage; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using PDF_Office.Model.BOTA; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace PDF_Office.ViewModels.BOTA { class OutLineControlViewModel : BindableBase, INavigationAware { private CPDFViewer PDFViewer; public ObservableCollection Outlinelist { get; set; } private bool isInsertHead = false; public bool IsInsertHead { get { return isInsertHead; } set { SetProperty(ref isInsertHead, value); } } public OutLineControlViewModel() { Outlinelist = new ObservableCollection(); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { return; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer == null) { return; } List datasource = PDFViewer.Document.GetOutlineList(); Outlinelist.Clear(); foreach (CPDFOutline item in datasource) { OutlineNode dto = ConvertCPDFToOutlineNode(item, null,false); if (dto != null) { Outlinelist.Add(dto); } } } public void Updata(bool IsMoveData) { PDFViewer.Document.ReleaseOutlineList(); ObservableCollection list = new ObservableCollection(); List datasource = PDFViewer.Document.GetOutlineList(); //遍历,转换UI状态存入临时数组 foreach (CPDFOutline item in datasource) { OutlineNode dto = ConvertCPDFToOutlineNode(item, null, IsMoveData); if (dto != null) { list.Add(dto); } } //清空绑定数组,并填充新数据 Outlinelist.Clear(); foreach (OutlineNode item in list) { Outlinelist.Add(item); } PDFViewer.UndoManager.CanSave = true; } public bool SetTitle(CPDFOutline cPDFOutline, string Title) { if (cPDFOutline == null) { return false; } return cPDFOutline.SetTitle(Title); } /// /// 插入大纲到目标节点 /// /// 目标节点 /// 需要插入的数据 /// public bool InsertOutLine(OutlineNode target, OutlineNode soure) { bool Tag = true; if (IsInsertHead) { CPDFOutline root = PDFViewer.Document.GetOutlineRoot(); if (root != null) { Tag = root.MoveChildAtIndex(PDFViewer.Document, soure.Outline, 0); } } else { if (target.IsInsertNextLayer) { target.IsExpanded = true; Tag = target.Outline.MoveChildAtIndex(PDFViewer.Document, soure.Outline, target.Outline.ChildList.Count); } else { CPDFOutline parent = target.Outline.GetParent(); if (parent != null && parent.ChildList.Count > 0) { int index = parent.ChildList.IndexOf(target.Outline); int sourceindex = parent.ChildList.IndexOf(soure.Outline); //源数据不在目标节点树中的情况 if (sourceindex == -1) { Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1); } //分向上和向下移动 else if (sourceindex > index) { Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index + 1); } else { Tag = parent.MoveChildAtIndex(PDFViewer.Document, soure.Outline, index); } } } } return Tag; } public WriteableBitmap WirteBitmap; byte[] bmpData; public WriteableBitmap LoadPreview() { CPDFPage page = PDFViewer.Document.PageAtIndex(1); Size size = PDFViewer.Document.GetPageSize(1); bmpData = new byte[(int)(size.Width * size.Height * 4)]; WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null); if (page.IsValid()) { page.RenderPageBitmapWithMatrix((float)0.5, new Rect(0, 0, (int)size.Width, (int)size.Height), 0xFFFFFFFF, bmpData, 1, true); } WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0); WirteBitmap.Freeze(); return WirteBitmap; } private OutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, OutlineNode parent,bool IsMoveData) { OutlineNode node = new OutlineNode(); if (outline != null) { node.IsInsertNextLayer = false; node.IsInsertCurrentLayer = false; node.IsExpanded = false; node.Outline = outline; CPDFAction action = outline.GetAction(); if (action != null) { if (action.ActionType != C_ACTION_TYPE.ACTION_TYPE_URI) { CPDFDestination cPDFDestination = outline.GetDestination(PDFViewer.Document); if (cPDFDestination != null) { node.PageIndex = (cPDFDestination.PageIndex + 1).ToString(); } else { node.PageIndex = ""; } } else { node.PageIndex = ""; } } else { node.PageIndex = ""; } if (parent != null) { node.Parent = parent; } if (Outlinelist != null && Outlinelist.Count > 0) { OutlineNode oldnode = FindOutlineFromDto(Outlinelist, node.Outline, IsMoveData); if (oldnode != null) { node.IsExpanded = oldnode.IsExpanded; } } node.Chlidlist = new ObservableCollection(); if (outline.ChildList.Count > 0) { foreach (CPDFOutline item in outline.ChildList) { node.Chlidlist.Add(ConvertCPDFToOutlineNode(item, node, IsMoveData)); } } } return node; } public OutlineNode FindOutlineFromDto(ObservableCollection list, CPDFOutline outline,bool IsMoveData) { foreach (OutlineNode item in list) { CPDFOutline parent = outline.GetParent(); CPDFOutline PARENT = item.Outline.GetParent(); int i = GetIndexFromParent(parent.ChildList, outline); int I = GetIndexFromParent(PARENT.ChildList, item.Outline); if (IsMoveData) { if (item.Outline.Title == outline.Title) { return item; } else if (item.Chlidlist.Count > 0) { OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData); if (retdto != null) { return retdto; } } } else { if (item.Outline.Title == outline.Title && i == I && outline.Level == item.Outline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null))) { return item; } else if (item.Chlidlist.Count > 0) { OutlineNode retdto = FindOutlineFromDto(item.Chlidlist, outline, IsMoveData); if (retdto != null) { return retdto; } } } } return null; } private int GetIndexFromParent(List parentlist, CPDFOutline outline) { for (int i = 0; i < parentlist.Count; i++) { if (parentlist[i] == outline) { return i; } } return -1; } } }