PageManager.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » tools » 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 » PDF Clown 
PDF Clown » it » stefanochizzolini » clown » tools » PageManager.cs
/*
  Copyright 2008 Stefano Chizzolini. http://clown.stefanochizzolini.it

  Contributors:
    * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it)

  This file should be part of the source code distribution of "PDF Clown library"
  (the Program): see the accompanying README files for more info.

  This Program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later version.

  This Program is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY, either expressed or implied; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.

  You should have received a copy of the GNU General Public License along with this
  Program (see README files); if not, go to the GNU website (http://www.gnu.org/).

  Redistribution and use, with or without modification, are permitted provided that such
  redistributions retain the above copyright notice, license and disclaimer, along with
  this list of conditions.
*/

using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.objects;
using it.stefanochizzolini.clown.files;
using it.stefanochizzolini.clown.objects;

using System.Collections.Generic;

namespace it.stefanochizzolini.clown.tools{
  /**
    <summary>Tool for page management.</summary>
  */
  public class PageManager
  {
    #region dynamic
    #region fields
    private Document document;
    private Pages pages;
    #endregion

    #region constructors
    public PageManager(
      ) : this(null)
    {}

    public PageManager(
      Document document
      )
    {Document = document;}
    #endregion

    #region interface
    #region public
    /**
      <summary>Appends a document to the end of the document.</summary>

      <param name="document">The document to add.</param>
    */
    public void Add(
      Document document
      )
    {Add((ICollection<Page>)document.Pages);}

    /**
      <summary>Appends a collection of pages to the end of the document.</summary>

      <param name="pages">The pages to add.</param>
    */
    public void Add(
      ICollection<Page> pages
      )
    {
      /*
        NOTE: To be added to an alien document,
        pages MUST be firstly contextualized into it,
        then added to the target pages collection.
      */
      this.pages.AddAll(
        (ICollection<Page>)this.document.Contextualize(pages)
        );
      this.pages.Update(); // NOTE: Update is fundamental to override original page collection.
    }

    /**
      <summary>Inserts a document at the specified position in the document.</summary>

      <param name="index">Position at which the document is to be inserted.</param>
      <param name="document">The document to be inserted.</param>
    */
    public void Add(
      int index,
      Document document
      )
    {Add(index,(ICollection<Page>)document.Pages);}

    /**
      <summary>Inserts a collection of pages at the specified position in the document.</summary>

      <param name="index">Position at which the pages are to be inserted.</param>
      <param name="pages">The pages to be inserted.</param>
    */
    public void Add(
      int index,
      ICollection<Page> pages
      )
    {
      /*
        NOTE: To be added to an alien document,
        pages MUST be firstly contextualized into it,
        then added to the target pages collection.
      */
      // Add the source pages to the document (contextualize)!
      /* NOTE: Deep addition. */
      ICollection<Page> addingPages = (ICollection<Page>)document.Contextualize(pages);
      // Add the source pages to the pages collection!
      /* NOTE: Shallow addition. */
      if(index >= this.pages.Count)
      {this.pages.AddAll(addingPages);}
      else
      {this.pages.InsertAll(index,addingPages);}
      this.pages.Update(); // NOTE: Update is fundamental to override original page collection.
    }

    /**
      <summary>Gets/Sets the document being manipulated.</summary>
    */
    public Document Document
    {
      get
      {return document;}
      set
      {
        document = value;
        pages = document.Pages;
      }
    }

    /**
      <summary>Extracts a page range from the document.</summary>

      <param name="beginIndex">The beginning index, inclusive.</param>
      <param name="endIndex">The ending index, exclusive.</param>
      <returns>Extracted page range.</returns>
    */
    public Document Extract(
      int beginIndex,
      int endIndex
      )
    {
      Document extractedDocument = new File().Document;

      // Add the pages to the target file!
      /*
        NOTE: To be added to an alien document,
        pages MUST be contextualized within it first,
        then added to the target pages collection.
      */
      extractedDocument.Pages.AddAll(
        (ICollection<Page>)extractedDocument.Contextualize(
          pages.GetSlice(beginIndex,endIndex)
          )
        );

      return extractedDocument;
    }

    /**
      <summary>Moves a page range to a target position within the document.</summary>

      <param name="beginIndex">The beginning index, inclusive.</param>
      <param name="endIndex">The ending index, exclusive.</param>
      <param name="targetIndex">The target index.</param>
    */
    public void Move(
      int beginIndex,
      int endIndex,
      int targetIndex
      )
    {
      int pageCount = pages.Count;

      IList<Page> movingPages = pages.GetSlice(beginIndex,endIndex);

      // Temporarily remove the pages from the pages collection!
      /* NOTE: Shallow removal. */
      pages.RemoveAll(movingPages);

      // Adjust indexes!
      pageCount -= movingPages.Count;
      if(targetIndex > beginIndex)
      {targetIndex -= movingPages.Count; /* Adjusts the target position due to shifting for temporary page removal. */}

      // Reinsert the pages at the target position!
      /* NOTE: Shallow addition. */
      if(targetIndex >= pageCount)
      {pages.AddAll(movingPages);}
      else
      {pages.InsertAll(targetIndex,movingPages);}
      pages.Update(); // NOTE: Update is fundamental to override original page collection.
    }

    /**
      <summary>Removes a page range from the document.</summary>

      <param name="beginIndex">The beginning index, inclusive.</param>
      <param name="endIndex">The ending index, exclusive.</param>
    */
    public void Remove(
      int beginIndex,
      int endIndex
      )
    {
      IList<Page> removingPages = pages.GetSlice(beginIndex,endIndex);

      // Remove the pages from the pages collection!
      /* NOTE: Shallow removal. */
      pages.RemoveAll(removingPages); pages.Update();

      // Remove the pages from the document (decontextualize)!
      /* NOTE: Deep removal. */
      document.Decontextualize(removingPages);
    }

    /**
      <summary>Splits the document into multiple single-paged documents.</summary>

      <returns>A list of single-paged documents.</returns>
    */
    public IList<Document> Split(
      )
    {
      IList<Document> documents = new List<Document>();
      foreach(Page page in pages)
      {
        Document pageDocument = new File().Document;
        pageDocument.Pages.Add(
          (Page)page.Clone(pageDocument)
          );
        documents.Add(pageDocument);
      }

      return documents;
    }
    #endregion
    #endregion
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.