pdfOutlines.cs :  » PDF » SharpPDF » sharpPDF » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » SharpPDF 
SharpPDF » sharpPDF » pdfOutlines.cs
using System;
using System.Text;
using System.Collections;

using sharpPDF.Bookmarks;
using sharpPDF.Exceptions;
using sharpPDF.Enumerators;

namespace sharpPDF{
  /// <summary>
  /// A Class that implements a PDF Outlines.
  /// </summary>
  internal class pdfOutlines : IWritable
  {
    
    private int _objectIDOutlines;
    private int _childIDFirst = 0;
    private int _childIDLast = 0;
    private int _childCount = 0;    
    private ArrayList _BookmarkRoot = new ArrayList();

    /// <summary>
    /// Outlines's ID
    /// </summary>
    public int objectIDOutlines
    {
      get
      {
        return _objectIDOutlines;
      }

      set
      {
        _objectIDOutlines = value;
      }
    }

    /// <summary>
    /// Class's constructor
    /// </summary>
    public pdfOutlines()
    {
      
    }

    /// <summary>
    /// Method that initialize
    /// </summary>
    /// <param name="counterID">Initial Object ID</param>
    /// <returns>Updated Object ID</returns>
    public int initializeOutlines(int counterID)
    {
      if (_BookmarkRoot.Count > 0)
      {    
        initializeBookmarks(counterID,_BookmarkRoot, _objectIDOutlines);  
        _childIDFirst = ((pdfBookmarkNode)_BookmarkRoot[0]).ObjectID;
        _childIDLast = ((pdfBookmarkNode)_BookmarkRoot[_BookmarkRoot.Count - 1]).ObjectID;
        counterID += _childCount;
      } else {
        _childCount = 0;
        _childIDFirst = 0;
        _childIDLast = 0;
      }
      return counterID;
    }

    /// <summary>
    /// Method that adds a bookmark to the outlines object
    /// </summary>
    /// <param name="Bookmark">BookmarkNode Object</param>
    public void addBookmark(pdfBookmarkNode Bookmark)
    {
      _BookmarkRoot.Add(Bookmark);
    }

    /// <summary>
    /// Method that initialize all bookmarks
    /// </summary>
    /// <param name="CounterID">Initial Object ID</param>
    /// <param name="StartPoint">ArrayList of BookmarkNodes of the same level</param>
    /// <param name="FatherID">Object ID of the father</param>
    /// <returns>Number of childs</returns>
    private int initializeBookmarks(int CounterID, ArrayList StartPoint, int FatherID)
    {
      int currentNodes = 0;
      if (StartPoint.Count > 0) {
        pdfBookmarkNode Node;
        for (int i = 0; i < StartPoint.Count; i++)
        {
          Node = (pdfBookmarkNode)StartPoint[i];        
          Node.ObjectID = CounterID + _childCount;        
          Node.parent = FatherID;                  
          currentNodes++;
          _childCount++;
          Node.childCount = initializeBookmarks(CounterID,  Node.Childs, Node.ObjectID);          
          if (Node.childCount > 0) {
            Node.first = Node.getFirstChild().ObjectID;
            Node.last = Node.getLastChild().ObjectID;  
          }
          if (StartPoint.Count > 1) {
            if (i > 0) {
              Node.prev = ((pdfBookmarkNode)StartPoint[i - 1]).ObjectID;
            }
            if (i < (StartPoint.Count - 1)) {
              Node.next = CounterID + _childCount;;
            }
          }
          currentNodes += Node.childCount;
        }
      }
      return currentNodes;
    }

    /// <summary>
    /// Method that returns the PDF codes to write the Outlines in the document
    /// </summary>
    /// <returns>String that contains PDF codes</returns>
    public string getText() 
    {
      StringBuilder strOutlines = new StringBuilder();  
      strOutlines.Append(_objectIDOutlines.ToString() + " 0 obj" + Convert.ToChar(13) + Convert.ToChar(10));
            strOutlines.Append("<<" + Convert.ToChar(13) + Convert.ToChar(10));
            strOutlines.Append("/Type /Outlines" + Convert.ToChar(13) + Convert.ToChar(10));
      if (_childCount != 0) {
        strOutlines.Append("/First "+ _childIDFirst.ToString() + " 0 R" + Convert.ToChar(13) + Convert.ToChar(10));
        strOutlines.Append("/Last "+ _childIDLast.ToString() + " 0 R" + Convert.ToChar(13) + Convert.ToChar(10));
        strOutlines.Append("/Count "+ _childCount.ToString() + Convert.ToChar(13) + Convert.ToChar(10));
      } else {
        strOutlines.Append("/Count 0" + Convert.ToChar(13) + Convert.ToChar(10));
      }
            strOutlines.Append(">>" + Convert.ToChar(13) + Convert.ToChar(10));
            strOutlines.Append("endobj" + Convert.ToChar(13) + Convert.ToChar(10));      
      return strOutlines.ToString();
    }

    /// <summary>
    /// Method that returns all nodes from a start collection
    /// </summary>
    /// <param name="StartPoint">ArrayList with the start point of pdfBookmarkNodes</param>
    /// <returns>Collection of all pdfBookmarkNodes from the start point</returns>
    private  ArrayList getNodes(ArrayList StartPoint)
    {
      ArrayList resultList = new ArrayList();
      if (StartPoint.Count > 0) {
        resultList.AddRange(StartPoint);
        foreach (pdfBookmarkNode Node in StartPoint)
        {
          resultList.AddRange(getNodes(Node.Childs));
        }
      }            
      return resultList;
    }

    /// <summary>
    /// Method that returns a sorted(by objectID) collection of pdfBookmarkNodes
    /// </summary>
    /// <returns>Sorted bookmark collection</returns>
    public ArrayList getBookmarks()
    {
      ArrayList returnedList = getNodes(_BookmarkRoot);
      returnedList.Sort();
      return returnedList;
    }
    
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.