/*
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
}
}
|