123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFDocument.Action;
- using ComPDFKit.Tool;
- using Compdfkit_Tools.PDFControlUI;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Xml.Linq;
- namespace Compdfkit_Tools.PDFControl
- {
- public partial class CPDFOutlineControl : UserControl
- {
- PDFViewControl ViewControl;
- public ObservableCollection<CPDFOutlineNode> OutlineList
- {
- set; get;
- }
- public CPDFOutlineControl()
- {
- InitializeComponent();
- Loaded += CPDFOutlineControl_Loaded;
- }
- private void CPDFOutlineControl_Loaded(object sender, RoutedEventArgs e)
- {
- CPDFOutlineUI.OutlineSelectionChanged -= CPDFOutlineUI_OutlineSelectionChanged;
- CPDFOutlineUI.OutlineSelectionChanged += CPDFOutlineUI_OutlineSelectionChanged;
- }
- private void CPDFOutlineUI_OutlineSelectionChanged(object sender, object e)
- {
- try
- {
- TreeViewItem new_item = (TreeViewItem)e;
- CPDFOutline outline = (CPDFOutline)new_item.Tag;
- CPDFAction action = outline.GetAction();
- if (action != null && action.ActionType != C_ACTION_TYPE.ACTION_TYPE_UNKNOWN)
- {
- ViewControl.PDFViewTool.ActionProcess(action);
- }
- else
- {
- CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
- CPDFDocument pdfDoc = pdfViewer.GetDocument();
- CPDFDestination dest = outline.GetDestination(pdfDoc);
- if (dest != null)
- {
- pdfViewer.GoToPage(dest.PageIndex, new Point(dest.Position_X, dest.Position_Y));
- }
- }
- }
- catch(Exception ex)
- {
- }
- }
- private int GetIndexFromParent(List<CPDFOutline> parentlist, CPDFOutline outline)
- {
- for (int i = 0; i < parentlist.Count; i++)
- {
- if (parentlist[i] == outline)
- {
- return i;
- }
- }
- return -1;
- }
- public CPDFOutlineNode FindOutlineFromDto(ObservableCollection<CPDFOutlineNode> list, CPDFOutline outline)
- {
- foreach (CPDFOutlineNode item in list)
- {
- CPDFOutline parent = outline.GetParent();
- CPDFOutline PARENT = item.PDFOutline.GetParent();
- item.CurrentNodeName = outline.Title;
- int i = GetIndexFromParent(parent.ChildList, outline);
- int I = GetIndexFromParent(PARENT.ChildList, item.PDFOutline);
- if (item.PDFOutline.Title == outline.Title && i == I && outline.Level == item.PDFOutline.Level && ((parent.Title == PARENT.Title) || (parent.Title == null && PARENT.Title == null)))
- {
- return item;
- }
- else if (item.ChildrenNodeList.Count > 0)
- {
- CPDFOutlineNode retdto = FindOutlineFromDto(item.ChildrenNodeList, outline);
- if (retdto != null)
- {
- return retdto;
- }
- }
- }
- return null;
- }
- private CPDFOutlineNode ConvertCPDFToOutlineNode(CPDFOutline outline, CPDFOutlineNode parent)
- {
- CPDFOutlineNode node = new CPDFOutlineNode();
- if (outline != null)
- {
- node.PDFOutline = outline;
- if (parent != null)
- {
- node.ParentNode = parent;
- }
- if (OutlineList != null && OutlineList.Count > 0)
- {
- CPDFOutlineNode oldnode = FindOutlineFromDto(OutlineList, node.PDFOutline);
- }
- node.ChildrenNodeList = new ObservableCollection<CPDFOutlineNode>();
- if (outline.ChildList.Count > 0)
- {
- foreach (CPDFOutline item in outline.ChildList)
- {
- node.ChildrenNodeList.Add(ConvertCPDFToOutlineNode(item, node));
- }
- }
- }
- return node;
- }
- public ObservableCollection<CPDFOutlineNode> GetOutlineViewDataSource()
- {
- ObservableCollection<CPDFOutlineNode> data = new ObservableCollection<CPDFOutlineNode>();
- if (ViewControl == null && ViewControl.PDFViewTool == null)
- {
- return data;
- }
- CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
- CPDFDocument pdfDoc = pdfViewer?.GetDocument();
- if(pdfDoc!=null)
- {
- List<CPDFOutline> datasource = pdfDoc.GetOutlineList();
- foreach (CPDFOutline item in datasource)
- {
- CPDFOutlineNode dto = ConvertCPDFToOutlineNode(item, null);
- if (dto != null)
- {
- data.Add(dto);
- }
- }
- }
-
- return data;
- }
- public void InitWithPDFViewer(PDFViewControl viewControl)
- {
- this.ViewControl = viewControl;
- if(ViewControl != null && viewControl.PDFViewTool!=null)
- {
- CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
- CPDFDocument pdfDoc=pdfViewer?.GetDocument();
- if(pdfDoc!=null)
- {
- List<CPDFOutline> outlineList = pdfDoc.GetOutlineList();
- CPDFOutlineUI.SetOutlineTree(outlineList);
- }
- }
- }
- }
- }
|